主從復(fù)制的配置
軟件版本
1.雙方的MySQL要一致
2.如果不一致:主的要低于從的
從哪兒開始復(fù)制:
1.都從0開始
2.主服務(wù)器已經(jīng)運行一段時間讶凉,并且存在不小的數(shù)據(jù)集:把主服務(wù)器備份染乌,然后在從服務(wù)器恢復(fù),從主服務(wù)器上備份處的位置開始復(fù)制
配置過程:
主服務(wù)器:
1.改server-id
2.啟用二進制日志
3.創(chuàng)建有復(fù)制權(quán)限的賬號
grant replication slave,replication client on *.* to 'uername'@'hostname' identified by 'password'
flush privileges
從服務(wù)器:
1.改server-id
2.啟用中繼日志
relay-log = /mydata/relaylogs/slave-bin
3.連接主服務(wù)器
連接主服務(wù)器的命令:
CHANGE MASTER TO MASTER_HOST = '', MASTER_USER='', MASTER_PASSWORD='', MASTER_LOG_FILE='', MASTER_LOG_POS=;
MASTER_HOST = 'host_name'? 主服務(wù)器地址
MASTER_USER = 'user_name' 以那個用戶的身份復(fù)制
MASTER_PASSWORD = 'password' 密碼
MASTER_LOG_FILE = 'master_log_name' 復(fù)制開始文件
MASTER_LOG_POS = master_log_pos 那個節(jié)點開始
4.啟動復(fù)制線程
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
配置實例:主從復(fù)制之從零開始
主服務(wù)器
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;
從服務(wù)器
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%';?
連接主服務(wù)器
mysql
CHANGE MASTER TO MASTER_HOST = '192.168.130.61', MASTER_USER='repluser', MASTER_PASSWORD='replpass';
START SLAVE;
主服務(wù)創(chuàng)建數(shù)據(jù)庫測試主從復(fù)制
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE t1(ID INT);
INSERT INTO t1 VALUES(1),(2);
主服務(wù)器
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
從服務(wù)器
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)
配置實例:主從復(fù)制之半道復(fù)制
主服務(wù)器
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? root@192.168.130.62:/mydata/backups
從服務(wù)器
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?
主服務(wù)創(chuàng)建數(shù)據(jù)庫測試主從復(fù)制
CREATE DATABASE testdb1;
USE testdb1;
CREATE TABLE t1(ID INT);
INSERT INTO t1 VALUES(1),(2);
連接主服務(wù)器
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;
主服務(wù)器? APP開發(fā)找上海捌躍網(wǎng)絡(luò)科技有限公司 17621291122
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
從服務(wù)器
?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)
轉(zhuǎn)自:http://blog.51cto.com/kaiyuandiantang/2320090