简述:
MySQL主从复制是一种常用的数据库复制技术,它可以将一个MySQL数据库的数据复制到其他多个MySQL数据库中,实现数据的备份和负载均衡。主从复制的原理是将主数据库的binlog日志传输给从数据库,从数据库通过解析binlog日志来实现数据的同步。
读写分离是在主从复制的基础上进一步优化数据库性能的一种技术。通过将读操作和写操作分离到不同的数据库实例上,可以提高数据库的并发处理能力和读取性能。通常情况下,主数据库负责处理写操作,从数据库负责处理读操作。
读写分离可以通过在应用程序中配置合适的数据库连接方式来实现。 在实际应用中,MySQL主从复制和读写分离可以结合使用,通过主从复制实现数据的备份和同步,通过读写分离实现数据库的负载均衡和性能优化。这种架构可以提高数据库的可用性和性能,适用于高并发的应用场景
这里四台主机实验
1:192.168.1.131 主服务器Master
2:192.168.1.139 从服务器slave1
3:192.168.1.123 从服务器slave2
4:192.168.1.132 amoeba服务器 做读写分离的
5. 客户机 实际情况下有 实验状态第四台代理

1.主从复制
相同操作
1.让其他主机同步指定的主机
yum -y install ntp
ntpdate 192.168.1.131 同步时间的
通过与该服务器进行时间同步,可以确保系统时间与网络时间保持一致。这对于需要准确时间的应用程序和系统非常重要,例如日志记录、安全认证等。
2.修改配置文件主服务器
vim /etc/my.cnf
server-id=1 编号
log-bin=mysql-binlog 启动日志
log-slave-updates=true 开启主从复制两台从服务器一样修改 1代表是主其他是从往后以此类推
server-id=2 编号
log-bin=mysql-binlog 启动日志
log-slave-updates=true 开启主从复制server-id=3 编号
log-bin=mysql-binlog 启动日志
log-slave-updates=true 开启主从复制安装 mariadb
主和从安装
yum -y install mariadb mariadb-server
启动
systemctl restart mariadb
主服务 创建replication用户
MariaDB [(none)]> grant replication slave on *.* to 'myslave'@'192.168.1.%' identified by '123456';
刷新 flush privileges;
获取二进制文件位置
show master status;
备份Master原有数据 如果有的话
mysqldump -uroot --all-databases > /root/alldbbackup.sql
通过scp传输给slave1,slave2服务器导入到 本地数据库
scp /root/ alldbbackup.sql root@192.168.1.139:/root/
从 slave配置
进入MySQL
停止从服务 stop slave;
MariaDB [(none)]> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec)添加主库信息
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.1.131', MASTER_USER='myslave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-binlog.000003', MASTER_LOG_POS=475; Query OK, 0 rows affected (0.00 sec)启动
MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec)查看 看到两个yes就成了
slave1 的 MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.131 Master_User: myslave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-binlog.000003 Read_Master_Log_Pos: 475 Relay_Log_File: relay-log-bin.000002 Relay_Log_Pos: 532 Relay_Master_Log_File: mysql-binlog.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: slave2的 MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.131 Master_User: myslave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-binlog.000003 Read_Master_Log_Pos: 475 Relay_Log_File: relay-log-bin.000002 Relay_Log_Pos: 532 Relay_Master_Log_File: mysql-binlog.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB:
2.读写分离
最后一台192.168.1.132 amoeba服务器 这里是准备好包的
基本的原理是让主数据库处理事务性查询,而从数据库处理select查询,数据库复制被用来把事务性查询导致的改变更新同步到集群中的从数据库
安装java环境
降版本
rz上传
jdk-6u31-linux-x64.bin
本身是一个二进制文件,脚本
省略了编译的过程,直接给执行权限安装
给执行权限
chmod +x jdk-6u31-linux-x64.bin运行
./jdk-6u31-linux-x64.bin到这里回车
移动
[root@localhost /]# mv jdk1.6.0_31/ /usr/local/jdk1.6添加变量
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.6
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/lib:$JAVA_HOME/jre/bin:$HOME/bin刷新
[root@localhost /]# source /etc/profile删除原jdk
[root@localhost /]# rm -rf /usr/bin/java

安装amoeba
rz amoeba-mysql-binary-2.2.0.tar
创建备用目录 mkdir /usr/local/amoeba
解压 tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
给执行权限
chmod -R 755 /usr/local/amoeba/设置变量 还是刚刚设置变量哪里
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME/bin刷新
source /etc/profile
在Master、Slave1、Slave2服务器中配置Amoeba的访问授权
MariaDB [(none)]> grant all on *.* to 'test'@'192.168.1.%' identified by '123456';
amoeba服务器修改配置文件
vim /usr/local/amoeba/conf/amoeba.xml
vim /usr/local/amoeba/conf/amoeba.xml
用户连接amoeba的设置
30 <property name="user">amoeba</property> 用户名
32 <property name="password">123456</property> 连接amaeba的密码
115 <property name="defaultPool">master</property> 默认池
117 <property name="writePool">master</property> 写的池
118 <property name="readPool">slaves</property> 读的池
修改数据服务配置文件
vim /usr/local/amoeba/conf/dbServers.xml

![]()

22 端口号 <property name="port">3306</property>
23 连接mysql的用户,MySQl授权的用户 <property name="schema">test</property>
这个用户可以不改,如果数据库设置的其他用户的话,只要下面的用户名是设置的用户即可
否则可以登录但是查不到数据显示断开数据连接
26 登录用户和授权用户一 <property name="user">test</property>
29 MySQL授权的密码<property name="password">123.com</property>
45 定义master 48 master的IP地址
<dbServer name="master" parent="abstractServer">
<property name="ipAddress">192.168.1.131</property>
原来52-56 定义从和从IP,几个从数据库几段
<dbServer name="slave1" parent="abstractServer">
<property name="ipAddress">192.168.1.139</property>这个是复制"slave1的
<dbServer name="slave2" parent="abstractServer">
<property name="ipAddress">192.168.1.123</property>
65 定义读的池slave
<dbServer name="slaves" virtual="true">68 轮询
property name="loadbalance">1</property>
<property name="poolNames">slave1,slave2</property>
启动放入后台
[root@localhost ~]# cd /usr/local/amoeba/bin
[root@localhost bin]# ./amoeba start &查看端口8066有就成功了
实验
我们没有客户端实验就用 amoeba服务器实验 安装 启动
[root@localhost ~]# yum -y install mariadb*
登陆
[root@localhost conf]# mysql -uamoeba -p123456 -h 192.168.1.132 -P 8066
先在主创建库
MySQL [(none)]> create database ddd;
Query OK, 1 row affected (0.02 sec)
从查看都有

slave1插入数据
MariaDB [ddd]> select * from customer;
+----+------------+------------------------+---------------------+
| id | name | email | created_date |
+----+------------+------------------------+---------------------+
| 1 | John Doe | john.doe@example.com | 2023-07-01 17:24:17 |
| 2 | Jane Smith | jane.smith@example.com | 2023-07-01 17:24:17 |
| 3 | John Doe | john.doe@example.com | 2023-07-01 17:25:04 |
| 4 | Jane Smith | jane.smith@example.com | 2023-07-01 17:25:04 |
| 5 | slave1 | slave1 | 2023-07-01 17:25:28 |
+----+------------+------------------------+---------------------+
slave2插入数据
MariaDB [ddd]> select * from customer;
+----+------------+------------------------+---------------------+
| id | name | email | created_date |
+----+------------+------------------------+---------------------+
| 1 | John Doe | john.doe@example.com | 2023-07-01 17:24:17 |
| 2 | Jane Smith | jane.smith@example.com | 2023-07-01 17:24:17 |
| 3 | slave2 | slave2 | 2023-07-01 17:26:12 |
+----+------------+------------------------+---------------------+
amoeba服务器查看 有轮询
MySQL [ddd]> select * from customer;
+----+------------+------------------------+---------------------+
| id | name | email | created_date |
+----+------------+------------------------+---------------------+
| 1 | John Doe | john.doe@example.com | 2023-07-01 17:24:17 |
| 2 | Jane Smith | jane.smith@example.com | 2023-07-01 17:24:17 |
| 3 | John Doe | john.doe@example.com | 2023-07-01 17:25:04 |
| 4 | Jane Smith | jane.smith@example.com | 2023-07-01 17:25:04 |
| 5 | slave1 | slave1 | 2023-07-01 17:25:28 |
| 10 | master2 | master2 | 2023-07-01 17:39:13 |
+----+------------+------------------------+---------------------+
6 rows in set (0.00 sec)
MySQL [ddd]> select * from customer;
+----+------------+------------------------+---------------------+
| id | name | email | created_date |
+----+------------+------------------------+---------------------+
| 1 | John Doe | john.doe@example.com | 2023-07-01 17:24:17 |
| 2 | Jane Smith | jane.smith@example.com | 2023-07-01 17:24:17 |
| 3 | slave2 | slave2 | 2023-07-01 17:26:12 |
| 10 | master2 | master2 | 2023-07-01 17:39:13 |
+----+------------+------------------------+---------------------+
4 rows in set (0.01 sec)
服务器是没有的
MariaDB [ddd]> select * from customer
-> ;
+----+------------+------------------------+---------------------+
| id | name | email | created_date |
+----+------------+------------------------+---------------------+
| 1 | John Doe | john.doe@example.com | 2023-07-01 17:24:17 |
| 2 | Jane Smith | jane.smith@example.com | 2023-07-01 17:24:17 |
+----+------------+------------------------+---------------------+
现在把两个从关闭 都是no了

然后在amoeba服务器创建
MySQL [(none)]> INSERT INTO customer (name, email) VALUES ('amoeba1', 'amoeba1');
Query OK, 1 row affected (0.01 sec)
MySQL [(none)]> INSERT INTO customer (name, email) VALUES ('amoeba1', 'amoeba1');
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> INSERT INTO customer (name, email) VALUES ('amoeba1', 'amoeba1');
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> INSERT INTO customer (name, email) VALUES ('amoeba1', 'amoeba1');
Query OK, 1 row affected (0.01 sec)
MySQL [(none)]> INSERT INTO customer (name, email) VALUES ('amoeba1', 'amoeba1');
Query OK, 1 row affected (0.00 sec)
主查看
MariaDB [ddd]> select * from customer ;
+----+------------+------------------------+---------------------+
| id | name | email | created_date |
+----+------------+------------------------+---------------------+
| 1 | John Doe | john.doe@example.com | 2023-07-01 17:24:17 |
| 2 | Jane Smith | jane.smith@example.com | 2023-07-01 17:24:17 |
| 3 | amoeba1 | amoeba1 | 2023-07-01 17:30:07 |
| 4 | amoeba1 | amoeba1 | 2023-07-01 17:30:10 |
| 5 | amoeba1 | amoeba1 | 2023-07-01 17:30:11 |
| 6 | amoeba1 | amoeba1 | 2023-07-01 17:30:11 |
| 7 | amoeba1 | amoeba1 | 2023-07-01 17:30:11 |
| 8 | amoeba1 | amoeba1 | 2023-07-01 17:30:11 |
+----+------------+------------------------+---------------------+
8 rows in set (0.00 sec)
把从打开

不会同步
amoeba服务器创建
MySQL [ddd]> INSERT INTO customer (name, email) VALUES ('LY1', 'LY1');
Query OK, 1 row affected (0.00 sec)
主从全都有

实验完成
文章介绍了如何配置MySQL的主从复制,通过时间同步、配置文件修改、用户授权和数据备份实现数据同步。接着,讲述了读写分离的概念,通过Amoeba服务器实现读写操作的分离,提高数据库性能。最后,展示了在Amoeba服务器上进行的读写测试,验证了主从复制和读写分离的效果。





被折叠的 条评论
为什么被折叠?



