准备环境:
1.至少三台虚拟机
2.MySQL5.6 (支持事务一致性)
[root@ tom42 ~]# echo "*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" >>/var/spool/cron/root
[root@ tom42 ~]# crontab -e //查看都有那些定时任务,也可以添加
2.1 更改主机名称 永久更改 如果连着xshell 断开重新连接就生效了
[root@ tom42 ~]# hostnamectl set-hostname tom42
2.2 添加域名解析
[root@ tom42 ~]# vim /etc/hosts tom42 10.0.0.42 tom43 10.0.0.43 tom44 10.0.0.44
[root@ tom42 ~]# systemctl stop firewalld [root@ tom42 ~]# systemctl disable firewalld [root@ tom42 ~]# setenforce 0 setenforce: SELinux is disabled
[root@ tom42 ~]# vim ssh.sh #!/bin/bash yum -y install sshpass &> /dev/null read -p "请输入服务器密码:" passwd UserName=root IP="10.0.0." #创建密钥 ssh-keygen -t dsa -f ~/.ssh/id_dsa -P "" &>/dev/null #分发公钥 for i in 42 43 44 do sshpass -p "$passwd" ssh-copy-id -i ~/.ssh/id_dsa.pub -p 22 -o StrictHostKeyChecking=no $UserName@$IP$i &>/dev/null done
1.1 wget +install
[root@ tom42 ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm [root@ tom42 ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm [root@ tom42 ~]# yum -y install mysql-server
1.2 重启 mysql
[root@ tom42 ~]# systemctl restart mysql
2.1 编辑 配置文件
[root@ tom42 ~]# vim /etc/my.cnf server-id=1 log-bin=mysql-bin
2.2 重启动mysql
[root@ tom42 ~]# systemctl restart mysql
2.3 给MySQL数据库授权
[root@ tom42 ~]# mysql -e "grant all on *.* to ‘tom‘@‘10.0.0.%‘ identified by‘123‘;" //授权 10.0.0.0/24 [root@ tom42 ~]# mysql -e "flush privileges;" [root@ tom42 ~]# log_file=`mysql -e "show master status;"|grep mysql-bin|awk ‘{print $1}‘` [root@ tom42 ~]# log_pos=`mysql -e "show master status;"|grep mysql-bin|awk ‘{print $2}‘` [root@ tom42 ~]# echo $log_file mysql-bin.000001 [root@ tom42 ~]# echo $log_pos 400
3.1 编辑 配置文件 注:不同点 (server-id=2 server-id=3 )
[root@ tom43 ~]# vim /etc/my.cnf server-id=2 relay-log=mysql-relay
3.2 重启动mysql
[root@ tom43 ~]# systemctl restart mysql
3.3 停止slave
[root@ tom43 ~]# mysql -e "stop slave;" [root@ tom43 ~]# mysql -e "reset slave;" [root@ tom43 ~]# mysql -e "change master to master_host=‘10.0.0.42‘,master_user=‘tom‘,master_password=‘123‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=400;\r"
[root@ tom43 ~]# mysql -e "start slave;"
3.4 查看状态 (两个yes为成功)
mysql> show slave statu s \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.42 Master_User: tom Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 400 Relay_Log_File: mysql-relay.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB:
三.
原文:https://www.cnblogs.com/gaiting/p/12304628.html