MySQL主從同步 配置(一主一從)

主從同步構建思路:

確保數據相同--從庫必須要有主庫中的數據

主服務配置--啓用binlog日誌,授權用戶,查看正在使用的日誌

從服務器配置--設定server_id,指定主庫信息

測試配置--客戶端連接主數據庫,寫入數據,從庫上也能查詢

主服務器:host50     192.168.4.50

從服務器:host51      192.168.4.51

一,在主服務器host50上配置主服務器

*打開配置文件  vim /etc/my.cnf  修改如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
general-log                
log-bin=master51  //啓用binlog日誌命名前綴master51
server_id=51         //指定服務器ID號
binlog_format="mixed"   //指定日誌格式
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"  

rpl_semi_sync_master-enabled = 1
rpl_semi_sync_slave-enabled = 1

/*高可用架構下。需要同時啓動master和slave
#validate_password_length=6
#validate_password_policy=0

#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

 

[root@host50]# systemctl   restart mysqld

[root@host50]#ls /var/lib/mysql/master50.*

 [root@host50]#mysql -uroot -p123456

用戶授權:給從庫host51添加連接時使用的用戶名

mysql> grant  replication slave  on  *.*  to repluser@"192.168.4.51" identified by "123qqq...A";

查看正在使用的binlog日誌信息

mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 |      154 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

*************************************************************************************************************************************

二,在host51中指定從庫信息

*打開配置文件  vim /etc/my.cnf  修改如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
server_id=51         //指定服務器ID號
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.

# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[root@host51]# systemctl   restart mysqld

進入數據庫

 [root@host50]#mysql -uroot -p123456

指定主庫信息

mysql>change master to 

           >master_user="repluser",               //主庫授權用戶名

           >master_host="192.168.4.50",       //指定主庫IP地址

           >master_password="123qqq...A",  //授權用戶名密碼

 

           >master_log_file="master51.000001",   //日誌文件

           >master_log_pos=154;                            //偏移位置

mysql>start slave;                    //啓動slave進程

mysql>show slave status\G          //查看slave狀態

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.4.50
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master51.000001
          Read_Master_Log_Pos: 599
               Relay_Log_File: host52-relay-bin.000005
                Relay_Log_Pos: 810
        Relay_Master_Log_File: master51.000002
             Slave_IO_Running: Yes                                 //IO線程已運行
            Slave_SQL_Running: Yes                              //SQL線程已運行
              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: 599
              Relay_Log_Space: 1183
              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: 51
                  Master_UUID: 7663861b-8be3-11e8-b256-5254001db4c8
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

三,測試配置

在主庫上操作數據:

                              -新建newdb庫t1表

                              -任意插入幾條表記錄

在從庫上查看數據更改情況

                               -確認新建的newdb庫t1表

                               -列出t1表的所有記錄

 

*******************************************************************************************************************************************

配置主從同步可能出現的問題

問題一:

Slave_IO 線程沒有運行

    ---報錯:Slave_IO_Running:NO

mysql>show slave status\G

                 Slave_IO_Running:NO

                 Last_IO_Error: 報錯信息......

原因分析

               ----連接不上master數據庫服務器

解決辦法:

               ---檢查物理連接(ping) 檢查授權用戶

               ---禁用防火牆 關閉SElinux

              ---binlog日誌文件指定錯誤(日誌名或pos節點)

mysql> stop slave

mysql>change master to 選項=值;

mysql>start slave;

問題二:

Slave_SQL 線程沒有運行

    ---報錯:Slave_SQL_Running:NO

mysql>show slave status\G

                 Slave_SQL_Running:NO

                 Last_SQL_Error: 報錯信息......

原因分析

               ----執行本機中繼日誌裏的sql命令時,sql命令使用的庫,表或記錄在本機並不存在

解決辦法:

mysql> stop slave;

mysal>......                                       //創建或恢復需要用到的庫或者表

mysql> start slave;

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