第二台从服务器跟第一台从服务操作一样
[root@localhost ~]# hostnamectl set-hostname 3 [root@localhost ~]# su [root@3 ~]# yum -y install ntpdate Loaded plugins: fastestmirror Determining fastest mirrors centos | 3.6 kB 00:00:00 Package ntpdate-4.2.6p5-28.el7.centos.x86_64 already installed and latest version Nothing to do [root@3 ~]# ntpdate 20.0.0.60 21 Oct 02:17:39 ntpdate[48587]: adjust time server 20.0.0.60 offset 0.006296 sec [root@3 ~]# crontab -e no crontab for root - using an empty one */2 * * * * /usr/sbin/ntpdate 20.0.0.60 >>/var/log/ntpdate.log crontab: installing new crontab [root@3 ~]# systemctl restart crond [root@3 ~]# systemctl enable crond和第一台从服务器基本一样的
[root@localhost ~]# vi /etc/my.cnf server-id = 33 relay-log = relay-log-bin relay-log-index = slave-relay-bin.index [root@localhost ~]# systemctl restart mysqld [root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. mysql> change master to master_host='20.0.0.60',master_user='myslave',master_password='As123',master_log_file='master-bin.0000001 Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 20.0.0.60 Master_User: myslave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 599 Relay_Log_File: relay-log-bin.000002 Relay_Log_Pos: 321 Relay_Master_Log_File: master-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes准备工作jdk和amoeba压缩包
[root@amoeba opt]# ll total 83024 -rw-r--r-- 1 root root 3161433 Oct 20 19:59 amoeba-mysql-binary-2.2.0.tar.gz -rwxr-xr-x 1 root root 81849212 Oct 20 20:00 jdk-6u14-linux-x64.bin ##这边我的jdk权限已经给了直接./执行了[root@amoeba conf]# vi dbServers.xml
[root@amoeba conf]# /usr/local/amoeba/bin/amoeba start & #开启amoeba服务并放到后台执行 [root@amoeba conf]# yum -y install mariadb* # 安装mysql客户端,用于做测试用 [root@amoeba conf]# systemctl start mariadb.service ## 开启数据库
这边有时候会有提示按下面的顺序做就行了 回车 (提示输入密码,我们没有密码直接回车) yes 然后输入两次密码 (输入新密码) n (删除里面匿名用户) n (禁止用户远程登录) n (删除测试数据库) y (重载里面的表)
[root@amoeba conf]# netstat -anpt |grep 8066 tcp6 0 0 :::8066 :::* LISTEN 33973/java ##端口已经开启,说明服务已经开了 [root@amoeba ~]# mysql -u amoeba -p -h 127.0.0.1 -P8066 ##因为是amoeba本机当测试机用的所以-h指向的是127.0.0.1然后去两台从服务器关闭主从复制
mysql> stop slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 20.0.0.60 Master_User: myslave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000003 Read_Master_Log_Pos: 1212 Relay_Log_File: relay-log-bin.000017 Relay_Log_Pos: 1379 Relay_Master_Log_File: master-bin.000003 Slave_IO_Running: No ##这两个关闭就行了 Slave_SQL_Running: No ##这两个关闭就行了 mysql> use ig Database changed mysql> show tables; +--------------+ | Tables_in_ig | +--------------+ | nb | +--------------+ 1 row in set (0.00 sec) mysql> insert into nb values (1); Query OK, 1 row affected (0.00 sec) mysql> select * from nb; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.00 sec) mysql> stop slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 20.0.0.60 Master_User: myslave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000003 Read_Master_Log_Pos: 1212 Relay_Log_File: relay-log-bin.000017 Relay_Log_Pos: 1379 Relay_Master_Log_File: master-bin.000003 Slave_IO_Running: No ##这两个关闭就行了 Slave_SQL_Running: No ##这两个关闭就行了 mysql> use ig Database changed mysql> show tables; +--------------+ | Tables_in_ig | +--------------+ | nb | +--------------+ 1 row in set (0.00 sec) mysql> insert into nb values (2); Query OK, 1 row affected (0.00 sec) mysql> select * from nb; +------+ | id | +------+ | 2 | +------+ 1 row in set (0.00 sec)最后去客户机测试
MySQL [ig]> select * from nb; +------+ | id | +------+ | 2 | +------+ 1 row in set (0.00 sec) MySQL [ig]> select * from nb; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.01 sec) MySQL [ig]> select * from nb; +------+ | id | +------+ | 2 | +------+ 1 row in set (0.01 sec) MySQL [ig]> select * from nb; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.01 sec)结论:发现每一次查询都会轮询在slave1和slave2上查询数据,如果开启主从复制,则数据都会相同 读写分离试验成功