在做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
本文出自 “奔跑的linux” 博客,请务必保留此出处http://benpaozhe.blog.51cto.com/10239098/1747615
原文地址:http://benpaozhe.blog.51cto.com/10239098/1747615