修改MySQL配置:
[root@192 ~]# vi /etc/my.cnf # [必须]主服务器唯一ID server-id=1 # [必须]启用二进制日志,指名路径。比如:自己本地的路径/log/mysqlbin log-bin=mysql-bin # [可选]0表示读写(主机),1表示只读(从机) ,默认0 read-only=0 # [可选]设置需要复制的数据库,默认全部记录 binlog-do-db=master_slave_001 # [可选]binlog保存天数 #expire_logs_days=30 # [可选]设置不要复制的数据库 #binlog-ignore-db=test # [可选]设置binlog格式(支持的值:STATEMENT、ROW、MIXED,默认:STATEMENT) #binlog_format=STATEMENT # [可选]控制单个二进制日志大小。此参数的最大和默认值是1GB #max_binlog_size=100M
重启MySQL:
[root@192 ~]# systemctl restart mysqld
如果启动不起来请检查mysqld日志(日志文件定义在my.cnf中):
[root@192 ~]# tailf -100 /var/log/mysqld.log
登陆MySQL:
[root@192 ~]# mysql -uroot -p
创建账号授权主从复制:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.3.202' IDENTIFIED BY 'slaveslave'; Query OK, 0 rows affected, 1 warning (0.02 sec)
查看主服务器状态,记录File、Position这两个值,配置从机复制主机的时候会用到:
mysql> show master status; +------------------+----------+------------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+------------------+------------------+-------------------+ | mysql-bin.000003 | 450 | master_slave_001 | | | +------------------+----------+------------------+------------------+-------------------+ 1 row in set (0.01 sec)
到此主服务器的MySQL配置完成,不要乱动了。
修改从服务器的MySQL Server的UUID。
因为克隆的虚拟机这个值与被克隆的虚拟机是一样的,配置主从复制时某些命令会报错。
修改配置:
[root@192 ~]# vi /var/lib/mysql/auto.cnf [auto] server-uuid=719d637e-7f5a-11ec-ad32-005056327f01
重启MySQL:
[root@192 ~]# systemctl restart mysqld
修改MySQL配置:
[root@192 ~]# vi /etc/my.cnf # [必须]从服务器唯一ID server-id=2 # [可选]启用中继日志指定文件名 relay-log=mysql-relay
重启MySQL:
[root@192 ~]# systemctl restart mysqld
登陆MySQL:
[root@192 ~]# mysql -uroot -p
配置从机复制主机:
mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.3.201', -> MASTER_USER='slave', -> MASTER_PASSWORD='slaveslave', -> MASTER_LOG_FILE='mysql-bin.000003', -> MASTER_LOG_POS=450; Query OK, 0 rows affected, 2 warnings (0.02 sec)
启动slave同步:
mysql> start slave; Query OK, 0 rows affected (0.01 sec)
如果报错执行如下命令重置relaylog日志:
mysql> reset slave;
查看从服务器同步状态:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.3.201 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 450 Relay_Log_File: mysql-relay.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes ...
Slave_IO_Running: Yes
、Slave_SQL_Running: Yes
两个yes表示主从复制配置成功。连接主服务器
新建数据库:master_slave_001、创建表tb001、插入一条数据:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> create database master_slave_001; Query OK, 1 row affected (0.00 sec) mysql> use master_slave_001; Database changed mysql> create table tb001 (id int, name char(10)); Query OK, 0 rows affected (0.03 sec) mysql> insert into tb001 (`id`, `name`) value (1001, "张三"); Query OK, 1 row affected (0.02 sec) mysql> select * from tb001; +------+--------+ | id | name | +------+--------+ | 1001 | 张三 | +------+--------+ 1 row in set (0.00 sec)
连接从服务器
查看数据库、表以及数据:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | master_slave_001 | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use master_slave_001; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------------------+ | Tables_in_master_slave_001 | +----------------------------+ | tb001 | +----------------------------+ 1 row in set (0.00 sec) mysql> select * from tb001; +------+--------+ | id | name | +------+--------+ | 1001 | 张三 | +------+--------+ 1 row in set (0.00 sec)
至此,主从复制配置完成。
如果重启或关闭从服务器,当从服务器重新启动后,在主服务器binlog日志有效期内的写操作都会被自动同步到从服务器,不需要再在从服务器重新配置。
如果重启或关闭主服务器,从服务器 show slave status\G
会一直connecting中,当主服务器重新启动后,从服务器会自动重新连接。
查看:
mysql> show variables like 'expire_logs_days'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | expire_logs_days | 0 | +------------------+-------+ 1 row in set (0.01 sec)
修改:
[root@192 ~]# vi /etc/my.cnf # [可选]binlog保存天数 expire_logs_days=30
重启MySQL:
[root@192 ~]# systemctl restart mysqld
再次查看:
mysql> show variables like 'expire_logs_days'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | expire_logs_days | 30 | +------------------+-------+ 1 row in set (0.01 sec)
如果停止主从复制以后还要重新启用需要在从服务器重新配置一遍。
在从服务器执行:
mysql> stop slave; mysql> reset master;
mysqldump
命令加特定参数导出产生binlog之前的所有数据,然后在从服务器执行start slave
之前先导入该SQL。或者借助三方工具比如:xtrabackup。