1. 程式人生 > 實用技巧 >windows環境下MySQL主從環境搭建 設定從庫只讀 以及搭建過程問題彙總

windows環境下MySQL主從環境搭建 設定從庫只讀 以及搭建過程問題彙總

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)