日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践

發布時間:2024/4/14 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

、、-------------------------------------master--------------------------------

mysql> GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.65.133' identified by ‘123456’;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '‘123456’' at line 1
mysql> GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.65.133' identified by'123456';
Query OK, 0 rows affected (0.01 sec)

mysql> show master status
-> show master status;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show master status' at line 2
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 253 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

mysql> ^CCtrl-C -- exit!
Aborted
root@ubuntu:~# ^C
root@ubuntu:~# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.5.40-0ubuntu0.12.04.1-log (Ubuntu)

Copyright (c) 2000, 2014, 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> create database first_db;
Query OK, 1 row affected (0.00 sec)

mysql> create table first_tb(id int(3),name char(10));
ERROR 1046 (3D000): No database selected
mysql> use first_db;
Database changed
mysql> create table first_tb(id int(3),name char(10));
Query OK, 0 rows affected (0.01 sec)

mysql> in
mysql> insert into first_tb values (001,'myself');
Query OK, 1 row affected (0.00 sec)

mysql> ^CCtrl-C -- exit!
Aborted
root@ubuntu:~#

?

?

?

?

?

?

、、------------------------------------------------------------------------------------------

?

、、--------------------------------------slave----------------------------------------------------

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select 'host' from user where user='root';
+------+
| host |
+------+
| host |
| host |
| host |
| host |
+------+
4 rows in set (0.00 sec)

mysql> update user set host = '%' where user ='root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select 'host' from user where user='root';
+------+
| host |
+------+
| host |
| host |
| host |
| host |
+------+
4 rows in set (0.00 sec)

mysql> exit
Bye
root@ubuntu:~# vi /etc/mysql/my.cnf
root@ubuntu:~# service mysql restart
mysql stop/waiting
mysql start/running, process 1636
root@ubuntu:~# vi /etc/mysql/my.cnf
root@ubuntu:~# service mysql restart
mysql stop/waiting
mysql start/running, process 1812
root@ubuntu:~# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.5.40-0ubuntu0.12.04.1-log (Ubuntu)

Copyright (c) 2000, 2014, 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> change master to
-> master_host='192.168.65.132',
-> change master to master_host=’192.168.10.130’,master_user=’rep1’,master_password=’123456’,master_log_file=’in.000005’,master_log_pos=261;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'change master to master_host=’192.168.10.130’,master_user=’rep1’,master_' at line 3
mysql> change master to master_host=’192.168.65.132’,master_user=’rep1’,master_password=’123456’,master_log_file=’in.000005’,master_log_pos=261;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '’192.168.65.132’,master_user=’rep1’,master_password=’123456’,master_' at line 1
mysql> change master to master_host='192.168.65.132',master_user='rep1',master_password='123456',master_log_file='mysql-bin.000005',master_log_pos=261;
Query OK, 0 rows affected (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

?

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.65.132
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 253
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000001
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: 253
Relay_Log_Space: 410
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 132
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| first_db |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql>

、、------------------------------------------------------------------------------------------

隨著數據量不斷的增加,由單臺Mysql作為獨立的數據庫有的時候是不能滿足實際需求的,無論是在安全性,高可用性以及高并發等各個方面。

? ?因此,一般來說都是通過主從復制(Master-Slave)的方式來同步數據,再通過讀寫分離(MySQL-Proxy)來提升數據庫的并發負載能力這樣的方案來進行部署與實施的。

? ?使用mysql主從復制的好處有

? ?1、采用主從服務器這種架構,穩定性得以提升。如果主服務器發生故障,我們可以使用從服務器來提供服務。

? ?2、在主從服務器上分開處理用戶的請求,可以提升數據處理效率。

? ?3、將主服務器上的數據復制到從服務器上,保護數據免受意外的損失。

? ?如下圖所示:

?

下面是我在實際工作過程中所整理的筆記,在此分享出來,以供大家參考。

一、MySQL的安裝與配置

具體的安裝過程,建議參考我的這一篇文章:http://www.yzswyl.cn/blread-1639.html

值得一提的是,我的安裝過程都是源碼包編譯安裝的,并且所有的配置與數據等都統一規劃到了/opt/mysql目錄中,因此在一臺服務器上安裝完成以后,可以將整個mysql目錄打包,然后傳到其它服務器上解包,便可立即使用。

二、MySQL主從復制(全部復制)

場景描述:

主數據庫服務器:192.168.10.130,MySQL已經安裝,并且無應用數據。

從數據庫服務器:192.168.10.131,MySQL已經安裝,并且無應用數據。

2.1 主服務器上進行的操作

1 2 3 4 $ vi?/opt/mysql/etc/my.cnf [mysqld] log-bin=mysql-bin?? //[必須]啟用二進制日志 server-id=130?????? //[必須]服務器唯一ID,默認是1,一般取IP最后一段
1 2 3 4 5 6 7 8 9 10 11 12 13 #啟動mysql服務 /opt/mysql/init.d/mysql?start #通過命令行登錄管理MySQL服務器 /opt/mysql/bin/mysql?-uroot -p'new-password' #授權給從數據庫服務器192.168.10.131 mysql> GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.10.131'?identified by ‘password’; #查詢主數據庫狀態 Mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000005 | 261 | | | +------------------+----------+--------------+------------------+

記錄下 FILE 及 Position 的值,在后面進行從服務器操作的時候需要用到。

2.2 配置從服務器

1 2 3 4 $ vi?/opt/mysql/etc/my.cnf [mysqld] log-bin=mysql-bin?? //[必須]啟用二進制日志 server-id=131?????? //[必須]服務器唯一ID,默認是1,一般取IP最后一段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #啟動mysql服務 /opt/mysql/init.d/mysql?start #通過命令行登錄管理MySQL服務器 /opt/mysql/bin/mysql?-uroot -p'new-password' #執行同步SQL語句 mysql> change master to master_host=’192.168.10.130’, master_user=’rep1’, master_password=’password’, master_log_file=’mysql-bin.000005’, master_log_pos=261; #正確執行后啟動Slave同步進程 mysql> start slave; #主從同步檢查 mysql> show slave status\G ============================================== **************** 1. row ******************* Slave_IO_State: Master_Host: 192.168.10.130 Master_User: rep1 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 415 Relay_Log_File: localhost-relay-bin.000008 Relay_Log_Pos: 561 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: YES Slave_SQL_Running: YES Replicate_Do_DB: ……………省略若干…………… Master_Server_Id: 1 1 row in?set?(0.01 sec) ==============================================

其中Slave_IO_Running 與 Slave_SQL_Running 的值都必須為YES,才表明狀態正常。

注意:如果主服務器已經存在應用數據,則在進行主從復制時,需要做以下處理:

(1)主數據庫進行鎖表操作,不讓數據再進行寫入動作

mysql> FLUSH TABLES WITH READ LOCK;

(2)查看主數據庫狀態

mysql> show master status;

(3)記錄下 FILE 及 Position 的值。

將主服務器的數據文件(整個/opt/mysql/data目錄)復制到從服務器,建議通過tar歸檔壓縮后再傳到從服務器解壓。

(4)取消主數據庫鎖定

mysql> UNLOCK TABLES;

2.3 驗證主從復制效果

主服務器上的操作

1 2 3 4 5 6 7 8 9 #在主服務器上創建數據庫first_db mysql> create database first_db; Query Ok, 1 row affected (0.01 sec) #在主服務器上創建表first_tb mysql> create table first_tb(id?int(3),name char(10)); Query Ok, 1 row affected (0.00 sec) #在主服務器上的表first_tb中插入記錄 mysql> insert into first_tb values (001,’myself’); Query Ok, 1 row affected (0.00 sec)

在從服務器上查看

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 mysql> show databases; ============================= +--------------------+ | Database | +--------------------+ | information_schema | | first_db | | mysql | | performance_schema | | test?| +--------------------+ 5 rows in?set?(0.01 sec) ============================= #數據庫first_db已經自動生成 ??????? mysql> use first_db Database chaged mysql> show tables; ============================= +--------------------+ | Tables_in_first_db | +--------------------+ | first_tb | +--------------------+ 1 row in?set?(0.02 sec) ============================= #數據庫表first_tb也已經自動創建 ??????? mysql> select?* from first_tb; ============================= +------+------+ | id?| name | +------+------+ | 1 | myself | +------+------+ 1 rows in?set?(0.00 sec) ============================= #記錄也已經存在

由此,整個MySQL主從復制的過程就完成了。

三、MySQL主從復制復制部分庫或表

這里跟全部復制不同的是主從服務器修改/etc/my.cnf配置的地方,這里只介紹不同的情況。

3.1 主服務器的配置

1 2 3 4 5 6 #在/etc/my.cnf配置文件: log-bin=mysql-bin # 啟動二進制日志系統 server-id=130 # 本機數據庫唯一ID log-bin=/var/log/mysql/updatelog?# 設定生成log文件名,這里的路徑沒有要手動創建并給它mysql用戶的權限。 binlog-do-db=test1 # 二進制需要同步的數據庫名 binlog-ignore-db=mysql,test?# 避免同步mysql用戶配置,以免不必要的麻煩

3.2 從服務器的配置

1 2 3 4 5 6 7 8 9 10 #修改/etc/my.cnf 文件: server-id=131 #從服務器ID號,不要和主ID相同 master-host=192.168.20.155 #指定主服務器IP地址 master-user=replication #指定在主服務器上可以進行同步的用戶名 master-password=123456 #密碼 master-port=3306 # 同步所用端口 master-connect-retry=60 #斷點從新連接時間 replicate-ignore-db=mysql #屏蔽對mysql庫的同步 replicate-do-db=test1 #同步的數據庫的名稱 replicate_do_table = testa #同步的數據表的名稱

replicate部分也可以這樣寫:

1 2 3 replicate-do-table=數據庫名.表名 #如果要是復制多個表只要在下面直接添加(復制幾個就添加幾個) replicate-do-table=數據庫名.表名

mysql replication單表或多表復制時需注意的幾個問題:

1.主庫和從庫的數據庫名必須相同;

2.主庫和從庫的復制可以精確到表,但是在需要更改主庫或從庫的數據結構時需要立刻重啟slave;

3.不能在mysql配置文件里直接寫入master的配置信息,需要用change master命令來完成;

4.指定replicate_do_db必須在my.ini里配置,不能用change master命令來完成;

5.如果不及時清理,日積月累二進制日志文件可能會把磁盤空間占滿,可以在配置文件里加上expire_logs_days=7,只保留最近7天的日志,建議當slave不再使用時,通過reset slave來取消relaylog;

上面幾條是在使用過程當中發現的律,不知道是否正確,有誤之處,還望指點。

最后,編寫一shell腳本,用nagios監控slave的兩個“yes”,如發現只有一個或零個“yes”,就表明主從有問題了,發短信警報吧。

詳情參考我的另一篇文章:nagios監控mysql主從復制

?

?

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

http://heylinux.com/archives/1004.html

Mysql作為目前世界上使用最廣泛的免費數據庫,相信所有從事系統運維的工程師都一定接觸過。但在實際的生產環境中,由單臺Mysql作為獨立的數據庫是完全不能滿足實際需求的,無論是在安全性,高可用性以及高并發等各個方面。

因此,一般來說都是通過 主從復制(Master-Slave)的方式來同步數據,再通過讀寫分離(MySQL-Proxy)來提升數據庫的并發負載能力 這樣的方案來進行部署與實施的。

如下圖所示:

下面是我在實際工作過程中所整理的筆記,在此分享出來,以供大家參考。

一、MySQL的安裝與配置
具體的安裝過程,建議參考我的這一篇文章:http://heylinux.com/archives/993.html
值得一提的是,我的安裝過程都是源碼包編譯安裝的,并且所有的配置與數據等都統一規劃到了/opt/mysql目錄中,因此在一臺服務器上安裝完成以后,可以將整個mysql目錄打包,然后傳到其它服務器上解包,便可立即使用。

二、MySQL主從復制
場景描述:
主數據庫服務器:192.168.10.130,MySQL已經安裝,并且無應用數據。
從數據庫服務器:192.168.10.131,MySQL已經安裝,并且無應用數據。

2.1 主服務器上進行的操作
啟動mysql服務
/opt/mysql/init.d/mysql start

通過命令行登錄管理MySQL服務器
/opt/mysql/bin/mysql -uroot -p'new-password'

授權給從數據庫服務器192.168.10.131
mysql> GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.10.131' identified by ‘password’;

查詢主數據庫狀態
Mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 261 | | |
+------------------+----------+--------------+------------------+

記錄下 FILE 及 Position 的值,在后面進行從服務器操作的時候需要用到。

2.2 配置從服務器
修改從服務器的配置文件/opt/mysql/etc/my.cnf
將 server-id = 1修改為 server-id = 10,并確保這個ID沒有被別的MySQL服務所使用。

啟動mysql服務
/opt/mysql/init.d/mysql start

通過命令行登錄管理MySQL服務器
/opt/mysql/bin/mysql -uroot -p'new-password'

執行同步SQL語句
mysql> change master to
master_host=’192.168.10.130’,
master_user=’rep1’,
master_password=’password’,
master_log_file=’mysql-bin.000005’,
master_log_pos=261;

正確執行后啟動Slave同步進程
mysql> start slave;

主從同步檢查
mysql> show slave status\G
==============================================
**************** 1. row *******************
Slave_IO_State:
Master_Host: 192.168.10.130
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 415
Relay_Log_File: localhost-relay-bin.000008
Relay_Log_Pos: 561
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: YES
Slave_SQL_Running: YES
Replicate_Do_DB:
……………省略若干……………
Master_Server_Id: 1
1 row in set (0.01 sec)
==============================================

其中Slave_IO_Running 與 Slave_SQL_Running 的值都必須為YES,才表明狀態正常。

如果主服務器已經存在應用數據,則在進行主從復制時,需要做以下處理:
(1)主數據庫進行鎖表操作,不讓數據再進行寫入動作
mysql> FLUSH TABLES WITH READ LOCK;

(2)查看主數據庫狀態
mysql> show master status;

(3)復制數據文件
將主服務器的數據文件(整個/opt/mysql/data目錄)復制到從服務器,建議通過tar歸檔壓縮后再傳到從服務器解壓。

(4)取消主數據庫鎖定
mysql> UNLOCK TABLES;

2.3 驗證主從復制效果

主服務器上的操作
在主服務器上創建數據庫first_db
mysql> create database first_db;
Query Ok, 1 row affected (0.01 sec)

在主服務器上創建表first_tb
mysql> create table first_tb(id int(3),name char(10));
Query Ok, 1 row affected (0.00 sec)

在主服務器上的表first_tb中插入記錄
mysql> insert into first_tb values (001,’myself’);
Query Ok, 1 row affected (0.00 sec)

在從服務器上查看
mysql> show databases;
=============================
+--------------------+
| Database |
+--------------------+
| information_schema |
| first_db |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.01 sec)
=============================
數據庫first_db已經自動生成

mysql> use first_db
Database chaged

mysql> show tables;
=============================
+--------------------+
| Tables_in_first_db |
+--------------------+
| first_tb |
+--------------------+
1 row in set (0.02 sec)
=============================
數據庫表first_tb也已經自動創建

mysql> select * from first_tb;
=============================
+------+------+
| id | name |
+------+------+
| 1 | myself |
+------+------+
1 rows in set (0.00 sec)
=============================
記錄也已經存在

由此,整個MySQL主從復制的過程就完成了,接下來,我們進行MySQL讀寫分離的安裝與配置。

三、MySQL讀寫分離
場景描述:
數據庫Master主服務器:192.168.10.130
數據庫Slave從服務器:192.168.10.131
MySQL-Proxy調度服務器:192.168.10.132

以下操作,均是在192.168.10.132即MySQL-Proxy調度服務器 上進行的。

3.1 MySQL的安裝與配置
具體的安裝過程與上文相同。

3.2 檢查系統所需軟件包
通過 rpm -qa | grep name 的方式驗證以下軟件包是否已全部安裝。
gcc* gcc-c++* autoconf* automake* zlib* libxml* ncurses-devel* libmcrypt* libtool* flex* pkgconfig*
libevent* glib*

若缺少相關的軟件包,可通過yum -y install方式在線安裝,或直接從系統安裝光盤中找到并通過rpm -ivh方式安裝。

3.3 編譯安裝lua
MySQL-Proxy的讀寫分離主要是通過rw-splitting.lua腳本實現的,因此需要安裝lua。

lua可通過以下方式獲得
從http://www.lua.org/download.html下載源碼包

從rpm.pbone.net搜索相關的rpm包
download.fedora.redhat.com/pub/fedora/epel/5/i386/lua-5.1.4-4.el5.i386.rpm
download.fedora.redhat.com/pub/fedora/epel/5/x86_64/lua-5.1.4-4.el5.x86_64.rpm

這里我們建議采用源碼包進行安裝
cd /opt/install
wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zvfx lua-5.1.4.tar.gz
cd lua-5.1.4

vi src/Makefile
在 CFLAGS= -O2 -Wall $(MYCFLAGS) 這一行記錄里加上-fPIC,更改為 CFLAGS= -O2 -Wall -fPIC $(MYCFLAGS) 來避免編譯過程中出現錯誤。

make linux
make install

cp etc/lua.pc /usr/lib/pkgconfig/
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/pkgconfig

3.4 安裝配置MySQL-Proxy
MySQL-Proxy可通過以下網址獲得:
http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/

推薦采用已經編譯好的二進制版本,因為采用源碼包進行編譯時,最新版的MySQL-Proxy對automake,glib以及libevent的版本都有很高的要求,而這些軟件包都是系統的基礎套件,不建議強行進行更新。
并且這些已經編譯好的二進制版本在解壓后都在統一的目錄內,因此建議選擇以下版本:
32位RHEL5平臺:
http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.1-linux-rhel5-x86-32bit.tar.gz
64位RHEL5平臺:
http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.1-linux-rhel5-x86-64bit.tar.gz

測試平臺為RHEL5 32位,因此選擇32位的軟件包
wget http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.1-linux-rhel5-x86-32bit.tar.gz

tar xzvf mysql-proxy-0.8.1-linux-rhel5-x86-32bit.tar.gz
mv mysql-proxy-0.8.1-linux-rhel5-x86-32bit /opt/mysql-proxy

創建mysql-proxy服務管理腳本
mkdir /opt/mysql-proxy/init.d/

vim mysql-proxy

#!/bin/sh # # mysql-proxy This script starts and stops the mysql-proxy daemon # # chkconfig: - 78 30 # processname: mysql-proxy # description: mysql-proxy is a proxy daemon to mysql# Source function library. . /etc/rc.d/init.d/functions#PROXY_PATH=/usr/local/bin PROXY_PATH=/opt/mysql-proxy/binprog="mysql-proxy"# Source networking configuration. . /etc/sysconfig/network# Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0# Set default mysql-proxy configuration. #PROXY_OPTIONS="--daemon" PROXY_OPTIONS="--admin-username=root --admin-password=password --proxy-read-only-backend-addresses=192.168.10.131:3306 --proxy-backend-addresses=192.168.10.130:3306 --admin-lua-script=/opt/mysql-proxy/lib/mysql-proxy/lua/admin.lua --proxy-lua-script=/opt/mysql-proxy/scripts/rw-splitting.lua" PROXY_PID=/opt/mysql-proxy/run/mysql-proxy.pid# Source mysql-proxy configuration. if [ -f /etc/sysconfig/mysql-proxy ]; then. /etc/sysconfig/mysql-proxy fiPATH=$PATH:/usr/bin:/usr/local/bin:$PROXY_PATH# By default it's all good RETVAL=0# See how we were called. case "$1" instart)# Start daemon.echo -n $"Starting $prog: "$NICELEVEL $PROXY_PATH/mysql-proxy $PROXY_OPTIONS --daemon --pid-file=$PROXY_PID --user=mysql --log-level=warning --log-file=/opt/mysql-proxy/log/mysql-proxy.logRETVAL=$?echoif [ $RETVAL = 0 ]; thentouch /var/lock/subsys/mysql-proxyfi;;stop)# Stop daemons.echo -n $"Stopping $prog: "killproc $progRETVAL=$?echoif [ $RETVAL = 0 ]; thenrm -f /var/lock/subsys/mysql-proxyrm -f $PROXY_PIDfi;;restart)$0 stopsleep 3$0 start;;condrestart)[ -e /var/lock/subsys/mysql-proxy ] && $0 restart;;status)status mysql-proxyRETVAL=$?;;*)echo "Usage: $0 {start|stop|restart|status|condrestart}"RETVAL=1;; esacexit $RETVAL

腳本參數詳解:
==============================================
PROXY_PATH=/opt/mysql-proxy/bin //定義mysql-proxy服務二進制文件路徑

PROXY_OPTIONS="--admin-username=root \ //定義內部管理服務器賬號
--admin-password=password \ //定義內部管理服務器密碼
--proxy-read-only-backend-addresses=192.168.10.131:3306 \ //定義后端只讀從服務器地址
--proxy-backend-addresses=192.168.10.130:3306 \ //定義后端主服務器地址
--admin-lua-script=/opt/mysql-proxy/lib/mysql-proxy/lua/admin.lua \ //定義lua管理腳本路徑
--proxy-lua-script=/opt/mysql-proxy/scripts/rw-splitting.lua" \ //定義lua讀寫分離腳本路徑

PROXY_PID=/opt/mysql-proxy/run/mysql-proxy.pid //定義mysql-proxy PID文件路徑

$NICELEVEL $PROXY_PATH/mysql-proxy $PROXY_OPTIONS \
--daemon \ //定義以守護進程模式啟動
--keepalive \ //使進程在異常關閉后能夠自動恢復
--pid-file=$PROXY_PID \ //定義mysql-proxy PID文件路徑
--user=mysql \ //以mysql用戶身份啟動服務
--log-level=warning \ //定義log日志級別,由高到低分別有(error|warning|info|message|debug)
--log-file=/opt/mysql-proxy/log/mysql-proxy.log //定義log日志文件路徑
==============================================

cp mysql-proxy /opt/mysql-proxy/init.d/
chmod +x /opt/mysql-proxy/init.d/mysql-proxy

mkdir /opt/mysql-proxy/run
mkdir /opt/mysql-proxy/log

mkdir /opt/mysql-proxy/scripts

配置并使用rw-splitting.lua讀寫分離腳本
最新的腳本我們可以從最新的mysql-proxy源碼包中獲取
cd /opt/install

wget http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.1.tar.gz

tar xzvf mysql-proxy-0.8.1.tar.gz
cd mysql-proxy-0.8.1
cp lib/rw-splitting.lua /opt/mysql-proxy/scripts

修改讀寫分離腳本rw-splitting.lua
修改默認連接,進行快速測試,不修改的話要達到連接數為4時才啟用讀寫分離
vim /opt/mysql-proxy/scripts/rw-splitting.lua
=============================
-- connection pool
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1, //默認為4
max_idle_connections = 1, //默認為8
is_debug = false
}
end
=============================

修改完成后,啟動mysql-proxy
/opt/mysql-proxy/init.d/mysql-proxy start

3.5 測試讀寫分離效果
創建用于讀寫分離的數據庫連接用戶
登陸主數據庫服務器192.168.10.130,通過命令行登錄管理MySQL服務器
/opt/mysql/bin/mysql -uroot -p'new-password'
mysql> GRANT ALL ON *.* TO 'proxy1'@'192.168.10.132' IDENTIFIED BY 'password';
由于我們配置了主從復制功能,因此從數據庫服務器192.168.10.131上已經同步了此操作。

為了清晰的看到讀寫分離的效果,需要暫時關閉MySQL主從復制功能
登陸從數據庫服務器192.168.10.131,通過命令行登錄管理MySQL服務器
/opt/mysql/bin/mysql -uroot -p'new-password'

關閉Slave同步進程
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

連接MySQL-Proxy
/opt/mysql/bin/mysql -uproxy1 -p'password' -P4040 -h192.168.10.132

登陸成功后,在first_db數據的first_tb表中插入兩條記錄
mysql> use first_db;
Database changed
mysql> insert into first_tb values (007,’first’);
Query Ok, 1 row affected (0.00 sec)
mysql> insert into first_tb values (110,’second’);
Query Ok, 1 row affected (0.00 sec)

查詢記錄
mysql> select * from first_tb;
=============================
+------+------+
| id | name |
+------+------+
| 1 | myself |
+------+------+
1 rows in set (0.00 sec)
=============================
通過讀操作并沒有看到新記錄

mysql> quit
退出MySQL-Proxy

下面,分別登陸到主從數據庫服務器,對比記錄信息
首先,檢查主數據庫服務器
mysql> select * from first_tb;
=============================
+------+------+
| id | name |
+------+------+
| 1 | myself |
+------+------+
| 007 | first |
+------+------+
| 110 | second |
+------+------+
3 rows in set (0.00 sec)
=============================
兩條新記錄都已經存在

然后,檢查從數據庫服務器
mysql> select * from first_tb;
=============================
+------+------+
| id | name |
+------+------+
| 1 | myself |
+------+------+
1 rows in set (0.00 sec)
=============================
沒有新記錄存在

由此驗證,我們已經實現了MySQL讀寫分離,目前所有的寫操作都全部在Master主服務器上,用來避免數據的不同步;
另外,所有的讀操作都分攤給了其它各個Slave從服務器上,用來分擔數據庫壓力。

經驗分享:
1.當MySQL主從復制在 show slave status\G 時出現Slave_IO_Running或Slave_SQL_Running 的值不為YES時,需要首先通過 stop slave 來停止從服務器,然后再執行一次本文 2.1與2.2 章節中的步驟即可恢復,但如果想盡可能的同步更多的數據,可以在Slave上將master_log_pos節點的值在之前同步失效的值的基礎上增大一些,然后反復測試,直到同步OK。因為MySQL主從復制的原理其實就是從服務器讀取主服務器的binlog,然后根據binlog的記錄來更新數據庫。

2.MySQL-Proxy的rw-splitting.lua腳本在網上有很多版本,但是最準確無誤的版本仍然是源碼包中所附帶的lib/rw-splitting.lua腳本,如果有lua腳本編程基礎的話,可以在這個腳本的基礎上再進行優化;

3.MySQL-Proxy實際上非常不穩定,在高并發或有錯誤連接的情況下,進程很容易自動關閉,因此打開--keepalive參數讓進程自動恢復是個比較好的辦法,但還是不能從根本上解決問題,因此通常最穩妥的做法是在每個從服務器上安裝一個MySQL-Proxy供自身使用,雖然比較低效但卻能保證穩定性;

4.一主多從的架構并不是最好的架構,通常比較優的做法是通過程序代碼和中間件等方面,來規劃,比如設置對表數據的自增id值差異增長等方式來實現兩個或多個主服務器,但一定要注意保證好這些主服務器數據的完整性,否則效果會比多個一主多從的架構還要差;

5.MySQL-Cluster 的穩定性也不是太好;

6.Amoeba for MySQL 是一款優秀的中間件軟件,同樣可以實現讀寫分離,負載均衡等功能,并且穩定性要大大超過MySQL-Proxy,建議大家用來替代MySQL-Proxy,甚至MySQL-Cluster。

總結

以上是生活随笔為你收集整理的MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。