通過Atlas實現MySQL讀寫分離

前期準備

準備4臺機器,系統為CentOS release 6.6

Ip分別為192.168.20.121、192.168.20.122、192.168.20.123、192.168.20.124

4臺機器分別作為Atlas代理服務,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

可執行文件

encrypt用來加密密碼,后面會用到

mysql-proxy是MySQL自己的讀寫分離代理

mysql-proxyd操作Atlas

VERSION

conf

test.cnf配置文件

一個文件為一個實例,實例名即文件名,啟動需要帶上這個實例名

lib依賴包

log記錄日志

啟動命令:/usr/local/mysql-proxy/bin/mysql-proxyd [實例名] start

停止命令:/usr/local/mysql-proxy/bin/mysql-proxyd [實例名] stop

同理,restart為重啟,status為查看狀態

配置文件解釋

請查看官方文檔

數據庫配置

1臺master2臺slave,都要配置相同的用戶名密碼,且都要可以遠程訪問分別進入3臺服務器,創建相同的用戶名密碼,創建數據庫test,設置權限

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;

主從數據庫配置

配置master服務器

找到MySQL配置文件my.cnf,一般在etc目錄下修改配置文件

[mysqld]

# 一些其他配置

...

#主從復制配置

innodb_flush_log_at_trx_commit=1

sync_binlog=1

#需要備份的數據庫

binlog-do-db=test

#不需要備份的數據庫

binlog-ignore-db=mysql

#啟動二進制文件log-bin=mysql-bin

#服務器IDserver-id=1

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

重啟數據庫service mysql restart

進入數據庫,配置主從復制的權限

mysql -uroot -p123456

grant replication slave on.to 'test'@'127.0.0.1' identified by 'test123';

查看主數據庫信息,記住下面的File與Position的信息,它們是用來配置從數據庫的關鍵信息。

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)

配置兩臺salve服務器

找到配置文件my.cnf修改配置文件如下

[mysqld]

# 一些其他配置

...

# 幾臺服務器不能一樣server-id=2

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

進入數據庫,配置從數據庫的信息,這里輸入剛才記錄下來的File與Position的信息,并且在從服務器上執行(執行的時候,#行不要復制進去):

# master數據庫的ip

mysql> change master to master_host='192.168.20.122',

# master的用戶名? ? ? ? master_user='buck',? ? ? ? # 密碼? ? ? ? master_password='hello',? ? ? ? # 端口? ? ? ? master_port=3306,? ? ? ? # master數據庫的`File `? ? ? ? master_log_file='mysql-bin.000002',? ? ? ? # master數據庫的`Position`? ? ? ? master_log_pos=17620976,? ? ? ? master_connect_retry=10;

啟動進程

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

檢查主從復制狀態,要看到下列Slave_IO_Running、Slave_SQL_Running的信息中,兩個都是Yes,才說明主從連接正確,如果有一個是No,需要重新確定剛才記錄的日志信息,停掉“stop slave”重新進行配置主從連接。

mysql> show slave status G;

1. row**

Slave_IO_State:WaitingformastertosendeventMaster_Host:192.168.246.134Master_User:buckMaster_Port:3306Connect_Retry:10Master_Log_File:mysql-bin.000002Read_Master_Log_Pos:17620976Relay_Log_File:mysqld-relay-bin.000002Relay_Log_Pos:251Relay_Master_Log_File:mysql-bin.000002Slave_IO_Running:YesSlave_SQL_Running:YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno:0Last_Error:Skip_Counter:0Exec_Master_Log_Pos:17620976Relay_Log_Space:407Until_Condition:NoneUntil_Log_File:Until_Log_Pos:0Master_SSL_Allowed:NoMaster_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:0Last_IO_Error:Last_SQL_Errno:0Last_SQL_Error:

1 row in set (0.00 sec)

ERROR:

No query specified

Atlas配置

使用Atlas的加密工具對上面用戶的密碼進行加密

/usr/local/mysql-proxy/bin/encrypt test123

29uENYYsKLo=

配置atlas

這是用來登錄到Atlas的管理員的賬號與密碼,與之對應的是Atlas監聽的管理接口IP和端口,也就是說需要設置管理員登錄的端口,才能進入管理員界面,默認端口是2345,也可以指定IP登錄,指定IP后,其他的IP無法訪問管理員的命令界面。方便測試,我這里沒有指定IP和端口登錄。配置主數據的地址與從數據庫的地址,這里配置的主數據庫是122,從數據庫是123、124

#Atlas后端連接的MySQL主庫的IP和端口,可設置多項,用逗號分隔

proxy-backend-addresses = 192.168.20.122:3306

#Atlas后端連接的MySQL從庫的IP和端口,@后面的數字代表權重,用來作負載均衡,若省略則默認為1,可設置多項,用逗號分隔

proxy-read-only-backend-addresses = 192.168.20.123:3306@1,192.168.20.124:3306@2

這個是用來配置MySQL的賬戶與密碼的,就是上面創建的用戶,用戶名是test,密碼是test123,剛剛使用Atlas提供的工具生成了對應的加密密碼

pwds = buck:RePBqJ+5gI4=啟動Atlas

root[@localhost/usr/local/mysql-proxy/bin]# ./mysql-proxyd test start

OK: MySQL-Proxy of test is started

測試

進入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

Other names may be trademarks of their respectiveowners.

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

Other names may be trademarks of their respectiveowners.

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登錄

著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。互聯網+時代,時刻要保持學習,攜手千鋒PHP,Dream It Possible。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,786評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,656評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,697評論 0 379
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,098評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,855評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,254評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,322評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,473評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,014評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,833評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,016評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,568評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,273評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,680評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,946評論 1 288
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,730評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,006評論 2 374

推薦閱讀更多精彩內容