標籤:database mysql master
主伺服器:10.236.51.151
從伺服器:10.236.51.152
安裝mysql
yum -y install mysql-devel mysql-server
運行安全設定嚮導
mysql_secure_installation
主伺服器
cp /usr/share/mysql/my-large.cnf /etc/my.cnf
修改master端的/etc/my.cnf檔案
vi /etc/my.cnf
server_id = 1 #(為1表示master,2表示slave)
binlog-do-db = test #(test表示要同步的資料庫),如同步全部資料庫可注釋本行
binlog-ignore-db= mysql #設定不需要同步的資料庫,多個資料庫可用“,”分隔開(一般這條可以不寫)
log-slave-updates
slave-skip-errors
log-bin = mysql-bin
進入mysql
mysql -uroot -p
建立一個資料庫test
mysql>create database test;
建立同步處理的使用者
在主伺服器上為從伺服器建立一個連線帳戶,該帳戶必須授予REPLICAITON SLAVE許可權。
mysql> grant replication slave on *.* to ‘replication‘@‘10.236.51.152‘ identified by ‘password‘;
mysql> flush privileges;
重啟mysql
/etc/init.d/mysqld restartmysql -uroot -p
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000002
Position: 329
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
mysql> unlock tables;
註:這裡鎖表的目的是為了生產環境中不讓進新的資料,好讓從伺服器定位同步位置。初次同步完成後,記得解鎖。
從伺服器
cp /usr/share/mysql/my-large.cnf /etc/my.cnf
修改slave端的/etc/my.cnf檔案
[mysqld]
server-id = 2
log-bin = mysql-bin
replicate-do-db = test
replicate-ignore-db = mysql,information_schema
重啟mysql
用change master語句指定同步位置
mysql> change master to master_host=‘10.236.51.151‘, master_user=‘replication‘, master_password=‘password‘, master_log_file=‘mysql-bin.000002‘, master_log_pos=0;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
mysql> stop slave
-> ;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host=‘10.236.51.151‘, master_user=‘replication‘, master_password=‘password‘, master_log_file=‘mysql-bin.000002‘, master_log_pos=0;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.236.51.151
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 329
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 474
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
Replicate_Ignore_DB: mysql
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: 329
Relay_Log_Space: 630
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:
1 row in set (0.00 sec)
mysql>
本文出自 “wemux” 部落格,請務必保留此出處http://wemux.blog.51cto.com/2848943/1543959
Mysql主從同步