一、为mysql运营主库添加一个repl 账号
[root@zabbix_server ~]# mysql -uroot -p -S /var/lib/mysql/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connectionid is 15778982Server version:5.7.25MySQL Community Server (GPL)
Copyright (c)2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type'help;' or '\h' for help. Type '\c' to clearthe current input statement.
mysql>
mysql> grant replication slave,replication client on *.* to 'repl'@'%' identified by 'xxxxxxxxxx';
二、修改主库my.cnf
增加以下字段
server_id=IP+PORT 标识服务器ID
log-bin=master-221 设置log_bin日志文件名
binlog_format=row 指定日志格式为row
server_id=693306log-bin=master-18-69binlog_format=row
三、重启主库mysql
停止mysql实例
[root@server-1 ~]# mysqladmin -uroot -p -S /home/mysql-5.7.26/mysql.sock shutdown
重启mysql实例
[root@server-1 ~]# mysqld_safe --defaults-file=/etc/mysql/my.cnf &
进入mysql
[root@server-1 ~]# mysql -uroot -p -S /home/mysql-5.7.26/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connectionid is 4Server version:5.7.26-log Source distribution
Copyright (c)2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type'help;' or '\h' for help. Type '\c' to clearthe current input statement.
mysql>
查看server_id变量
mysql> show variables like '%servier_id%';
Empty set (0.01sec)
mysql> show variables like '%server_id%';+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| server_id | 693306 |
| server_id_bits | 32 |
+----------------+--------+
2 rows in set (0.01sec)
mysql>
已经看到server_id变量已经成功设置。
四、修改从库my.cnf
添加server_id=IP+PORT
server_id=713306
五、重启mysql
[root@localhost mysql]# systemctl restart mysqld
[root@localhost mysql]#
进入mysql,查看server_id变量
[root@localhost data]# mysql -uroot -p -S /home/mysql-5.7.26/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connectionid is 2Server version:5.7.26MySQL Community Server (GPL)
Copyright (c)2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type'help;' or '\h' for help. Type '\c' to clearthe current input statement.
mysql> show variables like '%server_id%';+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| server_id | 713306 |
| server_id_bits | 32 |
+----------------+--------+
2 rows in set (0.01sec)
mysql>
已经变成我们设置值了,
六、接下来执行从库命令
mysql>change master to-> master_host='172.28.18.69',-> master_port=3306,-> master_user='repl',-> master_password='Zaq1xsw@',-> master_log_file='master-18-69.000001',-> master_log_pos=419;
Query OK,0 rows affected, 2 warnings (0.10sec)
mysql>
这里的 master_log_file和master_log_pos值,我们可以在主库上运行如下命令得到
mysql>show master status\G;*************************** 1. row ***************************File: master-18-69.000001Position:419Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: d24d8a53-880d-11e9-b1f3-842b2b5cdc15:1-7,
ee3e292b-866b-11e9-9df8-14feb5dc2c77:1-23
1 row in set (0.00sec)
ERROR:
No query specified
mysql>
执行成功后,我们启动从库操作
mysql>start slave;
Query OK,0 rows affected (0.00sec)
mysql>
查询从库状态
mysql>show slave status\G;*************************** 1. row ***************************Slave_IO_State: Waitingformaster to send event
Master_Host:172.28.18.69Master_User: repl
Master_Port:3306Connect_Retry:60Master_Log_File: master-18-69.000001Read_Master_Log_Pos:1593Relay_Log_File: localhost-relay-bin.000002Relay_Log_Pos:1497Relay_Master_Log_File: master-18-69.000001Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:0Last_Error:
Skip_Counter:0Exec_Master_Log_Pos:1593Relay_Log_Space:1708Until_Condition: None
Until_Log_File:
Until_Log_Pos:0Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:0Last_IO_Error:
Last_SQL_Errno:0Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:693306Master_UUID: ee3e292b-866b-11e9-9df8-14feb5dc2c77
Master_Info_File:/home/mysql-5.7.26/data/master.infoSQL_Delay:0SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waitingfor moreupdates
Master_Retry_Count:86400Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: d24d8a53-880d-11e9-b1f3-842b2b5cdc15:1-79,
ee3e292b-866b-11e9-9df8-14feb5dc2c77:1-15Auto_Position:0Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:1 row in set (0.00sec)
ERROR:
No query specified
mysql>
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
从库启动成功,同步成功
Exec_Master_Log_Pos: 1593
目前同步到主库数据库文件位置:1593
在主库上进入mysql,并查看主库状态
mysql>show master status\G;*************************** 1. row ***************************File: master-18-69.000001Position:1593Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: d24d8a53-880d-11e9-b1f3-842b2b5cdc15:1-7,
ee3e292b-866b-11e9-9df8-14feb5dc2c77:1-23
1 row in set (0.00sec)
ERROR:
No query specified
mysql>
同样主库文件最后位置也是1593,说明从库同步设置完毕