MySQL+Altas
【参考文档:https://www.cnblogs.com/MENGSHIYU/p/12061176.html】
环境说明 服务器:centos 7.0 MySQL:5.7.29 MySQL一主一从 Altas:2.2.1 服务器主机名:服务器IP:作用 bre01 192.168.248.129 Altas bre02 192.168.248.130 MySQL主库 bre03 192.168.248.131 MySQL从库 1.MySQL部署(两台MySQL服务器操作) [root@bre02 ~]# useradd mysql [root@bre02 ~]# mkdir /data/mysql/ -p [root@bre02 ~]# chown -R mysql: /data/mysql [root@bre02 opt]# tar -xf /opt/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@bre02 opt]# mv /usr/local/mysql-5.7.29-linux-glibc2.12-x86_64/ /usr/local/mysql [root@bre02 opt]# chown -R mysql: /usr/local/mysql/ [root@bre02 opt]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql [root@bre02 opt]# /usr/local/mysql//bin/mysql_ssl_rsa_setup --datadir=/data/mysql [root@bre02 opt]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld [root@bre02 opt]# /etc/init.d/mysqld start [root@bre02 opt]# grep password /data/mysql/error.log #查出默认生成密码 [root@bre02 opt]# mysql -uroot -p root@localhost:(none) 11:35:20> set password=password('root'); [root@bre02 opt]# mysql -uroot -proot #创建主从使用用户 (主库操作) root@localhost:(none) 11:36:46> grant replication slave,replication client on *.* to slave@'192.168.248.131' identified by "slave@123"; root@localhost:(none) 11:39:10> grant all privileges on *.* to wjht@'%' identified by 'root123'; root@localhost:(none) 11:38:24> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 1751 | | | | +------------------+----------+--------------+------------------+-------------------+ [root@bre03 opt]# mysql -uroot -proot #创建主从架构 (从库操作) root@localhost:(none) 11:51:30> CHANGE MASTER TO MASTER_HOST='192.168.248.130',MASTER_USER='slave',MASTER_PASSWORD='slave@123',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=1751; root@localhost:(none) 11:53:34> start slave; Altas 安装 [root@bre01 ~]# wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm [root@bre01 ~]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm #默认安装路径为:/usr/local/mysql-proxy/ 修改altas的配置文件 [root@bre04 mysql-proxy]# cd /usr/local/mysql-proxy/conf/;cp test.cnf test.cnf.bak 使用encrypt 生成密码加密文 [root@bre01 bin]# ./encrypt 123456 /iZxz+0GRoA= [root@bre04 mysql-proxy]# vim test.cnf [mysql-proxy] #带#号的为非必需的配置项目 #管理接口的用户名 admin-username=user #管理接口的密码 admin-password=pwd #Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔 proxy-backend-addresses=192.168.248.130:3306 #Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔 #proxy-read-only-backend-addresses = 192.168.248.131:3306@1 #用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码! pwds=wjht:wnOkwXC7jkU= #设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。 daemon=true #设置Atlas的运行方式,设为true时Atlas会启动两个进程,一个为monitor,一个为worker,monitor在worker意外退出后会自动将其重启,设为false时只有worker,没有monitor,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。keepalive=true #工作线程数,对Atlas的性能有很大影响,可根据情况适当设置 event-threads=8 #日志级别,分为message、warning、critical、error、debug五个级别 log-level=message #日志存放的路径 log-path=/usr/local/mysql-proxy/log #SQL日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME代表记录SQL日志且实时写入磁盘,默认为OFF #sql-log = OFF #慢日志输出设置。当设置了该参数时,则日志只输出执行时间超过sql-log-slow(单位:ms)的日志记录。不设置该参数则输出全部日志。 #sql-log-slow = 10 #实例名称,用于同一台机器上多个Atlas实例间的区分 #instance = test #Atlas监听的工作接口IP和端口(对外提供) proxy-address=0.0.0.0:3306 #Atlas监听的管理接口IP和端口 admin-address=0.0.0.0:2345 #分表设置,此例中person为库名,mt为表名,id为分表字段,3为子表数量,可设置多项,以逗号分隔,若不分表则不需要设置该项 #tables = person.mt.id.3 #默认字符集,设置该项后客户端不再需要执行SET NAMES语句 #charset = utf8 #允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接 #client-ips = 127.0.0.1, 192.168.1 #Atlas前面挂接的LVS的物理网卡的IP(注意不是虚IP),若有LVS且设置了client-ips则此项必须设置,否则可以不设置 #lvs-ips = 192.168.1.1 [root@bre01 conf]# /usr/local/mysql-proxy/bin/mysql-proxyd test start [root@bre01 conf]# mysql -uuser -ppwd -h192.168.248.129 -P mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.0.99-agent-admin Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select * from help; +----------------------------+---------------------------------------------------------+ | command | description | +----------------------------+---------------------------------------------------------+ | SELECT * FROM help | shows this help | | SELECT * FROM backends | lists the backends and their state | | SET OFFLINE $backend_id | offline backend server, $backend_id is backend_ndx's id | | SET ONLINE $backend_id | online backend server, ... | | ADD MASTER $backend | example: "add master 127.0.0.1:3306", ... | | ADD SLAVE $backend | example: "add slave 127.0.0.1:3306", ... | | REMOVE BACKEND $backend_id | example: "remove backend 1", ... | | SELECT * FROM clients | lists the clients | | ADD CLIENT $client | example: "add client 192.168.1.2", ... | | REMOVE CLIENT $client | example: "remove client 192.168.1.2", ... | | SELECT * FROM pwds | lists the pwds | | ADD PWD $pwd | example: "add pwd user:raw_password", ... | | ADD ENPWD $pwd | example: "add enpwd user:encrypted_password", ... | | REMOVE PWD $pwd | example: "remove pwd user", ... | | SAVE CONFIG | save the backends to config file | | SELECT VERSION | display the version of Atlas | +----------------------------+---------------------------------------------------------+ mysql> select * from help; +----------------------------+---------------------------------------------------------+ | command | description | +----------------------------+---------------------------------------------------------+ | SELECT * FROM help | 查看帮助 | | SELECT * FROM backends | 查看后端数据库 | | SET OFFLINE $backend_id | 平滑下线DB,set offline 2; | | SET ONLINE $backend_id | 平滑下线DB,set offline 2; | | ADD MASTER $backend | 添加主库: "add master 127.0.0.1:3306" | | ADD SLAVE $backend | 添加从库: "add slave 127.0.0.1:3306" | | REMOVE BACKEND $backend_id | 摘除后端数据库: "remove backend 1" | | SELECT * FROM clients | 查看可登陆的客户端 | | ADD CLIENT $client | 添加客户端: "add client 192.168.1.2" | | REMOVE CLIENT $client | 删除客户端: "remove client 192.168.1.2" | | SELECT * FROM pwds | 查看用户名和密码 | | ADD PWD $pwd | 添加用户和密码: "add pwd root:123", 自动加密 | | ADD ENPWD $pwd | 添加用户和密码: "add enpwd root:wnOkwXC7jkU=", 手动加密 | | REMOVE PWD $pwd | 删除用户: "remove pwd root", | | SAVE CONFIG | 保存配置到配置文件 | | SELECT VERSION | 查看版本号 | +----------------------------+---------------------------------------------------------+ mysql> SELECT * FROM backends; +-------------+----------------------+-------+------+ | backend_ndx | address | state | type | +-------------+----------------------+-------+------+ | 1 | 192.168.248.130:3306 | up | rw | | 2 | 192.168.248.131:3306 | up | ro | +-------------+----------------------+-------+------+ mysql> select * from pwds; +----------+--------------+ | username | password | +----------+--------------+ | wjht | wnOkwXC7jkU= | +----------+--------------+ mysql> add pwd wj:root123; mysql> select * from pwds; +----------+--------------+ | username | password | +----------+--------------+ | wjht | wnOkwXC7jkU= | | wj | wnOkwXC7jkU= | +----------+--------------+ mysql> select version; +---------+ | version | +---------+ | 2.2.1 | +---------+ #平滑下线DB-2 mysql> set offline 2; 【主从还是好的,offline只是说atlas不忘2号库分配】 +-------------+----------------------+---------+------+ | backend_ndx | address | state | type | +-------------+----------------------+---------+------+ | 2 | 192.168.248.131:3306 | offline | ro | +-------------+----------------------+---------+------+ 1 row in set (0.00 sec) mysql> select * from backends; +-------------+----------------------+---------+------+ | backend_ndx | address | state | type | +-------------+----------------------+---------+------+ | 1 | 192.168.248.130:3306 | up | rw | | 2 | 192.168.248.131:3306 | offline | ro | +-------------+----------------------+---------+------+ #平滑上线DB-2 mysql> set online 2; +-------------+----------------------+---------+------+ | backend_ndx | address | state | type | +-------------+----------------------+---------+------+ | 2 | 192.168.248.131:3306 | unknown | ro | +-------------+----------------------+---------+------+ 1 row in set (0.00 sec) mysql> select * from backends; +-------------+----------------------+-------+------+ | backend_ndx | address | state | type | +-------------+----------------------+-------+------+ | 1 | 192.168.248.130:3306 | up | rw | | 2 | 192.168.248.131:3306 | up | ro | +-------------+----------------------+-------+------+ 2 rows in set (0.00 sec)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!