在做mysql主从和互主配置时,如果一步一步操作,经常会调整半天,浪费时间。其实原理很简单,通过IO线程对bin-log进行相互同步,然后有个sql线程把bin-log执行一遍,为了防止相互打架
在做mysql主从和互主配置时,如果一步一步操作,经常会调整半天,浪费时间。其实原理很简单,通过IO线程对bin-log进行相互同步,然后有个sql线程把bin-log执行一遍,为了防止相互打架,设置一个offset,于是就想怎么样提高效率,最短的时间把环境搭建好,好在只要想就是有办法的,写个shell脚本,执行完搞定,脚本里对mysql本身并未做过多优化,只保证保主从、互主的成功,分享出来,在研究这个东西的可以了解一下。
3个脚本,第1个在第1台服务器上执行,第2个在第2台服务器上执行,还有一个changemaster脚本,必要时很好用的,不用1条1条的敲了,不废话了,上脚本,里面像IP和密码这些东西还是要手动改一下的,master-host等也是得手动改一下,研究这个的一看就明白了。
第1台服务器:
#!/bin/sh #writer:gaolixu binlog_path=/binlog [ -S /var/lib/mysql/mysql.sock ] && { echo 系统中已安装mysql!!!请卸载...;exit;} yum -y install mysql-server mysql service mysqld start sleep 1 mkdir -p $binlog_path chown -R mysql:mysql $binlog_path mysqladmin -u root password '123' mysql -u root -p123 -e "grant replication slave on *.* to slave@'%' identified by '123'" echo "[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-bin=$binlog_path/master log-bin-index=$binlog_path/master server-id=1 #expire-logs-days=100 #replicate-do-db=test #master-host=192.168.3.254 #master-user=slave #master-password=123 binlog-ignore-db=mysql binlog-ignore-db=information_schema auto-increment-increment=2 auto-increment-offset=1 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid" > /etc/my.cnf service mysqld restart echo "root用户密码为123,slave用户密码为123,请及时更改!!!" mysql -u root -p123 -e "show master status\G"
第2台服务器:
#!/bin/sh #writer:gaolixu binlog_path=/binlog [ -S /var/lib/mysql/mysql.sock ] && { echo 系统中已安装mysql!!!请卸载...;exit;} yum -y install mysql-server mysql service mysqld start sleep 1 mkdir -p $binlog_path chown -R mysql:mysql $binlog_path mysqladmin -u root password '123' mysql -u root -p123 -e "grant replication slave on *.* to slave@'%' identified by '123'" echo "[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-bin=$binlog_path/master log-bin-index=$binlog_path/master server-id=2 #expire-logs-days=100 #replicate-do-db=test #master-host=192.168.3.254 #master-user=slave #master-password=123 binlog-ignore-db=mysql binlog-ignore-db=information_schema auto-increment-increment=2 auto-increment-offset=2 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid" > /etc/my.cnf service mysqld restart echo "root用户密码为123,slave用户密码为123,请及时更改!!!" mysql -u root -p123 -e "show master status\G"
changemaster脚本:
#!/bin/sh #writer:gaolixu echo "默认使用帐号slave,密码123" read -p "请输入master_host(ip):" h read -p "请输入master_log_file:" lf read -p "请输入master_log_pos:" lp h=\'$h\' lf=\'$lf\' mysql -u root -p123 <<! change master to master_host=$h,master_user='slave',master_password='123',master_log_file=$lf,master_log_pos=$lp; start slave; exit ! mysql -u root -p123 -e "show slave status\G"|grep Slave
“运维网咖社”原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://www.net-add.com
©本站文章(技术文章和tank手记)均为社长"矢量比特"工作.实践.学习中的心得原创或手记,请勿转载!
欢迎扫描关注微信公众号【运维网咖社】
社长"矢量比特",曾就职中软、新浪,现任职小米,致力于DevOps运维体系的探索和运维技术的研究实践. |