Mysql实现主从复制(环境搭建)

Posted 伍妖捌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql实现主从复制(环境搭建)相关的知识,希望对你有一定的参考价值。

下载mysql镜像

docker pull mysql:5.7

创建Mysql实例并启动

通过下面命令创建两个mysql,master、slave

docker run -p 3310:3306 \\
--name mysql --restart always \\
-v /usr/local/mysql/log:/var/log/mysql \\
-v /usr/local/mysql/data:/var/lib/mysql \\
-v /usr/local/mysql/conf:/etc/mysql \\
-e MYSQL_ROOT_PASSWORD=MASTER2021@mysql.com \\
-d mysql:5.7

Master添加配置

vi /usr/local/mysql/conf/my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection=utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
server_id=10
log-bin=mysql-bin
read-only=0
binlog-do-db=test
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

授权用户来同步数据

1.进入mysql容器

docker exec -it mysql bash

2.进入mysql内部(mysql -u root -p)
1)、添加用来同步的用户

create user 'slave'@'%' identified by 'SLAVE2021@mysql.com';
grant replication slave on *.* to 'slave'@'%';

2)、添加远程访问

create user 'test'@'%' identified by 'test@test.com';
grant all privileges on *.* to 'test'@'%';

3.查看master状态

show master status;

slave同步数据

change master to master_host='192.168.111.200',master_user='slave',master_password='SLAVE2021@mysql.com',master_log_file='mysql-bin.000002',master_log_pos=0,master_port=3406;
start slave;
show slave status;

以上是关于Mysql实现主从复制(环境搭建)的主要内容,如果未能解决你的问题,请参考以下文章

Mysql实现主从复制(环境搭建)

web服务文件更新自动同步数据库主从复制shell脚本实现网站代码备份和mysql备份

MySQL主从复制与读写分离

MySQL主从复制的简单搭建

MySQL主从复制的简单搭建

MySQL主从复制的简单搭建