Linux Mysql主從複製
阿新 • • 發佈:2019-01-05
怎麼安裝mysql資料庫,這裡不說了,只說它的主從複製,步驟如下:
1、主從伺服器分別作以下操作:
1.1、版本一致
1.2、初始化表,並在後臺啟動mysql
1.3、修改root的密碼
2、如果是用虛擬機器克隆的mysql需要走這一步
因為是克隆所以server-uuid一樣,需要改成不一樣,檔案在locate auto.cnf
2、必須放在最後一行
修改主伺服器master:(10.101.110.45)
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin #[必須]啟用二進位制日誌
server-id=1 #[必須]伺服器唯一ID,預設是1,
binlog-do-db=ourneeddb #需要同步的資料庫
3、修改從伺服器slave:(10.101.110.58)
#vi /etc/my.cnf
[mysqld]
server-id=2 #設定從伺服器id,必須於主伺服器不同
relay-log=mysql-relay #開啟relaylog
binlog-do-db=ourneeddb #需要同步的資料庫
binlog-ignore-db=mysql #不同步mysql系統資料庫,若還有其它不想同步的,繼續新增
4、重啟兩臺伺服器的 service mysql restart
-> TO [email protected]'%'
6、登入主伺服器的mysql,查詢master的狀態
mysql>show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 308 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
注:執行完此步驟後不要再操作主伺服器MYSQL,防止主伺服器狀態值變化
-> master_user = 'mysql_backup',
-> master_password = '123456',
-> master_log_file = 'mysql-bin.000008',
8、檢查從伺服器複製功能狀態:
mysql> show slave status\G
*************************** 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
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: 420
Relay_Log_Space: 452
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: 24529cd4-b96f-11e7-9160-000c29807e7e
Master_Info_File: /usr/local/mysql/data/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、主從伺服器分別作以下操作:
1.1、版本一致
1.2、初始化表,並在後臺啟動mysql
1.3、修改root的密碼
2、如果是用虛擬機器克隆的mysql需要走這一步
因為是克隆所以server-uuid一樣,需要改成不一樣,檔案在locate auto.cnf
2、必須放在最後一行
修改主伺服器master:(10.101.110.45)
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin #[必須]啟用二進位制日誌
server-id=1 #[必須]伺服器唯一ID,預設是1,
binlog-do-db=ourneeddb #需要同步的資料庫
binlog-ignore-db=mysql #不同步mysql系統資料庫,若還有其它不想同步的,繼續新增
#vi /etc/my.cnf
[mysqld]
server-id=2 #設定從伺服器id,必須於主伺服器不同
relay-log=mysql-relay #開啟relaylog
binlog-do-db=ourneeddb #需要同步的資料庫
binlog-ignore-db=mysql #不同步mysql系統資料庫,若還有其它不想同步的,繼續新增
4、重啟兩臺伺服器的 service mysql restart
5、在主伺服器上建立帳戶並授權slave:(用來從伺服器連線主伺服器)
#/usr/local/mysql/bin/mysql -uroot -proot
mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.*-> TO [email protected]'%'
-> IDENTIFIED BY '123456';
在主伺服器建立好使用者,最好在從伺服器mysql -h主的ip -u使用者名稱 -p是否能訪問主的mysql資料庫,如果不能可能是防火牆的問題6、登入主伺服器的mysql,查詢master的狀態
mysql>show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 308 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
注:執行完此步驟後不要再操作主伺服器MYSQL,防止主伺服器狀態值變化
7、配置從伺服器Slave:
mysql> stop slave;
mysql> CHANGE MASTER TO master_host = '127.0.0.1',-> master_user = 'mysql_backup',
-> master_password = '123456',
-> master_log_file = 'mysql-bin.000008',
-> master_log_pos = 120;
Mysql>start slave; //啟動從伺服器複製功能8、檢查從伺服器複製功能狀態:
mysql> show slave status\G
*************************** 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
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: 420
Relay_Log_Space: 452
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: 24529cd4-b96f-11e7-9160-000c29807e7e
Master_Info_File: /usr/local/mysql/data/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)
注:Slave_IO及Slave_SQL程序必須正常執行,即YES狀態,否則都是錯誤的狀態(如:其中一個NO均屬錯誤)。以上操作過程,主從伺服器配置完成