溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL 5.6.35主從同步配置案例

發布時間:2020-07-14 00:49:21 來源:網絡 閱讀:726 作者:xiaocao13140 欄目:MySQL數據庫

MySQL 5.6主從同步配置案例分享

本文環境  
主庫:Redhat 6.5 x64 192.168.1.180 mysql-5.6.35   
備庫:Redhat 6.5 x64 192.168.1.181 mysql-5.6.35

一、常規配置方式一
1. mysql主服務器配置
 增加如下配置:
# vi /etc/my.cnf

[mysqld]  
log-bin                = master-bin    
log-bin-index          = master-bin.index    
binlog_format          = mixed    
server-id              = 1

修改my.cnf后重啟mysql
#/etc/init.d/mysqld restart
[root@tmaster bin]# /etc/init.d/mysqld restart
Shutting down MySQL..                                      [  OK  ]
Starting MySQL.                                                 [  OK  ]


[root@tmaster bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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 |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000002 |     2290 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)
   
mysql>
2. 主服務器配置同步復制帳號

mysql>grant replication slave on *.* to 'repl'@'%' identified by '123456';  
--刷新一下權限
mysql>flush privileges;

3. mysql從服務器配置

說明: 默認只要server-id不相同即可。
--增加如下配置:
# vi /etc/my.cnf

[mysqld]  
log-bin                = slave-bin    
binlog_format          = mixed    
server-id              = 11    
relay-log              = slave-relay-bin    
relay-log-index        = slave-relay-bin.index

配置說明: 不同步mysql庫可以實現主從庫有不同的帳號權限,經過測試,mysql5.6.29中只在從庫中配置有效。

其它參數:  
binlog-do-db                = mydb    僅同步一個數據庫    
#replicate-ignore-db        = mysql   忽略掉mysql庫,該參數產生很多意外的同步問題,還是不使用。    
replicate_wild_ignore_table = mysql.% 忽略掉mysql庫

最終從庫my.cnf內容如下:
###
log-bin                = slave-bin    
binlog_format          = mixed    
server-id              = 11    
relay-log              = slave-relay-bin    
relay-log-index        = slave-relay-bin.index
binlog-do-db                = mydb
replicate_wild_ignore_table = mysql.%

--從庫mysql重啟后方可生效
# /etc/init.d/mysqld restart
[root@tslave ~]# /etc/init.d/mysqld restart
Shutting down MySQL..                                      [  OK  ]
Starting MySQL.                                            [  OK  ]


4. 測試示例

CREATE DATABASE mydb;
CREATE TABLE user (  
  id varchar(20) NOT NULL,  
  username varchar(20) NOT NULL,    
  password char(32) NOT NULL,    
  PRIMARY KEY (id)    
) ;

INSERT INTO user  VALUES ('1', 'koumm', '123456');  
INSERT INTO user  VALUES ('2', 'zhangsan', '123456');    
INSERT INTO user  VALUES ('3', 'lisi', '123456');    
INSERT INTO user  VALUES ('4', 'li2si', '123456');    
INSERT INTO user  VALUES ('5', 'abc', '123456');    
INSERT INTO user  VALUES ('6', 'tom', '123456');    
INSERT INTO user  VALUES ('7', 'jk', '123456');    
INSERT INTO user  VALUES ('8', 'xb', '123456');

5. 正常的主從配置過程
(1) 主庫鎖表

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)


mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000002 |     2290 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+


mysql> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |       420 |
| master-bin.000002 |      2290 |
+-------------------+-----------+
2 rows in set (0.00 sec)




(2) 主庫備份

[root@master ~]# mysqldump -uroot -p -B mydb >/mysql/backu/mydb.sql  
說明:-B參數有建庫語句。

(3) 主庫解開鎖表功能
mysql> unlock tables;  
Query OK, 0 rows affected (0.00 sec)    
mysql>

6. 從庫導入數據庫,輸入密碼后回車

[root@slave ~]# mysql -uroot -p  </mysql/backup/mydb.sql
Enter password:

7. 從庫配置同步過程
(1) 配置同步,手動執行同步參數,該配置會寫入master.info文件中。

mysql> CHANGE MASTER TO  
    -> MASTER_HOST='192.168.1.180',    
    -> MASTER_PORT=3306,    
    -> MASTER_USER='repl',    
    -> MASTER_PASSWORD='123456',    
    -> MASTER_LOG_FILE='master-bin.000002',    
    -> MASTER_LOG_POS=2290;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

(2) 啟動從同步進程

mysql> start slave;  
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.180
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 2290
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 284
        Relay_Master_Log_File: master-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table: mysql.%
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 2290
              Relay_Log_Space: 457
              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: 7baedb0e-5ea0-11e8-8692-000c292185b2
             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)

ERROR:
No query specified



#查看如下兩個參數為YES,說明從庫運行正常。  
             Slave_IO_Running: Yes    
            Slave_SQL_Running: Yes
(3) 測試同步情況

# 主庫插入一條記錄

mysql> use mydb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| user           |
+----------------+
1 row in set (0.00 sec)

mysql> select * from user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
| 1  | koumm    | 123456   |
| 2  | zhangsan | 123456   |
| 3  | lisi     | 123456   |
| 4  | li2si    | 123456   |
| 5  | abc      | 123456   |
| 6  | tom      | 123456   |
| 7  | jk       | 123456   |
| 8  | xb       | 123456   |
+----+----------+----------+
8 rows in set (0.00 sec)

mysql> insert into user values ('9','liang','654321');
Query OK, 1 row affected (0.01 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
| 1  | koumm    | 123456   |
| 2  | zhangsan | 123456   |
| 3  | lisi     | 123456   |
| 4  | li2si    | 123456   |
| 5  | abc      | 123456   |
| 6  | tom      | 123456   |
| 7  | jk       | 123456   |
| 8  | xb       | 123456   |
| 9  | liang    | 654321   |
+----+----------+----------+
9 rows in set (0.00 sec)


# 從庫查詢同步情況

mysql> use mydb
Database changed
mysql> select * from user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
| 1  | koumm    | 123456   |
| 2  | zhangsan | 123456   |
| 3  | lisi     | 123456   |
| 4  | li2si    | 123456   |
| 5  | abc      | 123456   |
| 6  | tom      | 123456   |
| 7  | jk       | 123456   |
| 8  | xb       | 123456   |
| 9  | liang    | 654321   |
+----+----------+----------+
9 rows in set (0.00 sec)




三、主從同步簡單管理

1. 停止MYSQL同步

STOP SLAVE IO_THREAD;     #停止IO進程
STOP SLAVE SQL_THREAD;    #停止SQL進程
STOP SLAVE;               #停止IO和SQL進程

2. 啟動MYSQL同步

START SLAVE IO_THREAD;    #啟動IO進程
START SLAVE SQL_THREAD;   #啟動SQL進程
START SLAVE;              #啟動IO和SQL進程

3. 重置MYSQL同步

RESET SLAVE;
清除主從同步參數,它會刪除master.info和relay-log.info文件,以及所有的中繼日志,并啟動一個新的中繼日志。
適用重新再次配置一次從庫的情況。

4. 查看MYSQL同步狀態

SHOW SLAVE STATUS;

5. 臨時跳過MYSQL同步錯誤

確保數據一致的情況下臨時的跳過這個錯誤操作如下,有可能要多次。

STOP SLAVE;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
START SLAVE;

# vi /etc/my.cnf

[mysqld]
#slave-skip-errors=1062,1053,1146 #跳過指定error no類型的錯誤
#slave-skip-errors=all            #跳過所有錯誤



原來參考:https://www.linuxidc.com/Linux/2016-04/130729.htm


其它相關資料參考:

實現兩個MySQL數據庫之間的主從同步 http://www.linuxidc.com/Linux/2016-02/128100.htm

Linux環境中MySQL主從同步--添加新的從庫 http://www.linuxidc.com/Linux/2015-08/122448.htm

通過 XtraBackup 實現不停機不鎖表搭建MySQL主從同步 http://www.linuxidc.com/Linux/2015-08/121806.htm

MySQL主從同步配置記錄 http://www.linuxidc.com/Linux/2015-07/119939.htm

Linux下MySQL數據庫主從同步配置 http://www.linuxidc.com/Linux/2016-03/129138.htm



向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女