systemctl stop mysqld.service
停止服务vim /etc/my.cnf
设置基于gtid的主从复制
log-bin=mysql-bin ##基于二进制日志位置的主从复制
server-id=1
gtid_mode=ON ##打开gtid
enforce-gtid-consistency=ON
rm -fr /var/lib/mysql/*
清空数据
systemctl start mysqld
重启服务
2.初始化MySQL,创建用户并授权
cat /var/log/mysqld.log |grep password
查看临时密码
mysql -uroot -p
mysql> alter user root@localhost identified by 'My12345.';##更改密码
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.43.%' IDENTIFIED BY 'My12345.'; ##创建用户并授权
在server2中
1.设置基于gtid的主从复制
systemctl stop mysqld.service
停止服务
vim /etc/my.cnf
设置基于gtid的主从复制
log-bin=mysql-bin ##基于二进制日志位置的主从复制
server-id=1
gtid_mode=ON ##打开gtid
enforce-gtid-consistency=ON
rm -fr /var/lib/mysql/*
清空数据
systemctl start mysqld
重启服务
2.初始化MySQL,更改复制方式为gtid主从复制
cat /var/log/mysqld.log |grep password
查看临时密码
mysql -uroot -p
mysql> alter user root@localhost identified by 'My12345.';
mysql> CHANGE MASTER TO
-> MASTER_HOST = '192.168.43.10',
-> MASTER_USER = 'repl',
-> MASTER_PASSWORD = 'My12345.',
-> MASTER_AUTO_POSITION = 1;
mysql> START slave;##开启复制
SHOW slave STATUS\G ##查看状态
在server3中设置读写分离
1.安装mysql-proxy
tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local/
解压包
ln -s /usr/local/mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-proxy
做软连接
2.配置mysql-proxy
mkdir /usr/local/mysql-proxy/bin/conf
建立配置目录
vim /usr/local/mysql-proxy/conf/mysql-proxy.conf
配置
[mysql-proxy]
proxy-address=0.0.0.0:3306
proxy-backend-addresses=192.168.43.10:3306
proxy-read-only-backend-addresses=192.168.43.20:3306
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
pid-file=/usr/local/mysql-proxy/log/mysql-proxy.pid
log-file=/usr/local/mysql-proxy/log/mysql-proxy.log
plugins=proxy
log-level=debug
keepalive=true
daemon=true
chmod 660 /usr/local/mysql-proxy/bin/conf/mysql-proxy.conf
修改配置文件权限
mkdir /usr/local/mysql-proxy/log
建立日志目录
2.设置读写分离启动机制
vim /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1,
max_idle_connections = 2,
is_debug = false
}
end
3.启动mysql-proxy
/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
启动
注意:不能启动mysql,因为mysql-proxy使用3306端口
cat /usr/local/mysql-proxy/log/mysql-proxy.log
查看日志
测试:
在server1中
mysql> grant insert,update,select on *.* to repl@'%' identified by 'My12345.';
mysql> flush privileges;
mysql> USE test;
mysql> CREATE TABLE testtable(
-> username varchar(10) not null,
-> password varchar(15) not null);
mysql> desc testtable;
在用户端:
(注意:用户端安的是MariaDB-server)
mysql -h 192.168.43.30 -ured -pMy12345.