配置基于 GTID 的主从复制
1.修改 master 和 slave 的配置文件
server-id=113
gtid_mode=on
enforce-gtid-consistency=on
replicate-do-db=gateway_target # 如果只需同步部分表,就在 slave 上配置这两个额外项
replicate-do-table=gateway_target.t_target_snapshot
2.导出 master 的库和表结构到 slave,先停止 master
mysqldump -d --master-data -uroot -pAdmin123 -B gateway_target > 115.sql
mysqladmin -uroot -p password -S /tmp/mysql3307.sock < 115.sql
3.启动 slave 主从复制
mysql -uroot -proot -S /tmp/mysql3307.sock mysql
change master to master_host=‘192.168.50.115‘,master_port=3306, master_user=‘root‘, master_password=‘Admin123‘, master_auto_position=1;
start slave;
show slave status;
4.配置多源复制*
master-info-repository = table
relay-log-info-repository = table
START SLAVE FOR CHANNEL ‘master1‘;
MySQL多实例
1.创建实例目录
mkdir -p /var/lib/mysql{3307,3308}/data && chown -R mysql:mysql /var/lib/mysql{3307,3308}/data
2.初始化实例的数据库
mysqld --no-defaults --initialize-insecure --basedir=/usr --datadir=/var/lib/mysql3307/data --user=mysql --explicit_defaults_for_timestamp
遇到问题: mysqld: Can‘t create directory ‘/var/lib/mysql3307/data/‘ (OS errno 17 - File exists)
原因:ubuntu 有一个 AppArmor, 类似于 SELinux, 是一个系统安全应用程序, 定义了个别应用程序可以访问的系统资源, 如果不设置, 即使改了用户组权限也没用.
vi /etc/apparmor.d/usr.sbin.mysqld
/var/lib/mysql3307/ r,
/var/lib/mysql3308/ r,
/var/lib/mysql3307/** rwk,
/var/lib/mysql3308/** rwk,
systemctl restart apparmor
3.配置文件
[mysqld_multi]
mysqld=/usr/bin/mysqld_safe
mysqladmin=/usr/bin/mysqladmin
user=root
pass=root
[mysqld3307]
port=3307
socket=/tmp/mysql3307.sock
datadir=/var/lib/mysql3307/data
skip-external-locking
log-error=/var/lib/mysql3307/data/mysql3307_error.log
pid-file=/var/lib/mysql3307/data/mysql3307.pid
server-id=3307
4.设置实例密码
mysqladmin -uroot -p password -S /tmp/mysql3307.sock
mysqladmin -uroot -p password -S /tmp/mysql3308.sock
5.运行多实例,多实例不会影响默认实例的运行
mysqld_multi start | stop | report
6.设置远程登录
mysql -uroot -proot -S /tmp/mysql3307.sock mysql
update user set host=‘%‘ where user=‘root‘;
grant all privileges on *.* to ‘root‘@‘%‘;
flush privileges;
sudo firewall-cmd --zone=public --add-port=3307/tcp --permanent && sudo firewall-cmd reload
原文地址:https://www.cnblogs.com/wanglongbiao/p/11507445.html