1、搭建过程:https://www.cnblogs.com/naruto123/p/8138708.html
grant replication slave on *.* to ‘从库用户名(zhf)‘@‘从库主机地址(127.0.0.1)‘identified by ‘密码(123456)‘;
flush privileges;
(这里grant replication slave on 不能指定数据库,只能是*.*,否则会报ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
mysql> grant replication slave on test.* to ‘zhf‘@‘127.0.0.1‘ identified by ‘123456‘; ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES mysql> grant replication slave on *.* to ‘zhf‘@‘127.0.0.1‘ identified by ‘123456‘; Query OK, 0 rows affected, 1 warning (0.00 sec)
)
现在我们切到从库(slave),把主库与从库联系起来,使用上述授权replication slave的账号连接,master_log_pos就是在主库执行show master status \G的pos位置,
执行以下命令:
change master to master_host=‘127.0.0.1‘,master_port=3307,master_user=‘zhf‘,master_password=‘123456‘,master_log_file=‘master-bin.000012‘,master_log_pos=596;
2、主从搭建 每个Node都要保证server_id、server_uuid唯一。
mysql> show global variables like ‘server%‘; +----------------+--------------------------------------+ | Variable_name | Value | +----------------+--------------------------------------+ | server_id | 2 | | server_id_bits | 32 | | server_uuid | 6a45f11f-db80-11ea-a5e2-040e3c1a6e98 | +----------------+--------------------------------------+ 3 rows in set, 1 warning (0.00 sec)
3、对于需要保证master-slave主从同步的salve库,如果要设置为只读状态,需要在从库执行的命令为:
set read_only=1;
read_only=1只读模式,可以限定普通用户进行数据修改的操作,但不会限定具有super权限的用户(如超级管理员root用户)的数据修改操作。
上述设置重启服务就会变为read_only为0,要永久设置得放在my.ini [mysqld]read_only=1
4、start slave表示开启从库复制功能,Mysql服务restart之后也是开启的,无需再start slave
5、MySQL主从复制,启动slave时报错Slave failed to initialize relay log info structure from the repository
解决办法:https://blog.csdn.net/weixin_37998647/article/details/79950133
现象:
mysql> start slave; ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository 解决步骤: mysql> reset slave; Query OK, 0 rows affected (0.01 sec) mysql> change master to ...... mysql> start slave; Query OK, 0 rows affected (0.00 sec) 这样slave 就可以启动了。
6、MySQL数据同步,出现Slave_SQL_Running:no和slave_io_running:no,问题的解决方法:https://www.cnblogs.com/l-hh/p/9922548.html
7、innodb对应的文件*.frm、*.ibd无法拷贝,提示mysql5.7 Tablespace ‘`mysql`.`engine_cost`‘ exists.
解决办法删掉对应的两个文件,再用Sql语句导入或者mysqldump导入。
8、如果show slave status,Slave_IO_Running为NO,Last_IO_Error提示:
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
解决办法:https://blog.csdn.net/tpc4289/article/details/79899089
现象:
*************************** 1. row *************************** Slave_IO_State: Master_Host: 127.0.0.1 Master_User: zhf Master_Port: 3307 Connect_Retry: 60 Master_Log_File: master-bin.000012 Read_Master_Log_Pos: 596 Relay_Log_File: slave-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: master-bin.000012 Slave_IO_Running: No 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: 596 Relay_Log_Space: 154 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: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 1593 Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: Master_Info_File: D:\middleware\mysql5.7-slave\data\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: 200811 16:05:01 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)