1. 程式人生 > >centos7上mysql5.6版本主從復制

centos7上mysql5.6版本主從復制

ror 二進制日誌 path 服務器 mes running bsp 必須 -s

做主從復制實驗:

第一步:主服務器上操作

1、修改主服務器master:

[root@localhost ~]# vim /etc/my.cnf
server_id = 1  //[必須]服務器唯一ID,默認是1
log-bin=mysql-bin //[必須]啟用二進制日誌

2、重啟主數據庫

[root@localhost ~]# systemctl restart mysqld

3、在主服務器上建立帳戶並授權slave:

mysql> grant replication slave on *.* to ‘zhangsan‘@‘%‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)

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

4、登錄主服務器的mysql,查詢master的狀態

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

註:執行完此步驟後不要再操作主服務器MYSQL,防止主服務器狀態值變化

第二步:從服務器操作

1、修改從服務器slave:

[root@localhost ~]# vim /etc/my.cnf
 server_id = 2
log-bin=mysql-bin

2、重啟從數據庫

[root@localhost ~]# systemctl restart mysqld

3、配置從服務器Slave:

mysql> change master to master_host=‘192.168.35.131‘,master_user=‘zhangsan‘,master_password=‘123456‘, master_log_file=‘mysql-bin.000001‘,master_log_pos=401; 
Query OK, 0 rows affected, 2 warnings (0.02 sec)

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

4、啟動從服務器復制功能並檢查從服務器復制功能狀態

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

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.35.131  //主服務器地址
                  Master_User: zhangsan     //授權帳戶名,盡量避免使用root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 401   //#同步讀取二進制日誌的位置,大於等於Exec_Master_Log_Pos
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes    //此狀態必須YES
            Slave_SQL_Running: Yes    //此狀態必須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: 401
              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: 31f8646b-484e-11e8-b503-000c298f5b19
             Master_Info_File: /data/mysqldb/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

註:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,否則都是錯誤的狀態(如:其中一個NO均屬錯誤)。

第三步:檢測主從配置

1、在主數據庫中創建一個庫data,查看從庫中是否有

主庫中:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database data;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| data               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

2、從庫中查看:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| data               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

能夠看到從庫中是有data庫的,說明主從復制配置完成,但是要註意,從表裏不能做更新,刪除等操作,只能使用查詢,所以要慎重。

總結:完成以上操作過程,主從服務器配置完成。

centos7上mysql5.6版本主從復制