MySQL(主从复制、PXC集群、MyCat、HAProxy)

搭建主库

#创建目录 
mkdir /data/mysql/master01
cd /data/mysql/master01 
mkdir conf data 
chmod 777 * -R
#创建配置文件 
cd /data/mysql/master01/conf 
vim my.cnf
#输入如下内容 
[mysqld] 
log-bin=mysql-bin  #开启二进制日志 
server-id=1  #服务id,不可重复
#创建容器 
docker create --name percona-master01 -v /data/mysql/master01/data:/var/lib/mysql -v /data/mysql/master01/conf:/etc/my.cnf.d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root percona:5.7.23
#启动 
docker start percona-master01 && docker logs -f percona-master01
#创建同步账户以及授权 
create user 'itcast'@'%' identified by 'itcast'; grant replication slave on *.* to 'itcast'@'%'; flush privileges;
#出现 (我没遇到这错误)
[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and错误解 决方案,在my.cnf配置文件中设置 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO ,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
#查看master状态 
show master status;
#查看二进制日志相关的配置项 
show global variables like 'binlog%';
#查看server相关的配置项 server_id
show global variables like 'server%';

搭建从库

#创建目录
mkdir /data/mysql/slave01
cd /data/mysql/slave01
mkdir conf data
chmod 777 * -R
#创建配置文件
cd /data/mysql/slave01/conf
vim my.cnf
#输入如下内容
[mysqld]
server-id=2 #服务id,不可重复
sql_mode=‘STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO ,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION’
#创建容器
docker create --name percona-slave01 -v /data/mysql/slave01/data:/var/lib/mysql -v /data/mysql/slave01/conf:/etc/my.cnf.d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root percona:5.7.23
#启动
docker start percona-slave01 && docker logs -f percona-slave01
#设置master相关信息 (show master status;得到 master_log_file=‘mysql-bin.000002’, master_log_pos=648;)
CHANGE MASTER TO master_host=‘106.14.217.165’, master_user=‘itcast’, master_password=‘itcast’, master_port=3306, master_log_file=‘mysql-bin.000002’, master_log_pos=648;
#启动同步
start slave;
#查看master状态
show slave status;
#修改主库的配置 cd …/…/master01/conf/ # vim my.cnf

binlog_format=MIXED
#重启
docker restart percona-master01 && docker logs -f percona-master01
#查看二进制日志相关的配置项
show global variables like ‘binlog%’;

你可能感兴趣的:(mysql)