主从复制

主从复制前提(搭建)

两台以上数据库实例,版本一致

[root@db01 ~]# systemctl start mysqld3307

[root@db01 ~]# systemctl start mysqld3308

[root@db01 ~]# systemctl start mysqld3309

区分不同角色

[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "select @@server_id"

+-------------+

| @@server_id |

+-------------+

|          7 |

+-------------+

[root@db01 ~]# mysql -S /data/3308/mysql.sock -e "select @@server_id"

+-------------+

| @@server_id |

+-------------+

|          8 |

+-------------+

[root@db01 ~]# mysql -S /data/3309/mysql.sock -e "select @@server_id"

+-------------+

| @@server_id |

+-------------+

|          9 |

+-------------+

主库开起二进制日志

[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "select @@log_bin"

+-----------+

| @@log_bin |

+-----------+

|        1 |

+-----------+

[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "select @@log_bin_basename"

+----------------------+

| @@log_bin_basename  |

+----------------------+

| /data/3307/mysql-bin |

+----------------------+

主库创建专用复制用户

[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "grant replication slave on *.* to repl@'10.0.0.%' identified by '123'"

备份主库数据,恢复从库

[root@db01 data]# mysqldump -S /data/3307/mysql.sock -A --master-data=2 >/tmp/full.sql

[root@db01 data]# mysql -S /data/3308/mysql.sock

[root@db01 data]# mysql -S /data/3309/mysql.sock

开启从库复制功能(连接信息,复制起点)

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=674;

CHANGE MASTER TO

  MASTER_HOST='10.0.0.51',

  MASTER_USER='repl',

  MASTER_PASSWORD='123',

  MASTER_PORT=3307,

  MASTER_LOG_FILE='mysql-bin.000004',

  MASTER_LOG_POS=674,

  MASTER_CONNECT_RETRY=10;

start slave;

检查主从状态

[root@db01 ~]# mysql -S /data/3309/mysql.sock  -e "show slave status \G"|grep "Running"

[root@db01 ~]# mysql -S /data/3308/mysql.sock  -e "show slave status \G"|grep "Running"

你可能感兴趣的:(主从复制)