mysql主從服務器搭建

  當業務流量過大時,我們的一臺服務器可能難以負載,我們需要用到主從服務器的配置。

具體配置如下:

172.17.10.57位主服務器

172.17.55.206 從服務器

正確的安裝數據庫後,確保兩臺的能互通。


 1  主服務器建立帳號

mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456';

//一般不用root帳號,“%”表示所有客戶端都可能連,只要帳號,密碼正確,此處可用具體客戶端IP代替,如192.168.145.226,加強安全

 先進行測試,看是否能正確的連接。對數據的權限管理也要清楚的瞭解。


2主服務器配置如下:

在/etc/my.cn

#vi /etc/my.cnf
       [mysqld]
       log-bin=mysql-bin   //[必須]啓用二進制日誌
       server-id=222       //[必須]服務器唯一ID,默認是1,一般取IP最後一段


3、修改從服務器slave:
   #vi /etc/my.cnf
       [mysqld]
       log-bin=mysql-bin   //[必須]啓用二進制日誌
       server-id=226       //[必須]服務器唯一ID,默認是1,一般取IP最後一段



4、重啓兩臺服務器的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;



7、檢查從服務器複製功能狀態:

   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的黑白名單那一樣。

   建議在從服務器上進行配置








如果出現錯誤記得來這裏找

mysql 互爲主從複製常見問題


報錯: 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






3、雙主從服務器複製
 (1)第一臺服務器設置
auto_increment_increment = 2   \\數據表記錄的標識增長數,一般等於服務器的數量
auto_increment_offset = 1      \\數據表記錄標識每次增加的數,一般第一臺爲1,第二臺爲2,以此類推
sync_binlog = 0                \\二進制寫入磁盤的同步方式

 (2)第二臺服務器配置
auto_increment_increment = 2
auto_increment_offset = 2
sync_binlog = 0









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章