1.环境:
基于虚拟机,所以直接克隆一台同样的虚拟机操作
从库:
[root@slave ~]# ifconfig eth0|awk -F '[ :]+''NR==2{print $4}';ifconfig eth1|awk -F '[ :]+' 'NR==2{print $4}'
192.168.56.42
172.16.1.42
/etc/init.d/iptables stop
或者:
Vim /etc/sysconfig/iptable
-A INPUT -m state --state NEW -m tcp -p tcp --dport3306 -j ACCEPT
主库:要记录bin_log,
[root@master ~]# cat -n /etc/my.cnf|sed -n '41p'
41 log_bin = /home/mysql/data/binlog/mysql-bin
保证server_id数值和主库的不一样。
[root@slave ~]# cat -n /etc/my.cnf|sed -n '11p'
11 server_id = 101
注意:服务器的主机名不要随意变动,否则导致MySQL服务重新启动会报错,必须杀当前的MySQL掉进程,然后再restart启动MySQL服务
mysql 5.6的复制引入了uuid的概念,各个复制结构中的server_uuid得保证不一样,但是查看到直接copy data文件夹后server_uuid是相同的,show variables like ‘%server_uuid%’;
同时要保证主从数据库的auto.cnf中数值不一样,同时必须保证两台主从的机器的防火墙都必须是关闭的。
修改两台服务器上的文件/data/mysql/data/auto.cnf然后重启数据库服务就ok了
负责会报错:1593
Last_IO_Error: Fatal error: The slave I/O threadstops because master and slave have equal MySQL server UUIDs; these UUIDs mustbe different for replication to work.
以及报错:2003原因是防火墙没有关闭。Change master 时密码输入错误导致。
Last_IO_Error: error connecting to master 'rep@172.16.1.41:3306' -retry-time: 60 retries: 1
提示:必须保证两台的服务器的时间是一致的,要适时更新时间并且写入定时任务
Crontab -l
###time sync###by wujianwei at 2016-5-19
*/5 * * * * /usr/sbin/ntpdatentp1.aliyun.com &>/dev/null
搭建一主多从数据库时报错:
在从库操操作:
mysql> start slave;
ERROR 1201 (HY000): Could not initialize masterinfo structure; more error messages can be found in the MySQL error log
mysql> show slave status\G
Last_Errno:1872
Last_Error: Slave failed to initialize relay log info structure from therepository
在一台主机上增加一个slave,启动的时候报
[ERROR] Slave SQL: Slave failed toinitialize relay log info structure from the repository, Error_code: 1872
原因:检查my.cnf,原来没指定relay_log,mysql默认产生的relay_log名被该server上的另一个mysql slave占用了。
原因是:从库的my.cnf配置文件中没有加入参数
relay_log=/data/mysql/data/relay-log/mysql-relay-bin
1.stop slave;
2. reset slave;
mysql>reset slave
3.mysql>change master to
4.start slave
2.主从环境搭建配置步奏:
1.master主库操作:
/etc/init.d/iptables stop
或者:
vim /etc/sysconfig/iptable
-A INPUT -m state --state NEW -m tcp -p tcp --dport3306 -j ACCEPT
echo "log_bin =/home/mysql/data/binlog/mysql-bin" /etc/my.cnf
sed -i 's#server_id = 100#server_id = 1#g'/etc/my.cnf
mkdir /home/mysql/data/binlog/ -p
chown mysql.mysql /home/mysql/data/binlog
mysql -uroot -p123456 -e "grant replicationslave on *.* to rep@'172.16.1.%' identified by '111111'; flush privileges "
mysqldump -uroot -p123456 -B -A -F -R -x--master-data=1 --events|gzip>/backup/rep_$(date +%F).sql.gz
mysqldump -uroot -p123456 -B -A -F -R -x--master-data=1 --events|gzip>/backup/rep_$(date +%F).sql.gz
mysql-uroot -p123456 -e "show master status;"
scp -rp -P22 /backup/rep_2016-08-18.sql.gz root@172.16.1.42:/backup/
2从库操作:mysql-5.6s1
vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp--dport 3306 -j ACCEPT
sed -i 's#server_id = 100#server_id = 2#g' /etc/my.cnf
gzip -d /backup/rep_2016-08-18.sql.gz
mysql -uroot -p123456 </backup/rep_2016-08-18.sql
mysql -uroot -p123456 -e "CHANGE MASTERTO MASTER_HOST='172.16.1.41',MASTER_PORT=3306,MASTER_USER='rep',MASTER_PASSWORD='111111';startslave;show slave status\G" |grep -i "yes"
3.一组多从数据库配置mysql-5.6s2
vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp--dport 3306 -j ACCEPT
sed -i 's#server_id = 100#server_id = 3#g'/etc/my.cnf
vim /data/mysql/data/auto.cnf
/etc/init.d/mysqld restart
gzip -d rep_2016-08-18.sql.gz
mysql -uroot -p123456 </backup/rep_2016-08-18.sql
mysql -uroot -p123456 -e "showdatabases;"
mysql -uroot -p123456 -e "CHANGE MASTERTO MASTER_HOST='172.16.1.41',MASTER_PORT=3306,MASTER_USER='rep',MASTER_PASSWORD='111111';startslave;show slave status\G" |grep -i "yes"
1.演示:
Master:
mysql -uroot -p123456 -e "create databasewjw05"
mysql> use wjw01;
mysql> CREATE table `t1` (`id` bigint(12) NOTNULL auto_increment,`name` varchar(12) NOT NULL,PRIMARY KEY (`id`) );
mysql> INSERT INTO t1(name) values('oldboy');
slave:
mysql -uroot -p123456 -e "showdatabases"|grep wjw05
mysql -uroot -p123456 -e "showdatabases"|grep wjw05
mysql -uroot -p123456 -e "desc wjw01.t1"
mysql -uroot -p123456 -e "select * fromwjw01.t1"