1. 程式人生 > 實用技巧 >mysql實現主從複製

mysql實現主從複製

首先 用兩個伺服器 我用的是mysql5.7

116.62.234.228(主)

47.95.228.178(從)

一.主伺服器上的配置

1.建立一個數據庫

mysql>create database test;

2.修改配置檔案

vim /etc/my.cnf

3.在[mysqld]下面新增

server-id=1
log-bin=mysql-bin       ## 開啟二進位制日誌
binlog-do-db=db1       ## 要主從的資料庫
binlog-ignore-db=mysql  ## 不需要主從的資料庫

4.之後重啟

service mysql restart

5.之後

mysql>create user buxin@47.95.227.178 identified by 'buxin123';#建立用於主從的使用者

mysql>grant REPLICATION SLAVE on *.* to buxin@'47.95.227.178'; #授許可權

mysql>show master status; #查詢用於同步的二進位制檔案和位置

6.then

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

 二.

1.

mysql>create database test;#建立相同的資料庫

2.修改mysql的配置檔案

vim /etc/my.cnf

3.在[mysqld]下面新增

server-id=2
log-bin=mysql-bin                # 開啟二進位制日誌
replicate-do-db=test              # 被主從的資料庫

4.重啟

service mysql restart
mysql>change master to master_host='116.62.234.228',master_user='buxin
',master_password='buxin123', master_log_file='mysql-bin.000022',master_log_pos=679;#配置和上面一樣的
mysql>start slave;
mysql>show slave status \G;

之後

  Slave_IO_State: Waiting for master to send event
                  Master_Host: 116.62.234.228
                  Master_User: buxin
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000022
          Read_Master_Log_Pos: 679
               Relay_Log_File: iz2ze7y54l8o2xkgmjrkd3z-relay-bin.000002
                Relay_Log_Pos: 845
        Relay_Master_Log_File: mysql-bin.000022
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: centuryt,test
          Replicate_Ignore_DB: 

之後就成功了 !!! 轉載於

https://blog.csdn.net/Jack_young_/article/details/84957583?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param