標籤:
來自:http://www.osyunwei.com/archives/7269.html,改版
mysql主從複製
本文採用的是
centos6.5+mysql-5.6.23版本
之前在 windows7安裝過主從複製,現在在linux實現主從複製
mysql安裝方法:
http://www.cnblogs.com/lin3615/p/4376224.html
配置:
配置MySQL主伺服器(192.168.179.142)
從伺服器兩台(192.168.179.146,192.168.179.147)
資料庫就以 test為例,
// 從這裡開始配置第一台從伺服器
#建立一個MySQL主從資料庫同步處理的使用者 lin3615,密碼123456,並授予給192.168.179.146
登陸資料庫,進入控制台
insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject) values(‘localhost‘,‘lin3615‘,password(‘123456‘),‘‘,‘‘,‘‘);
#重新整理系統授權表
flush privileges;
#授權使用者lin3615 只能從 192.168.179.146 這個IP訪問主伺服器192.168.179.142上面的資料庫
grant replication slave on *.* to ‘lin3615‘@‘192.168.179.146‘ identified by ‘123456‘;
先把 主伺服器的資料庫複寫到 從伺服器,具體方法可參考:
http://www.cnblogs.com/lin3615/p/3749438.html
配置MySQL主伺服器(192.168.179.142)的my.cnf檔案
vim /etc/my.cnf
#編輯設定檔,在[mysqld]部分添加下面內容
server-id=1 #設定伺服器id,為1表示主伺服器,一般用IP地址接替log-bin=mysql-bin #啟動MySQ二進位日誌系統binlog-do-db=test #需要同步的資料庫名,如果有多個資料庫,可重複此參數,每個資料庫一行binlog-ignore-db=mysql #不同步mysql系統資料庫#儲存退出
service mysqld restart #重啟MySQLmysql -h localhost -u root -p #登陸show variables like ‘server_id‘; #查看server-id的值是否為1mysql> show variables like ‘server_id‘;show master status; #查看主伺服器,出現以下類似資訊
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000011 | 107 | osyunweidb | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
注意:這裡記住File的值:mysql-bin.000011和Position的值:107,後面會用到。
配置MySQL從伺服器(192.168.179.146)的my.cnf檔案
vim /etc/my.cnf #編輯設定檔,在[mysqld]部分添加下面內容server-id=2 #設定伺服器id,修改其值為2,表示為從資料庫log-bin=mysql-bin #啟動MySQ二進位日誌系統replicate-do-db=test #需要同步的資料庫名,如果有多個資料庫,可重複此參數,每個資料庫一行replicate-ignore-db=mysql #不同步mysql系統資料庫read_only #設定資料庫唯讀service mysqld restart #重啟MySQL
#進入MySQL控制台
#查看server-id的值,必須為上面設定的2,否則請返回修改設定檔
show variables like ‘server_id‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
1 row in set (0.01 sec)
#停止slave同步進程
stop slave; #此選項,之前測試時用 slave stop/slave start(啟動),現在測試一直報錯,日誌無法查看,原來是命令不對,搞了半天才知道
#執行同步語句
change master to master_host=‘192.168.179.142‘,master_user=‘lin3615‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘ ,master_log_pos=107;
#開啟slave同步進程
start slave;
#查看slave同步資訊
SHOW SLAVE STATUS\G
出現以下內容
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.21.128
Master_User: osyunweidbbak
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000011
Read_Master_Log_Pos: 107
Relay_Log_File: mysqlslave-relay-bin.000004
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000011
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: osyunweidb
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: 107
Relay_Log_Space: 560
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
1 row in set (0.00 sec)
mysql>
注意查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
以上這兩個參數的值為Yes,即說明配置成功!
// 結束配置第一台從伺服器
同理,配置第二台,從從伺服器開始,重新設定一個新的使用者名稱
即:
#建立一個MySQL主從資料庫同步處理的使用者 lin3615_1,密碼123456,並授予給192.168.179.147
insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject) values(‘localhost‘,‘lin3615_1‘,password(‘123456‘),‘‘,‘‘,‘‘);
重複第一台從庫的設定即可
接著可以測試,在主庫上進行 curd
mysql主從複製-linux版本