mysql主备同步:
dd1主:yum install mysql-server -y
vim /etc/my.cnf
server-id=1
log-bin=mysql-bin 启动二进制日志系统
binlog-do-db=test 需要同步的数据库
binlog-ignore-db=mysql 禁止同步的数据库
/etc/init.d/mysqld start
show master status
mysql> GRANT REPLICATION SLAVE ON *.* TO dd@‘172.25.254.%‘IDENTIFIED BY ‘redhat‘ 创建同步用户并给予权限
mysql>Flush privileges
dd2备:yum install mysql-server -y
测试 看能不能远程登陆访问:mysql -h 172.25.254.11dd -p redhat
vim /etc/my.cnf
server-id=2
read-only
/etc/init.d/mysqld start
在同步之前确保 master 与 slave 上的数据一致性
mysql> change master tomaster_host=‘172.25.254.11‘, master_user=‘dd‘,
master_password=‘redhat‘,master_log_file=‘mysql-bin.000003‘, master_log_pos=106;
slave start
show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes 如果都是 yes,表示从库的 I/O,Slave_SQL 线程都正确开启.表明数据库正在同步
测试:
mysql数据库的主备(gtid方式):
gtid是一个基于原始mysql服务器生成的一个已经被成功执行的全局事务id,它由服务器id及事物id组合而成,这个全局事物id不仅仅在原始服务器上唯一,在所有存在主从关系的mysql服务器也是唯一的
安装软件包:(两台数据库同时做)
tar xfmysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
yum install -ymysql-community-client-5.7.17-1.el6.x86_64.rpmmysql-community-common-5.7.17-1.el6.x86_64.rpmmysql-community-libs-5.7.17-1.el6.x86_64.rpmmysql-community-libs-compat-5.7.17-1.el6.x86_64.rpmmysql-community-server-5.7.17-1.el6.x86_64.rpm
yum install * -y
注意:myslq密码在启动时初始化生成
mysql -p
alter userroot@localhost identified by ‘Westos+007‘ (注意新的密码格式大小写特殊字符)
dd1(主)
vim /etc/my.cnf
gtid-mode=ON
enforce-gtid-consistency
log-bin=mysql-bin
binlog-do-db=test
log-slave-updates slave更新是否记录日志
dd2(备)
vim /etc/my.cnf
gtid-mode=ON
enforce-gtid-consistency
#log-bin=mysql-bin
#binlog-do-db=test
log-slave-updates
/etc/init.d/mysqld restart (两台同时)
dd1:mysql -p
grant replicationslave on *.* to dd@‘172.25.42.%‘ identified by ‘Westos+007‘
dd2:mysql -p
mysql -u dd -h172.25.42.10 -p (测试)
change master tomaster_host=‘172.25.42.10‘,master_user=‘dd‘,master_password=‘Westos+007‘,master_auto_position=1;
(注意和前一个主备设置的区别就不用手动添加要同步的日志自动定位因为在生产环境中备份服务器很可能不止一台这样每一分里面的同步日志是不同的无法定位所以gtid可以不用指定binlog和主mysql的读取指针定位)
start slave
show slave status\G
同步测试:
主备mysql半同步传输(io)(一个binlog 返回一个ACK 主mysql上面的数据才会提交)异步传输(不需要返回ACK就提交)
主:show plugins
install pluginrpl_semi_sync_master soname ‘semisync_master.so‘
set globalrpl_semi_sync_master_enabled=1;
show global statuslike ‘%semi%‘;
show globalvariables like ‘%rpl_semi%‘;
从:show plugins
install pluginrpl_semi_sync_master soname ‘semisync_master.so‘;
install pluginrpl_semi_sync_slave soname ‘semisync_slave.so‘;
show global statuslike ‘%semi%‘;
show globalvariables like ‘%rpl_semi%‘;
测试半同步,异步的切换:
stop slaveio_thread
insert intotest.usertb values (‘dd331‘,‘41113331‘);
发现提交等待10s 备机无相应(ACK),则转为异步
多线程并行复制:(针对 sql 只在slave上面做)
备:vim /etc/my.cnf
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
/etc/init.d/mysqldrestart
mysql -p
show processlist (查看线程)
mysql 读写分离(mysql-proxy)
主机:dd1.example.com 172.25.254.11
备机:dd2.example.com 172.25.254.12
管理机:dd3.example.com 172.25.254.13
dd3:
tar zxfmysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
mvmysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/
cd /usr/local/
ln -smysql-proxy-0.8.5-linux-el6-x86-64bit/ mysql-proxy
cd /usr/local/mysql-proxy/bin
vim mysql-proxy.conf
[mysql-proxy]
user=root
#admin-username=lin3615
#admin-password=123456
#proxy-address=192.168.179.142:4040
proxy-read-only-backend-addresses=172.25.0.4
proxy-backend-addresses=172.25.0.3
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
#admin-lua-script=/usr/local/mysql-proxy/lua/admin-sql.lua
log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log
log-level=info
daemon=true
keepalive=true
cd /usr/local/mysql-proxy/share/dos
vim rw-splitting.lua
cd/usr/local/mysql-proxy
mkdir logs
cd/usr/local/mysql-proxy/bin
./mysql-proxy --defaults-file=mysql-proxy.conf
chmod 600mysql-proxy.conf
dd1:
grant all on *.* to proxy@‘172.25.254.%‘identified by‘Westos+007‘;
测试:
通过代理访问数据库实现读写分离:
原文地址:http://12117148.blog.51cto.com/12107148/1911307