Mysql主從配置

準(zhǔn)備工作

環(huán)境

  1. 兩臺(tái)虛擬機(jī),地址:master-192.168.52.130/slave-192.168.52.131;
  2. 系統(tǒng)版本:Centos 7;
  3. 軟件版本:mysql 5.6;

數(shù)據(jù)庫(kù)

  1. 同樣的配置:端口-3307等;
  2. 同樣的數(shù)據(jù):database-TestDB等;

主從配置

主庫(kù)配置

  1. 編輯 /etc/my.cnf:
[mysqld]
basedir=/var/lib/mysql
datadir=/var/lib/mysql/data
socket=/var/lib/mysql/mysql.sock
port=3307

log-bin=/var/lib/mysql/mysql-bin.log
explicit_defaults_for_timestamp=true

innodb_flush_log_at_trx_commit=1
sync_binlog=1
server-id=1

binlog-do-db=TestDB
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
binlog-ignore-db=test

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

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

配置項(xiàng)解釋:

server-id=1 標(biāo)識(shí)
binlog-do-db=TestDB 需要同步的數(shù)據(jù)庫(kù)
binlog-ignore-db=performance_schema 不需要同步的數(shù)據(jù)庫(kù)

  1. 創(chuàng)建同步用戶,在主服務(wù)器上為從服務(wù)器建立一個(gè)連接帳戶,該帳戶必須授予REPLICAITON SLAVE權(quán)限。在主服務(wù)器登陸mysql上執(zhí)行:
grant replication slave on *.* to 'master'@'%' identified by '123456';
  1. 重啟之后,查看狀態(tài):
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000009
         Position: 120
     Binlog_Do_DB: TestDB
 Binlog_Ignore_DB: performance_schema,mysql,test
Executed_Gtid_Set: 
1 row in set (0.01 sec)

注意上面的 File 和 Position ,從庫(kù)slave 配置的時(shí)候會(huì)使用。

從庫(kù)配置

  1. 編輯 /etc/my.cnf
[mysqld]

...

server_id=2
log-bin=mysql-bin.log
replicate-do-db=TestDB
  1. change master語(yǔ)句指定同步位置:
mysql>change master to master_host='192.168.52.130', master_user='master', master_password='123456', master_log_file='mysql-bin.000007', master_log_pos=120,master_port=3307;

  1. 啟用/關(guān)閉 slave;
start slave;

stop slave;
  1. 重啟,查看slave :show slave status\G;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.52.130
                  Master_User: master
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 120
               Relay_Log_File: mysqld-relay-bin.000010
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: TestDB
          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: 120
              Relay_Log_Space: 620
              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
                  Master_UUID: 1cacfa49-04c4-11e7-b744-000c29d5b78c
             Master_Info_File: /var/lib/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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
1 row in set (0.00 sec)

測(cè)試

在主庫(kù)進(jìn)行數(shù)據(jù)的操作,查看從庫(kù)的數(shù)據(jù)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容