mysql5.7开启主从同步

在同一台服务器的2个实例(3355,3366)进行测试

1,设置主从裤

开通主库master(3355)的bin-log:

1)停止数据裤

2)修改3355数据库的my.cnf

增加bin-log字段及设置master的serverid

vim my.cnf

log-bin=/mysqldata/3355/mysql-bin

server-id=1

3)修改从库3366的server-id使其与主库区分

vim my.cnf

server-id=1

4)登录主库mysql查看是否设置成功

mysql> show variables like 'log_bin' \G

*************************** 1. row ***************************

Variable_name: log_bin

        Value: ON

1 row in set (0.00 sec)

 

2,在主库master上创建从库slave同步权限的用户ddd

 

CREATE USER 'ddd'@'192.168.0.%' IDENDIFIED BY '123456';

grant replication slave on *.* TO 'ddd'@'192.168.0.%';

 

3,锁表(需要无业务进行时)

mysql> flush table with read lock;

4,查看master状态

mysql> show master status\G

*************************** 1. row ***************************

             File: mysql-bin.000001

         Position: 2400

     Binlog_Do_DB: 

 Binlog_Ignore_DB: 

Executed_Gtid_Set: 

 

1 row in set (0.00 sec)

 

mysql> show master logs\G

*************************** 1. row ***************************

 Log_name: mysql-bin.000001

File_size: 2400

 

1 row in set (0.00 sec)

 

5,新开窗口,备份主库数据

mysqldump -uroot -p123456 -S /mysqldata/3355/mysql.sock -NBAE --master-data=1 >/opt/rep.sql

  

6,将主库的只读锁去掉

mysql> unlock tables;

7,将主库数据导入从库slave

mysql -uroot -p123456 -S /mysqldata/3366/mysql.sock < /opt/rep.sql

 

8,在从库slave上设置MASTER文件

mysql -uroot -p123456 -S /mysqldata/3366/mysql.sock 

mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.68',MASTER_PORT=3355,MASTER_USER='ddd',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=2400;

 

9,在从库启动salve

 mysql> start slave; 

10,查看从库同步状态

mysql> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.0.68

                  Master_User: ddd

                  Master_Port: 3355

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000001

          Read_Master_Log_Pos: 2400

               Relay_Log_File: localhost-relay-bin.000002

                Relay_Log_Pos: 1475

        Relay_Master_Log_File: mysql-bin.000001

             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: 2400

              Relay_Log_Space: 1686

              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: 5bebbfab-8446-11e6-b70e-000c29d331cb

             Master_Info_File: /mysqldata/3366/data/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.00 sec)

 

 

 

你可能感兴趣的:(mysql5.7开启主从同步)