最近公司項(xiàng)目要求MySQL高可用,加上以前公司聽過QiHoo360的Atlas蝉绷,所以就嘗試搭建了一個(gè)MySQL讀寫分離鸭廷,并且高可用的枣抱。
前期準(zhǔn)備
- 準(zhǔn)備4臺(tái)機(jī)器熔吗,系統(tǒng)為
CentOS release 6.6
- Ip分別為
192.168.20.121
、192.168.20.122
佳晶、192.168.20.123
桅狠、192.168.20.124
- 4臺(tái)機(jī)器分別作為
Atlas代理服務(wù)
,master MySQL
,slave MySQL 1
中跌,slave MySQL 2
- 下載QiHoo360的Atlas 地址
安裝Atlas
下載得到
Atlas-XX.el6.x86_64.rpm
安裝文件sudo rpm –i Atlas-XX.el6.x86_64.rpm
安裝安裝在
/usr/local/mysql-proxy
-
安裝目錄分析
-
bin
- 可執(zhí)行文件
-
encrypt
用來加密密碼咨堤,后面會(huì)用到 -
mysql-proxy
是MySQL自己的讀寫分離代理 -
mysql-proxyd
操作Atlas VERSION
-
conf
-
test.cnf
配置文件 - 一個(gè)文件為一個(gè)實(shí)例,實(shí)例名即文件名漩符,啟動(dòng)需要帶上這個(gè)實(shí)例名
-
-
lib
依賴包 -
log
記錄日志
-
啟動(dòng)命令:
/usr/local/mysql-proxy/bin/mysql-proxyd [實(shí)例名] start
停止命令:
/usr/local/mysql-proxy/bin/mysql-proxyd [實(shí)例名] stop
同理一喘,
restart
為重啟,status
為查看狀態(tài)-
配置文件解釋
請(qǐng)查看官方文檔
數(shù)據(jù)庫(kù)配置
1臺(tái)master2臺(tái)slave嗜暴,都要配置相同的用戶名密碼凸克,且都要可以遠(yuǎn)程訪問
-
分別進(jìn)入3臺(tái)服務(wù)器,創(chuàng)建相同的用戶名密碼闷沥,創(chuàng)建數(shù)據(jù)庫(kù)
test
萎战,設(shè)置權(quán)限CREATE USER 'test'@'%' IDENTIFIED BY 'test123'; CREATE USER 'test'@'localhost' IDENTIFIED BY 'test123'; grant all privileges on test.* to 'test'@'%' identified by 'test123'; grant all privileges on test.* to 'test'@'localhost' identified by 'test123'; flush privileges;
-
主從數(shù)據(jù)庫(kù)配置
- 配置master服務(wù)器
找到MySQL配置文件
my.cnf
,一般在etc
目錄下-
修改配置文件
[mysqld] # 一些其他配置 ... #主從復(fù)制配置 innodb_flush_log_at_trx_commit=1 sync_binlog=1 #需要備份的數(shù)據(jù)庫(kù) binlog-do-db=test #不需要備份的數(shù)據(jù)庫(kù) binlog-ignore-db=mysql #啟動(dòng)二進(jìn)制文件 log-bin=mysql-bin #服務(wù)器ID server-id=1 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0
重啟數(shù)據(jù)庫(kù)
service mysql restart
-
進(jìn)入數(shù)據(jù)庫(kù)舆逃,配置主從復(fù)制的權(quán)限
mysql -uroot -p123456 grant replication slave on *.* to 'test'@'127.0.0.1' identified by 'test123';
-
查看主數(shù)據(jù)庫(kù)信息蚂维,記住下面的
File
與Position
的信息,它們是用來配置從數(shù)據(jù)庫(kù)的關(guān)鍵信息路狮。mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 17620976 | test | mysql | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
- 配置兩臺(tái)salve服務(wù)器
找到配置文件
my.cnf
-
修改配置文件如下
[mysqld] # 一些其他配置 ... # 幾臺(tái)服務(wù)器不能一樣 server-id=2 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0
-
進(jìn)入數(shù)據(jù)庫(kù)虫啥,配置從數(shù)據(jù)庫(kù)的信息,這里輸入剛才記錄下來的
File
與Position
的信息览祖,并且在從服務(wù)器上執(zhí)行:# master數(shù)據(jù)庫(kù)的ip mysql> change master to master_host='192.168.20.122', # master的用戶名 -> master_user='buck', # 密碼 -> master_password='hello', # 端口 -> master_port=3306, # master數(shù)據(jù)庫(kù)的`File ` -> master_log_file='mysql-bin.000002', # master數(shù)據(jù)庫(kù)的`Position` -> master_log_pos=17620976, -> master_connect_retry=10;
-
啟動(dòng)進(jìn)程
mysql> start slave; Query OK, 0 rows affected (0.00 sec)
-
檢查主從復(fù)制狀態(tài)孝鹊,要看到下列
Slave_IO_Running
、Slave_SQL_Running
的信息中展蒂,兩個(gè)都是Yes
又活,才說明主從連接正確,如果有一個(gè)是No锰悼,需要重新確定剛才記錄的日志信息柳骄,停掉“stop slave”重新進(jìn)行配置主從連接。mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.246.134 Master_User: buck Master_Port: 3306 Connect_Retry: 10 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 17620976 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000002 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: 17620976 Relay_Log_Space: 407 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: 1 row in set (0.00 sec) ERROR: No query specified
- 配置master服務(wù)器
Atlas配置
-
使用Atlas的加密工具對(duì)上面用戶的密碼進(jìn)行加密
/usr/local/mysql-proxy/bin/encrypt test123 29uENYYsKLo=
-
配置atlas
這是用來登錄到Atlas的管理員的賬號(hào)與密碼箕般,與之對(duì)應(yīng)的是
Atlas監(jiān)聽的管理接口IP和端口
耐薯,也就是說需要設(shè)置管理員登錄的端口,才能進(jìn)入管理員界面丝里,默認(rèn)端口是2345
曲初,也可以指定IP登錄,指定IP后杯聚,其他的IP無(wú)法訪問管理員的命令界面臼婆。方便測(cè)試,我這里沒有指定IP和端口登錄幌绍。-
配置主數(shù)據(jù)的地址與從數(shù)據(jù)庫(kù)的地址颁褂,這里配置的主數(shù)據(jù)庫(kù)是122故响,從數(shù)據(jù)庫(kù)是123、124
#Atlas后端連接的MySQL主庫(kù)的IP和端口颁独,可設(shè)置多項(xiàng)彩届,用逗號(hào)分隔 proxy-backend-addresses = 192.168.20.122:3306 #Atlas后端連接的MySQL從庫(kù)的IP和端口,@后面的數(shù)字代表權(quán)重誓酒,用來作負(fù)載均衡樟蠕,若省略則默認(rèn)為1,可設(shè)置多項(xiàng)靠柑,用逗號(hào)分隔 proxy-read-only-backend-addresses = 192.168.20.123:3306@1,192.168.20.124:3306@2
-
這個(gè)是用來配置MySQL的賬戶與密碼的坯墨,就是上面創(chuàng)建的用戶,用戶名是
test
,密碼是test123
,剛剛使用Atlas提供的工具生成了對(duì)應(yīng)的加密密碼pwds = buck:RePBqJ+5gI4=
-
啟動(dòng)Atlas
[root@localhost /usr/local/mysql-proxy/bin]# ./mysql-proxyd test start OK: MySQL-Proxy of test is started
測(cè)試
-
進(jìn)入atlas的管理界面
[root@localhost ~]#mysql -h127.0.0.1 -P2345 -uuser -ppwd 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, 2013, 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 | +----------------------------+---------------------------------------------------------+ 16 rows in set (0.00 sec) mysql>
-
使用工作接口來訪問
[root@localhost ~]#mysql -h127.0.0.1 -P1234 -utest -ptest123 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 34 Server version: 5.0.81-log MySQL Community Server (GPL) Copyright (c) 2000, 2013, 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) mysql>
使用可視化管理工具Navicat
登錄
使用用戶名test
病往、密碼test123
捣染、端口1234
、地址192.168.20.121
正常登錄停巷。注意耍攘,這里登錄的是atlas服務(wù)器,不再是任何一個(gè)MySQL服務(wù)器