抽空重新整理了一下 mysql 主从同步的随笔,网上也有很多这样的,个人也踩了一些坑,希望对小伙伴们有一些帮助。


 

环境配置:

主: windows7  mysql5.7  (master)  192.168.0.192

从: 本地虚拟机 centos7.4  mysql5.7  (slave)   192.168.0.136

环境方面,可能不同的小伙伴们会遇到一些问题,我列举一下:

(1) 虚拟机网络ip配置不了,访问不了外网

(2) 虚拟机和本地ip互相 ping 不通

(3) 本地 mysql 开启外网访问失败

基本上我遇到的问题就这些,也都一一解决了,有遇到同类型问题的小伙伴可以联系我处理。

如果你没有遇到这些问题,那恭喜你,咱们继续吧 ^_^


 

master机器上的操作
1、更改配置文件,我们找到mysql的配置文件  my.ini 。
配置如下:

[mysqld]

log-bin=mysql-bin
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1
binlog_ignore_db=mysql

2、重启mysql,以使配置文件生效。

3、创建主从同步的mysql user

$ mysql -u root -p
Password:

##创建slave1用户,并指定该用户只能在主机192.168.0.136上登录。
mysql> CREATE USER 'slave1'@'192.168.0.136' IDENTIFIED BY 'slavepass';
Query OK, 0 rows affected (0.00 sec)

##为slave1赋予REPLICATION SLAVE权限。
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave1'@'192.168.0.136';
Query OK, 0 rows affected (0.00 sec)

4、为MYSQL加读锁为了主库与从库的数据保持一致,我们先为mysql加入读锁,使其变为只读

mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

 5、记录下来MASTER REPLICATION LOG 的位置该信息稍后会用到。

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File    | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |  613 |    |     |     |
+------------------+----------+--------------+------------------+-------------------+
row in set (0.00 sec)

 6、将master DB中现有的数据信息导出(这里我们可以另开也一个cmd窗口,来导出信息)

$ mysqldump -u root -p --all-databases --master-data > dbdump.sql

 7、解除master DB的读锁

mysql> UNLOCK TABLES;

8、将步骤6中的 dbdump.sql 文件 传到虚拟机中


 

slave 机器上的操作

1、更改配置文件我们找到文件 /etc/my.cnf 新增以下配置

[mysqld]
server-id=2
read-only=1

2、重启mysql,以使配置文件生效

3、导入从master DB。导出的dbdump.sql文件,以使master-slave数据一致

$ mysql -u root -p < /home/ubuntu/dbdump.sql

4、使slave与master建立连接,从而同步

$ mysql -u root -p
Password:

mysql> STOP SLAVE;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CHANGE MASTER TO
 -> MASTER_HOST='192.168.33.22',
 -> MASTER_USER='slave1',
 -> MASTER_PASSWORD='slavepass',
 -> MASTER_LOG_FILE='mysql-bin.000001',
 -> MASTER_LOG_POS=613;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

MASTER_LOG_FILE='mysql-bin.000001'与MASTER_LOG_POS=613的值,是从master机器中的SHOW MASTER STATUS得到的。经过如此设置之后,就可以进行master-slave同步了

5、到这里如果环境和配置都没问题,那就已经ok了,我们可以使用命令来查看同步情况

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.192
Master_User: slave1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 3222
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 3029
Relay_Master_Log_File: mysql-bin.000002
Slave_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: 0
Last_Error: 
Skip_Counter: 0
Exec_Master_Log_Pos: 3222
Relay_Log_Space: 3240
Until_Condition: None
Until_Log_File: 
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File: 
Master_SSL_CA_Path: 
Master_SSL_Cert: 
Master_SSL_Cipher: 
Master_SSL_Key: 
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error: 
Last_SQL_Errno: 0
Last_SQL_Error: 
Replicate_Ignore_Server_Ids: 
Master_Server_Id: 1
Master_UUID: 359eb423-722e-11e9-96fb-4ccc6a4d7344
Master_Info_File: /usr/local/mysql/var/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind: 
Last_IO_Error_Timestamp: 
Last_SQL_Error_Timestamp: 
Master_SSL_Crl: 
Master_SSL_Crlpath: 
Retrieved_Gtid_Set: 
Executed_Gtid_Set: 
Auto_Position: 0
Replicate_Rewrite_DB: 
Channel_Name: 
Master_TLS_Version: 
1 row in set (0.00 sec)

这里要注意这两个地方

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

必须要都是yes,如果连接不上,或者其他问题,那么就去最上面检查我说的那几个问题,是不是都解决了。

从服务器可以用这个命令测试是否能连接主机mysql:  mysql -u slave1 -p -h 192.168.0.192

到这里,没有问题的童鞋就可以自己测试,master这边改动,看slave那边是否同步了。

 

暂时分享到这里,借鉴了一些别人的文档,配置的时候如果有问题的小伙伴可以联系我 

 

 

 

 

 




posted on 2021-04-22 18:16  龟仙人  阅读(82)  评论(0编辑  收藏  举报