主從複製的配置
軟件版本
1.雙方的MySQL要一致
2.如果不一致:主的要低於從的
從哪兒開始複製:
1.都從0開始
2.主服務器已經運行一段時間,並且存在不小的數據集:把主服務器備份,然後在從服務器恢復,從主服務器上備份處的位置開始複製
配置過程:
主服務器:
1.改server-id
2.啓用二進制日誌
3.創建有複製權限的賬號
grant replication slave,replication client on *.* to 'uername'@'hostname' identified by 'password'
flush privileges
從服務器:
1.改server-id
2.啓用中繼日誌
relay-log = /mydata/relaylogs/slave-bin
3.連接主服務器
連接主服務器的命令:
CHANGE MASTER TO MASTER_HOST = '', MASTER_USER='', MASTER_PASSWORD='', MASTER_LOG_FILE='', MASTER_LOG_POS=;
MASTER_HOST = 'host_name' 主服務器地址
MASTER_USER = 'user_name' 以那個用戶的身份複製
MASTER_PASSWORD = 'password' 密碼
MASTER_LOG_FILE = 'master_log_name' 複製開始文件
MASTER_LOG_POS = master_log_pos 那個節點開始
4.啓動複製線程
START SLAVE [thread_types]
START SLAVE [SQL_THREAD] UNTIL
MASTER_LOG_FILE = 'log_name', MASTER_LOG_POS = log_pos
START SLAVE [SQL_THREAD] UNTIL
RELAY_LOG_FILE = 'log_name', RELAY_LOG_POS = log_pos
thread_types:
[thread_type [, thread_type] ... ]
thread_type: IO_THREAD | SQL_THREAD
配置實例:主從複製之從零開始
主服務器
service mysqld stop
vim /etc/my.cnf
server-id = 10
log-bin=/mydata/binlog/master-bin
chown -R mysql.mysql /mydata/binlog
service mysqld start
mysql
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.130.%' IDENTIFIED BY 'replpass';
FLUSH PRIVILEGES;
從服務器
service mysqld stop
vim /etc/my.cnf
#log-bin=mysql-bin
#binlog_format=mixed
server-id = 20
relay-log = /mydata/binlog/relay-bin
chown -R mysql.mysql /mydata/binlog/
service mysqld start
mysql
SHOW GLOBAL VARIABLES LIKE '%relay%';
連接主服務器
mysql
CHANGE MASTER TO MASTER_HOST = '192.168.130.61', MASTER_USER='repluser', MASTER_PASSWORD='replpass';
START SLAVE;
主服務創建數據庫測試主從複製
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE t1(ID INT);
INSERT INTO t1 VALUES(1),(2);
主服務器
SHOW MASTER STATUS\G;
*************************** 1. row ***************************
File: master-bin.000002
Position: 780
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
ERROR: No query specified
從服務器
SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.130.61
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000002
Read_Master_Log_Pos: 780
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 1080
Relay_Master_Log_File: master-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: 780
Relay_Log_Space: 2102
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: 10
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
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
1 row in set (0.00 sec)
配置實例:主從複製之半道複製
主服務器
service mysqld stop
vim /etc/my.cnf
server-id = 10
log-bin=/mydata/binlog/master-bin
chown -R mysql.mysql /mydata/binlog
service mysqld start
mysql
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.130.%' IDENTIFIED BY 'replpass';
FLUSH PRIVILEGES;
SHOW MASTER STATUS\G;
*************************** 1. row ***************************
File: master-bin.000002
Position: 780
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
ERROR: No query specified
mysqldump --all-databases --flush-logs --master-data=2 --lock-all-tables > /mydata/backups/all.sql
scp /mydata/backups/all.sql [email protected]:/mydata/backups
從服務器
service mysqld stop
vim /etc/my.cnf
#log-bin=mysql-bin
#binlog_format=mixed
server-id = 20
relay-log = /mydata/binlog/relay-bin
chown -R mysql.mysql /mydata/binlog/
service mysqld start
mysql
SHOW GLOBAL VARIABLES LIKE '%relay%';
mysql < /mydata/backups/all.sql
主服務創建數據庫測試主從複製
CREATE DATABASE testdb1;
USE testdb1;
CREATE TABLE t1(ID INT);
INSERT INTO t1 VALUES(1),(2);
連接主服務器
mysql
CHANGE MASTER TO MASTER_HOST = '192.168.130.61', MASTER_USER='repluser', MASTER_PASSWORD='replpass', MASTER_LOG_FILE='master-bin.000002', MASTER_LOG_POS=780;
START SLAVE;
主服務器
SHOW MASTER STATUS\G;
*************************** 1. row ***************************
File: master-bin.000003
Position: 828
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
ERROR: No query specified
從服務器
SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.130.61
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 828
Relay_Log_File: relay-bin.000004
Relay_Log_Pos: 1128
Relay_Master_Log_File: master-bin.000003
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: 828
Relay_Log_Space: 1479
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: 10
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
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
1 row in set (0.00 sec)