1. 程式人生 > >mysql5.7主從複製

mysql5.7主從複製

安裝環境

作業系統 :CentOS Linux release 7.5.1804 (Core)
資料庫版本:MySQL 5.7.24
主機A:192.168.1.11 (Master)
主機B:192.168.1.12 (Slave)

在Centos下安裝mysql參考:https://blog.csdn.net/mameng1988/article/details/82762666

1 Master的配置

在Linux環境下MySQL的配置檔案的位置是在 /etc/my.cnf ,在該檔案下指定Master的配置如下:

[mysqld]
log-bin=mysql-bin
server-id=11
#binlog-ignore-db=information_schema
#binlog-ignore-db=cluster
#binlog-ignore-db=mysql
#binlog-do-db=ufind_db 

這裡的server-id用於標識唯一的資料庫,這裡設定為11(IP的最後兩位),在設定從庫的時候就需要設定為其他值。
log-bin用於開啟二進位制日誌。
其他的都不是必需。

1.1 重啟mysql

[[email protected] etc]# service mysqld restart

1.2 進入mysql

[[email protected] etc]# mysql -u root -p

1.3 賦予從庫許可權帳號

允許使用者在主庫上讀取日誌,賦予192.168.1.12也就是Slave機器有File許可權,只賦予Slave機器有File許可權還不行,還要給它REPLICATION SLAVE的許可權才可以。

在Master資料庫命令列中輸入:

mysql>GRANT FILE ON *.* TO 'root'@'192.168.1.12' IDENTIFIED BY 'mysql password';

mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.1.12' IDENTIFIED BY 'mysql password';
mysql>FLUSH PRIVILEGES;

這裡使用的仍是 root 使用者作為同步的時候使用到的使用者,可以自己設定。

1.4 重啟mysql

[[email protected]
etc]# service mysqld restart

1.5 登入mysql

[[email protected] etc]# mysql -u root -p
     mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

這裡的 File 、Position 是在配置Salve的時候要使用到的。Binlog_Do_DB表示要同步的資料庫,Binlog_Ignore_DB 表示Ignore的資料庫,這些都是空的,說明我們沒有配置,說明它們不是必需的。

另外:如果執行這個步驟始終為Empty set(0.00 sec),那說明前面的my.cnf沒配置對。

2 Slave的配置

2.1 修改/etc/my.cnf

[mysqld]
server-id=12
relay_log=mysql-relay-bin
read_only=1
# 如果此slave需要作為其他mysql的master,則需要把下邊兩行註釋開啟。
# log-bin=mysql-bin
# log_slave_updates=1

server_id 是必須的,也是唯一的,這裡設定為12(IP的後兩位),不能和master及其他slave一樣。
relay_log 配置中繼日誌。
read_only 它防止改變資料(除了特殊的執行緒。

2.2 重啟mysql

[[email protected] etc]# service mysqld restart

2.3 slave連線master

2.3.1 檢視master的二進位制日誌檔名稱及Position

在master資料庫上:

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |     2025 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

2.3.2 連線master

在slave資料庫上:

mysql> change master to master_host='192.168.1.11',master_user='root',master_password='123456',master_log_file='mysql-bin.000002', master_log_pos=2025;

2.3.3 啟動slave複製

在slave資料庫上:

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

2.3.4 主從同步狀態檢查

在slave資料庫上:

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.11
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 2025
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-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: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 2025
              Relay_Log_Space: 531
              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: 45
                  Master_UUID: 240f86da-e69c-11e8-92e1-6c92bf135230
             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.01 sec)

其中Slave_IO_Running 與 Slave_SQL_Running 的值都必須為YES,才表明狀態正常。

3 驗證主從複製效果

3.1 在master資料庫上

mysql> create database test;

Query OK, 1 row affected (0.00 sec)

mysql> use test;

Database changed

mysql> create table t_user(id int NOT NULL AUTO_INCREMENT,name varchar(64), lastime datetime,PRIMARY KEY (`id`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Query OK, 0 rows affected (0.00 sec)

mysql> insert into t_user(id,name,lastime) values(1,'Moxiao',now());

Query OK, 1 row affected (0.01 sec)

3.2 在slave資料庫上

mysql> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

5 rows in set (0.00 sec)

mysql> use test;

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> select * from t_user;

+------+-------+----------------------------------+
| id   |  name |       lastime                    |
+------+-------+----------------------------------+
|    1 | Moxiao|  2018-11-13 15:22:39             |
+------+-------+----------------------------------+
1 row in set (0.00 sec)

主從複製成功!