一、MHA故障处理
1. 检查MHA状态
[root@db03 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
2 检查配置文件
[root@db03 ~]# cat /etc/mha/app1.cnf
[server default]
manager_log=/var/log/mha/app1/manager
manager_workdir=/var/log/mha/app1
master_binlog_dir=/data/binlog
master_ip_failover_script=/usr/local/bin/master_ip_failover
password=mha
ping_interval=2
repl_password=123
repl_user=repl
report_script=/usr/local/bin/send
ssh_user=root
user=mha
[server1]
hostname=10.0.0.51
port=3306
[server3]
hostname=10.0.0.53
port=3306
[binlog1]
no_master=1
hostname=10.0.0.53
master_binlog_dir=/data/mysql/binlog
3. 检查ssh状态:
[root@db03 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
4. 检查脚本
[root@db03 /usr/local/bin]# ls -l /usr/local/bin/
5. 检查binlogserver
[root@db03 /usr/local/bin]# ps -ef |grep mysqlbinlog
6. 确认主库,恢复binlog_server
db01 [(none)]>show master status
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000006 | 194 | | | a5260572-c262-11e9-bab1-000c2980e248:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
[root@db03 /usr/local/bin]# cd /data/mysql/binlog/
[root@db03 /data/mysql/binlog]# rm -rf *
[root@db03 /data/mysql/binlog]# mysqlbinlog -R --host=10.0.0.51 --user=mha --password=mha --raw --stop-never mysql-bin.000006 &
7. 检查主库vip
[root@db01 ~]# ifconfig eth0:1 10.0.0.55/24
8. 检查主从状态
[root@db03 /data/mysql/binlog]# masterha_check_repl --conf=/etc/mha/app1.cnf
9. 启动MHA
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
[root@db03 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:8255) is running(0:PING_OK), master:10.0.0.51
二、atlas实现读写分离
原理
- 工作环境
大型网站为了软件大量的并发访问,除了在网站实现分布式负载均衡,远远不够。到了数据业务层、数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库连接操作,数据库必然会崩溃,数据丢失的话,后果更是 不堪设想。这时候,我们会考虑如何减少数据库的联接,一方面采用优秀的代码框架,进行代码的优化,采用优秀的数据缓存技术, 如:memcached,如果资 金丰厚的话,必然会想到假设服务器群,来分担主数据库的压力。那么通过MySQL主从配置,实现读写分离,减轻数据库压力。
- 基本原理
让master来响应事务性操作,让slave来响应select非事务性操作,然后再采用主从复制来把master上的事务性操作同步到slave数据库中。
1.1 安装Atlas
[root@db03 /server/tools]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:Atlas-2.2.1-1 ################################# [100%]
[root@db03 /server/tools]# cd /usr/local/mysql-proxy/conf
[root@db03 /usr/local/mysql-proxy/conf]# mv test.cnf test.cnf.bak
1.2 配置
[root@db03 /usr/local/mysql-proxy/conf]# cat > test.cnf <
1.3 启动atlas
[root@db03 /usr/local/mysql-proxy/conf]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
error: MySQL-Proxy of test is running now
[root@db03 /usr/local/mysql-proxy/conf]# netstat -lntup|grep proxy
tcp 0 0 0.0.0.0:2345 0.0.0.0:* LISTEN 8164/mysql-proxy
tcp 0 0 0.0.0.0:33060 0.0.0.0:* LISTEN 8164/mysql-proxy
1.4 远程登录mha用户
[root@db03 /usr/local/mysql-proxy/conf]# mysql -umha -pmha -h 10.0.0.53 -P33060
db03 [(none)]>
1.5 检查只读id,读的操作会在2个从节点上飘逸,自带负载均衡
#测试读操作
db03 [(none)]>select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 52 |
+-------------+
1 row in set (0.01 sec)
db03 [(none)]>select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 53 |
+-------------+
1 row in set (0.00 sec)
#测试写操作:
mysql> begin;select @@server_id;commit;
Query OK, 0 rows affected (0.01 sec)
+-------------+
| @@server_id |
+-------------+
| 51 |
+-------------+
1 row in set (0.00 sec)
1.6 生产授权用户
例子:root@'10.0.0.%'
(1) 业务主库中
db01 [(none)]>grant all on *.* to root@'10.0.0.%' identified by '123';
(2) 将密码加密处理
cd /usr/local/mysql-proxy/bin/
[root@db03 /usr/local/mysql-proxy/bin]# ./encrypt 123
3yb5jEku5h4=
(3) 修改配置文件
vim /usr/local/mysql-proxy/conf/test.cnf
...
pwds = repl:3yb5jEku5h4=,mha:O2jBXONX098=,root:3yb5jEku5h4=
...
(4) 重启atlas
[root@db03 /usr/local/mysql-proxy/conf]# /usr/local/mysql-proxy/bin/mysql-proxyd test restart
OK: MySQL-Proxy of test is stopped
OK: MySQL-Proxy of test is started
(5)登录测试
mysql -uroot -p123 -h 10.0.0.53 -P 33060
1.7 Atlas基础管理
# 连接管理接口
[root@db03 ~]# mysql -uuser -ppwd -h10.0.0.53 -P2345
# atlas常用的
(1) 查帮助
SELECT * FROM help ;
(2) 查看节点信息
SELECT * FROM backends ;
(3) 上线和下线节点
SET OFFLINE $backend_id ;
SET ONLINE $backend_id
(4) 删除和添加节点
REMOVE BACKEND 3;
ADD SLAVE 10.0.0.53:3306;
(5) 添加用户和删除用户
SELECT * FROM pwds ; 查看
REMOVE PWD $pwd ; 删除
ADD PWD root:123; 添加 明文
ADD ENPWD $pwd 添加 密文
(6) 持久化配置
SAVE CONFIG
| 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