當業務流量過大時,咱們的一臺服務器可能難以負載,咱們須要用到主從服務器的配置。html
具體配置以下:mysql
172.17.10.57位主服務器linux
172.17.55.206 從服務器sql
正確的安裝數據庫後,確保兩臺的能互通。數據庫
1 主服務器創建賬號
安全
mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456';服務器
//通常不用root賬號,「%」表示全部客戶端均可能連,只要賬號,密碼正確,此處可用具體客戶端IP代替,如192.168.145.226,增強安全ide
先進行測試,看是否能正確的鏈接。對數據的權限管理也要清楚的瞭解。
測試
2主服務器配置以下:this
在/etc/my.cn
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必須]啓用二進制日誌
server-id=222 //[必須]服務器惟一ID,默認是1,通常取IP最後一段
三、修改從服務器slave:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必須]啓用二進制日誌
server-id=226 //[必須]服務器惟一ID,默認是1,通常取IP最後一段
四、重啓兩臺服務器的mysql
/etc/init.d/mysql restart
5 查看狀態
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 | 331 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
6 從服務器鏈接主服務器
mysql> change master to master_host='172.17.10.57', -> master_user='test', -> master_password='passwd', -> master_log_file='mysql-bin.000006'; mysql> start slave mysql> show slave status\G;
mysql> change master to master_host = '172.17.10.57',master_user = 'mysync',master_password='q123456',master_log_file='mysql-bin.000019',master_log_pos=120;
七、檢查從服務器複製功能狀態:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.222 //主服務器地址
Master_User: myrync //受權賬戶名,儘可能避免使用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
......
注:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,不然都是錯誤的狀態(如:其中一個NO均屬錯誤)。
以上操做過程,主從服務器配置完成。
總結:
1 首先須要檢查主從關係的連通性
2 創建一個合理權限的帳戶,只要給能複製的那個就行。
3 配置能複製過去的庫或者表格,既能夠在主服務器上配置,也能夠在從服務器上配置。
制定了一個能夠過去的,其他的就是不能夠過去的。這個就像linux的黑白名單那同樣。
建議在從服務器上進行配置
報錯: 1) change master致使的: Last_IO_Error: error connecting to master - retry-time: 60 retries 2) 在沒有解鎖的狀況下中止slave進程: stop slave; ERROR 1192 (HY000): Cant execute the given command because you have active locked tables
報錯:
1)
change master致使的:
Last_IO_Error: error connecting to master - retry-time: 60 retries
2)
在沒有解鎖的狀況下中止slave進程:
> stop slave;
ERROR 1192 (HY000): Can't execute the given command because you have active locked tables or an active transaction
3)
change master語法錯誤,落下逗號
mysql> change master to
-> master_host='IP'
-> master_user='USER',
-> master_password='PASSWD',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=106;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'master_user='USER',
master_password='PASSWD',
master_log_file='mysql-bin.000002' at line 3
4)
在沒有中止slave進程的狀況下change master
mysql> change master to master_host=‘IP', master_user='USER', master_password='PASSWD', master_log_file='mysql-bin.000001',master_log_pos=106;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
5)
A B的server-id相同:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids;
these ids must be different for replication to work (or the --replicate-same-server-id option must be used on
slave but this does not always make sense; please check the manual before using it).
查看server-id
mysql> show variables like 'server_id';
手動修改server-id
mysql> set global server_id=2; #此處的數值和my.cnf裏設置的同樣就行
mysql> slave start;
6)change master以後,查看slave的狀態,發現slave_IO_running 爲NO
須要注意的是,作完上述操做以後最後重啓mysql進程。
參考博客
http://www.cnblogs.com/hustcat/archive/2009/12/19/1627525.html
三、雙主從服務器複製 (1)第一臺服務器設置auto_increment_increment = 2 \\數據表記錄的標識增加數,通常等於服務器的數量auto_increment_offset = 1 \\數據表記錄標識每次增長的數,通常第一臺爲1,第二臺爲2,以此類推sync_binlog = 0 \\二進制寫入磁盤的同步方式 (2)第二臺服務器配置auto_increment_increment = 2auto_increment_offset = 2sync_binlog = 0