在MySQL Master上的配置NTP时间同步服务器
[root@localhost ~]# rpm -q ntp
ntp-4.2.6p5-28.el7.centos.x86_64
[root@localhost ~]# vim /etc/ntp.conf #添加两行
server 127.127.1.0
fudge 127.127.1.0 stratum 8
启动NTP服务
[root@localhost ~]# systemctl enable ntpd
[root@localhost ~]# systemctl start ntpd
在2个Slave节点上配置与Master进行时间同步
[root@localhost ~]# rpm -q ntpdate
ntpdate-4.2.6p5-28.el7.centos.x86_64
[root@localhost ~]# ntpdate 192.168.1.1
31 Jul 10:13:21 ntpdate[9548]: adjust time server 192.168.200.111 offset -0.045710 sec
所有机器上的操作
[root@localhost ~]# yum -y install mariadb mariadb-devel mariadb-server
配置MySQL Master服务器
1、在/etc/my.cnf中修改或者增加如下内容:
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysql-binlog
log-slave-updates=true
重启MySQL服务器
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# netstat -lnpt | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 10073/mysqld
2、创建Replication用户
[root@localhost ~]# mysql -uroot -p123456
MariaDB [(none)]> grant replication slave on *.* to 'myslave'@'192.168.1.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3、获得Master DB的相关信息
供slave连接使用,记录下File和Position的值。
[root@localhost ~]# mysql -uroot -p'123456'
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 104
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000001 | 516864 | | |
+---------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
4、备份Master原有数据
如果在生产环境中Master服务器已经运行一段时间,或者Master服务器上已经存在数据,为了保证所有数据的一致性,需要先将Master目前已有的数据全部导给Slave服务器。
备份的方法有很多,可以直接备份数据文件,也可以使用mysqldump工具。全新搭建的环境不存在数据备份问题。
[root@mysql-master ~]# mysqldump -uroot --all-databases > /root/alldbbackup.sql
[root@mysql-master ~]# scp /root/ alldbbackup.sql [email protected]:/root/
[root@mysql-master ~]# scp /root/ alldbbackup.sql [email protected]:/root/
5、在MySQL Slave上的配置
导入Master的备份脚本
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql -uroot -p < /root/alldbbackup.sql
从库连接主库进行测试,如果连接成功说明主库配置成功
[root@localhost ~]# mysql -u myslave -p123456 -h 192.168.200.1
6.修改MySQL配置文件
配置多个从服务器时依次设置server-id号
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=2
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index、
修改完后重启数据库
[root@localhost ~]# systemctl restart mariadb
7.在Slave服务器授权,启动从库,进行主从库数据同步
参数说明:
CHANGE MASTER TO
MASTER_HOST='master_host_name', #主服务器的IP地址
MASTER_USER='replication_user_name', #主服务器授权的用户
MASTER_PASSWORD='replication_password', #主服务器授权的密码
MASTER_LOG_FILE='recorded_log_file_name', #主服务器二进制日志的文件名
MASTER_LOG_POS=recorded_log_position; #日志文件的开始位置
[root@localhost ~]# mysql -uroot -p123456
MariaDB [(none)]> stop slave;
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.200.1',MASTER_USER='myslave',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-binlog.000001',MASTER_LOG_POS=516864;
MariaDB [(none)]> start slave;
MariaDB [test1]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000001
Read_Master_Log_Pos: 516864
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 1894
Relay_Master_Log_File: mysql-binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 516864
Relay_Log_Space: 2186
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.02 sec)
MariaDB [test1]>
8.测试复制是否成功
在Master服务器上创建一个数据库或者表,到Slave服务器上查看,如果配置成功就可以成功同步
因为Amoeba是基于jdk1.5版本开发的,所以官方推荐使用1.5或者1.6版本,高版本不建议使用。
安装并配置Amoeba
[root@localhost ~]# mkdir /usr/local/amoeba
拉进来一个tar安装包
[root@localhost ~]# '//tmp/VMwareDnD/PBHaKE/amoeba-mysql-binary-2.2.0.tar.gz' ^C
[root@localhost ~]# mv /tmp/VMwareDnD/PBHaKE/amoeba-mysql-binary-2.2.0.tar.gz ~
[root@localhost ~]# tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
[root@localhost ~]# chmod -R 755 /usr/local/amoeba/
配置Amoeba读写分离,两个Slave读负载均衡
在Master、Slave1、Slave2服务器中配置Amoeba的访问授权
master主机添加授权
MariaDB [(none)]> grant all on *.* to 'test'@'192.168.1.%' identified by '123.com';
Query OK, 0 rows affected (0.00 sec)
server1从数据库添加授权
MariaDB [(none)]> grant all on *.* to 'test'@'192.168.1.%' identified by '123.com';
Query OK, 0 rows affected (0.00 sec)
server2从数据库添加授权
MariaDB [(none)]> grant all on *.* to 'test'@'192.168.1.%' identified by '123.com';
Query OK, 0 rows affected (0.00 sec)
刷新授权
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
回到Amoeba服务机修改配置文件
编辑amoeba.xml配置文件
vim /usr/local/amoeba/conf/amoeba.xml
[root@localhost ~]# vim /usr/local/amoeba/conf/amoeba.xml
30 amoeba
31
32 123456
下翻至115行把注释掉的删除
115 master
116
117
118 master
119 slaves
编辑dbServer.xml配置文件
vim /usr/local/amoeba/conf/dbServers.xml
23 test
24
25
26 test
27
28
29 123.com
45
46
47
48 192.168.1.1
49
50
51
52
53
54
55 192.168.1.2
56
57
58
59
60
61
62 192.168.1.3
63
64
65
66
67
68
69 1
70
71
72 slave1,slave2
73
74
75
76
1 2 3 4 5 6 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
配置无误后,启动Amoeba软件,默认端口是TCP协议8066
/usr/local/amoeba/bin/amoeba start &
netstat -lnpt | grep 8066
netstat -anpt | grep 3306
[root@localhost ~]# /usr/local/amoeba/bin/amoeba start &
[1] 61989
[root@localhost ~]# log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
2023-07-29 02:37:21,333 INFO context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-2.2.0
log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf
2023-07-29 02:37:21,472 INFO net.ServerableConnectionManager - Amoeba for Mysql listening on 0.0.0.0/0.0.0.0:8066.
2023-07-29 02:37:21,472 INFO net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:28891.
[root@localhost ~]# netstat -lnpt | grep 8066
tcp6 0 0 :::8066 :::* LISTEN 61989/java
[root@localhost ~]#
[root@localhost ~]# netstat -anpt | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 59177/mysqld
tcp6 0 0 192.168.1.4:38582 192.168.1.2:3306 ESTABLISHED 61989/java
tcp6 0 0 192.168.1.4:40164 192.168.1.3:3306 ESTABLISHED 61989/java
tcp6 0 0 192.168.1.4:49912 192.168.1.1:3306 ESTABLISHED 61989/java
[root@localhost ~]#
测试连通性
在Client上进行访问测试
yum -y install mariadb mariadb-devel
访问代理阿米巴
mysql -uamoeba -p123456 -h 192.168.200.114 -P 8066
测试阿米巴代理
mysql主服务器创建数据库、数据表
MariaDB [(none)]> create database test1;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use test1;
Database changed
MariaDB [test1]> create table student(id int(10),name varchar(10),daaress varchar(20));
Query OK, 0 rows affected (0.03 sec)
MariaDB [test1]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test1 |
+--------------------+
6 rows in set (0.00 sec)
Slave1与Slave2测试是否同步
同步成功
Slave服务器1
[root@localhost ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 70
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test1 |
+--------------------+
6 rows in set (0.01 sec)
MariaDB [(none)]> use test1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test1]> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| student |
+-----------------+
1 row in set (0.00 sec)
MariaDB [test1]>
Slave服务器2
[root@localhost ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 70
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test1 |
+--------------------+
6 rows in set (0.01 sec)
MariaDB [(none)]> use test1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test1]> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| student |
+-----------------+
1 row in set (0.00 sec)
MariaDB [test1]>
回到mysql主服务器创建数据信息
依次在slave两台服务器创建一条新的数据信息
mysql主服务器创建数据信息
MariaDB [test1]> insert into student values('1','zhangsan','master');
Query OK, 1 row affected (0.01 sec)
MariaDB [test1]>
slave服务器1创建数据信息
MariaDB [test1]> insert into student values('2','lisi','slave1');
Query OK, 1 row affected (0.00 sec)
MariaDB [test1]>
slave服务器1创建数据信息
MariaDB [test1]> insert into student values('3','wangwu','slave2');
Query OK, 1 row affected (0.00 sec)
MariaDB [test1]>
在client测试机上测试阿米巴代理读写分离功能
两台读取服务器为slave1、slave2轮流读取服务器搭建成功
查看是否从主服务器同步过来test1
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lwj |
| mysql |
| performance_schema |
| test |
| test1 |
+--------------------+
6 rows in set (0.01 sec)
指定数据库
MySQL [(none)]> use test1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
查看数据表
MySQL [test1]> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| student |
+-----------------+
1 row in set (0.01 sec)
第一次查看数据
MySQL [test1]> select * from student;
+------+----------+---------+
| id | name | daaress |
+------+----------+---------+
| 1 | zhangsan | master |
| 2 | lisi | slave1 |
+------+----------+---------+
2 rows in set (0.01 sec)
第二次查看数据
MySQL [test1]> select * from student;
+------+----------+---------+
| id | name | daaress |
+------+----------+---------+
| 1 | zhangsan | master |
| 3 | wangwu | slave2 |
+------+----------+---------+
2 rows in set (0.01 sec)
第三次查看数据
MySQL [test1]> select * from student;
+------+----------+---------+
| id | name | daaress |
+------+----------+---------+
| 1 | zhangsan | master |
| 2 | lisi | slave1 |
+------+----------+---------+
2 rows in set (0.01 sec)
MySQL [test1]>
测试写操作:
在Client上插入一条语句
在client插入数据信息
MySQL [test1]> insert into student values('4','zhaoliu','client');
Query OK, 1 row affected (0.01 sec)
slave1与2看不到数据信息只有master能看到