mysql 主從配置

Mysql 5.6.41安裝 

服務器節點兩臺:  192.168.1.129(主),192.168.1.131(從)

一,192.168.1.129(主)/etc/my.cnf配置文件加入

# # server-id 唯一的服務辨識號,數值位於 1 到 2^32-1之間.
# # 此值在master和slave上都需要設置.
# # 如果 “master-host” 沒有被設置,則默認爲1, 但是如果忽略此選項,MySQL不會作爲master生效.
server-id = 1

#[必須]啓用二進制日誌
log-bin = mysql-bin

# # 需要開啓生成二進制日誌記錄相關配置,配置在需要被複制的服務器上,即:master
#指定對名稱爲test_db的數據庫記錄二進制日誌
binlog-do-db = test

#指定不對名稱爲mysql的數據庫記錄二進制日誌
binlog-ignore-db = mysql

#指定不對名稱爲information_schema的數據庫記錄二進制日誌                  #
binlog-ignore-db = information_schema

#binlog日誌格式,mysql默認採用,如果從服務器slave有別的slave要複製那麼該slave也需要這一項#
binlog_format = mixed

#超過7天的binlog刪除###
expire_logs_days = 7

重啓mysql後,連接進mysql 查看狀態

[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.6.41-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000002 |     1388 | test         | mysql,information_schema |                   |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)

mysql> 

#給ip地址爲所有(%表示任何ip)MySQL服務器上的 盈月 授權對該master的複製權限
#mysql> grant replication slave on *.* to 'yingyue'@'%';
#給ip地址爲所有(%表示任何ip)MySQL服務器上的 盈月 授權對該master的複製權限
mysql> grant replication slave on *.* to 'hxs'@'192.168.1.%' identified by '123456';

#刷新權限
mysql> flush privileges; 

主數據庫配置完成

二, 192.168.1.131(從)/etc/my.cnf配置文件加入

server-id = 2 
#需要做複製的數據庫,如果複製多個數據庫,重複設置這選項即可master上不需要此項,slave上需要
replicate-do-db = test

#不需要複製的數據庫,如果要忽略複製多個數據庫,重複設置這個選項即可 
replicate-ignore-db = mysql
replicate-ignore-db = information_schema

#MySQL選項以避免外部鎖定。該選項默認開啓
skip-external-locking 
    
#默認存儲引擎
default-storage-engine = InnoDB
#停止從服務器
mysql> stop slave;

#重置從服務器
mysql> reset slave;

#從服務器關聯到主服務器
mysql> change master to master_user='hxs', 
master_password='123456', 
master_host='192.168.1.129',
master_port=3306, 
#可以在主數據庫129上通過 show master status 查看
master_log_file='mysql-bin.000002',
master_log_pos=1388;

#開啓從服務器
mysql> start slave

#查看從服務器連接狀態
 

mysql> show slave status\G ;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.129
                  Master_User: hxs
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 601
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql,information_schema
           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: 601
              Relay_Log_Space: 460
              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: 209b7324-cd01-11e8-9652-000c298cf5f5
             Master_Info_File: /usr/local/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)


是否成功就看
Slave_IO_Running: Yes 
Slave_SQL_Running: Yes
是否都爲Yes
如果有出現以上結果,則配置成功!

三,驗證主從是否可以成功同步,在主庫中進行增刪改查,看從表能否正確同步

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