|NO.Z.00097|——————————|^^ 部署 ^^|——|Linux&MySQL集群.V05|——|MySQL中间件|主从配置|
一、mysql中间件

### --- 中间件:
~~~ 一种提供在不同技术、不同的软件之间共享资源的程序,更大化了利用了数据库的性能,
~~~ 可以无限扩展(注:真实环境中并非如此)
### --- 数据库的中间件:
~~~ mysql proxy (官方版本) 性能低,需要 lua 脚本
~~~ atlas 性能低,响应时间长
~~~ amoeba 陈思儒研发的
二、先搭建一个主从关系的服务器
### --- 先搭建一个主从关系的服务器
### --- 部署规划
~~~ centos6.x server21:20.20.20.21 主服务器
~~~ centos6.x server21:20.20.20.21 从服务器
~~~ centos6.x server21:20.20.20.21 amoeba服务器
### --- 在主、从服务器上安装 mysql mysql-server
~~~ 在主从服务器上安装mysql服务端和客户端;并开启二进制日志
[root@server21 ~]# yum install -y mysql mysql-server
[root@server21 ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=21
[root@server21 ~]# chkconfig mysqld on
[root@server21 ~]# service mysqld start
[root@server22 ~]# yum install -y mysql mysql-server
[root@server22 ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=22
[root@server22 ~]# chkconfig mysqld on
[root@server22 ~]# service mysqld start
### --- 在主服务器上授权,从服务器上保存授权信息,并开启从服务线程。
~~~ 配置主服务器
[root@server21 ~]# mysql
mysql> grant replication slave on *.* to slave@'20.20.20.22' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 257 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
### --- 关闭从服务器线程,为了做读写分离时,测试有明显的实验效果(实际生产环境中不能停掉。。)
[root@server22 ~]# mysql
mysql> change master to
-> master_user='slave',
-> master_password='123456',
-> master_host='20.20.20.21',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=257;
Query OK, 0 rows affected (0.05 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
三、验证测试
### --- 测试
~~~ 在主服务器上创建数据库和表
mysql> create database atyanqi; // 在主服务上创建数据库atyanqi
Query OK, 1 row affected (0.00 sec)
mysql> use atyanqi;
Database changed
mysql> create table yjs (id int,name char(20),age int); // 在atyanqi数据库中插入表yjs
Query OK, 0 rows affected (0.02 sec)
mysql> insert into yjs values (1,'laow',28),(2,'laos',29); // 在yjs表中写入数据
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
~~~ 在主服务器上查看创建的表和数据
mysql> select * from yjs;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | laow | 28 |
| 2 | laos | 29 |
+------+------+------+
2 rows in set (0.00 sec)
~~~ 在从服务器上查看创建的表和数据
mysql> select * from atyanqi.yjs;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | laow | 28 |
| 2 | laos | 29 |
+------+------+------+
2 rows in set (0.00 sec)
### --- 停止从进程
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into atyanqi.yjs values (3,'laox',18); // 在从上面再加入一条数据
Query OK, 1 row affected (0.01 sec)
mysql> select * from atyanqi.yjs; // 在从上面查看是3条数据
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | laow | 28 |
| 2 | laos | 29 |
| 3 | laox | 18 |
+------+------+------+
3 rows in set (0.01 sec)
mysql> select * from yjs; // 主服务器上查看还是2条数据
+------+------+------+ // 在生产环境中是不会出现这种情况的。
| id | name | age |
+------+------+------+
| 1 | laow | 28 |
| 2 | laos | 29 |
+------+------+------+
2 rows in set (0.00 sec)
### --- 启动从服务器的服务
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
分类:
cdv007-network
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」