Mysql主從複製

折騰了幾天,終於弄清了mysql主從複製,原來如此簡單html

 

1.主從保持版本一致,mysql

2.開啓主服務器的bin-logsql

[root@cetos2 ~]# whereis my.cn
my: /etc/my.cnf
[root@cetos2 ~]# vim /etc/my.cnfshell

 

 

server-id=129  //服務器id 這個要與從服務器區分
log_bin=mysql-bin
log_bin_index=mysql-bin.index
expire_logs_days=10
max_binlog_size=100M數據庫

重啓服務vim

[root@cetos2 ~]# systemctl restart mysql;
[root@cetos2 ~]# 服務器

--查看主服務器日誌是否已開啓  ON表明已開啓ide

mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)測試

 

3.修改從服務器slaveui

#vi /etc/my.cnf [mysqld] log-bin=mysql-bin //[不是必須]啓用二進制日誌 server-id=226 //[必須]服務器惟一ID,默認是1,通常取IP最後一段

4.在主服務器上創建賬戶並受權slave:

mysql>GRANT REPLICATION SLAVE ON *.* 'repl'@'%' identified by 'm123456';

//一般複製用單獨的帳號 repl

5. 登陸主服務器的mysql,查詢master的狀態


mysql> show master status\g;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

ERROR:
No query specified

注:執行完此步驟後不要再操做主服務器MYSQL,防止主服務器狀態值變化

 

6.配置從服務器Slave:


mysql> change master to master_host='192.168.146.128',master_user='repl',
-> master_password='m123456',
-> master_log_file='mysql-bin.000004',master_log_pos=120;

  • master_host:Master 服務器IP
  • master_user:Master 服務器受權用戶,也就是 Master 前面建立的那個用戶
  • master_password:Master 服務器受權用戶對應的密碼
  • master_log_file:Master binlog 文件名,對應查詢Master服務器狀態時,File字段
  • master_log_pos:Master binlog 文件中的 Postion 值,對應查詢Master服務器狀態時,Position字段
  • 更多的選項能夠看:http://dev.mysql.com/doc/refman/5.7/en/change-master-to.html  

Mysql>start slave; //啓動從服務器複製功能

若是上面有錯誤能夠從新配置

在從庫上reset slave all  ,從新配置,用repl用戶

mysql> reset slave all

mysql>stop slave //中止

mysql>start slave //啓動

7.檢查從服務器複製功能狀態:

mysql> show slave status\G

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.146.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 517
Relay_Log_File: mysqld-relay-bin.000010
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000006
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: 517
Relay_Log_Space: 457
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: 128
Master_UUID: 96ed73d8-942e-11ea-ae75-000c29f06575
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)

ERROR:
No query specified

 

*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.222 //主服務器地址 Master_User: mysync //受權賬戶名,儘可能避免使用root Master_Port: 3306 //數據庫端口,部分版本沒有此行 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 600 //#同步讀取二進制日誌的位置,大於等於Exec_Master_Log_Pos Relay_Log_File: ddte-relay-bin.000003 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes //此狀態必須YES Slave_SQL_Running: Yes //此狀態必須YES ...... 注:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,不然都是錯誤的狀態(如:其中一個NO均屬錯誤)。 以上操做過程,主從服務器配置完成。

8.主從服務器測試

主服務器Mysql,創建數據庫,並在這個庫中建表插入一條數據:
能夠看到從服務器已能同步

轉自 http://www.javashuo.com/article/p-ncqrdqvr-co.html

相關文章
相關標籤/搜索