集羣環境搭建配置文檔
MYSQL CLUSTER 7.3.6
網絡組申請的mysql集羣環境,Red Hat linux 6.3 64位操作系統,規劃4臺虛擬主機 配置: CPU 4核,內存6G,存儲100G
MYSQL CLUSTER 7.3.6 目前最新版本
一、節點分佈情況:
MGM:10.40.20.147 管理節點
NDBD1:10.40.20.148 存儲節點
NDBD2:10.40.20.149 存儲節點
SQL1:10.40.20.149 SQL節點
SQL2:10.40.20.150 SQL節點
MYsql數據庫集羣由三部分組成,管理節點、存儲節點和SQL節點。
管理節點是用來管理存儲節點和SQL節點的,存儲節點和SQL節點都要與管理節點通信;
存儲節點是用來負載均衡存儲數據,所用的存儲引擎爲NDB;
SQL節點是對外接口,用來與應用程序進行通信,應用程序只能訪問它;
配置文件有兩種:管理節點配置文件和普通節點配置文件,
config.ini是管理節點配置文件;my.cnf是數據節點和SQL節點配置文件。
config.ini文件中[ndb_mgmd]指明管理節點地址,[ndbd]指明數據節點地址,[mysqld]指明SQL節點地址。
二、下載安裝包:
PC端,先官網下載頁面 http://dev.mysql.com/downloads/cluster/ ,選擇 red hat linux 下面的 MySQL-Cluster-gpl-7.3.6-2.el6.x86_64.rpm-bundle.tar
壓縮文件,下載到本地。
三、關閉防火牆和selinux:
3.1關閉防火牆(在四臺服務器上操作)
[root@linux147 ~]# service iptables stop |
3.2默認不啓動防火牆(在四臺服務器上操作)
[root@linux147 ]# chkconfig iptables off |
關閉selinux
# 關閉 selinux
1 | vim /etc/selinux/config #(改爲SELINUX=disabled)保存退出, |
四、環境清理(在各節點服務器上都要執行)
清除MySQL舊版本:
首先使用如下命令來清理之前操作系統自帶的MySQL安裝:
1 | yum -y remove mysql |
|
然後使用如下命令:
1 | [root@localhost src]# rpm -qa | grep mysql* |
2 | mysql-libs-5.1.61-4.el6.i686 |
3 | apr-util-mysql-1.3.9-3.el6_0.1.i686 |
對於找到的2個剩餘MySQL包,按照如下的命令格式予以刪除:
[root@linux147 tmp]# rpm -e --nodeps mysql-libs-5.1.66-2.el6_3.x86_64
[root@linux147 tmp]# rpm -e --nodeps mod_auth_mysql-3.0.0-11.el6_0.1.x86_64
[root@linux147 tmp]# rpm -e --nodeps php-mysql-5.3.3-22.el6.x86_64
[root@linux147 tmp]# rpm -e --nodepmysql-connector-odbc-5.1.5r1144-7.el6.x86_64
[root@linux147 tmp]# rpm -e --nodeps rsyslog-mysql-5.8.10-6.el6.x86_64
[root@linux147 tmp]# rpm -e --nodeps qt-mysql-4.6.2-25.el6.x86_64
[root@linux147 tmp]# rpm -e --nodeps mysql-connector-java-5.1.17-6.el6.noarch
[root@linux147 tmp]# rpm -e --nodeps dovecot-mysql-2.0.9-5.el6.x86_64
五、軟件準備(在各臺服務器上都要執行)
1.將 mysql-cluster-gpl-7.3.6-2.el6.x86_64.rpm-bundle.tar上傳到各服務器的某個目錄下(如 /tmp ) 下面,然後解壓並移到 /tmp目錄下
1 | tar xvf MySql-Cluster-gpl-7.3.6-2.el6.x86_64.rpm-bundle.tar |
移動解壓內容到mysql目錄
[root@linux150 tmp]# mkdir /tmp/mysql
[root@linux150 tmp]# mv /tmp/MySQL*.rpm /tmp/mysql/
2 安裝 My Cluster:
[root@linux148 tmp]# cd mysql
每臺機器都安裝
[root@linux147 mysql]# rpm -ivh MySQL-Cluster-server-gpl-7.3.6-2.el6.x86_64.rpm
2014-09-11 22:43:48 9148 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-09-11 22:43:48 9148 [Note] InnoDB: The InnoDB memory heap is disabled
2014-09-11 22:43:48 9148 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-09-11 22:43:48 9148 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-09-11 22:43:48 9148 [Note] InnoDB: Using Linux native AIO
2014-09-11 22:43:48 9148 [Note] InnoDB: Using CPU crc32 instructions
2014-09-11 22:43:48 9148 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-09-11 22:43:48 9148 [Note] InnoDB: Completed initialization of buffer pool
2014-09-11 22:43:48 9148 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2014-09-11 22:43:48 9148 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2014-09-11 22:43:48 9148 [Note] InnoDB: Database physically writes the file full: wait...
2014-09-11 22:43:48 9148 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2014-09-11 22:43:48 9148 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2014-09-11 22:43:48 9148 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2014-09-11 22:43:48 9148 [Warning] InnoDB: New log files created, LSN=45781
2014-09-11 22:43:48 9148 [Note] InnoDB: Doublewrite buffer not found: creating new
2014-09-11 22:43:48 9148 [Note] InnoDB: Doublewrite buffer created
2014-09-11 22:43:48 9148 [Note] InnoDB: 128 rollback segment(s) are active.
2014-09-11 22:43:48 9148 [Warning] InnoDB: Creating foreign key constraint system tables.
2014-09-11 22:43:48 9148 [Note] InnoDB: Foreign key constraint system tables created
2014-09-11 22:43:48 9148 [Note] InnoDB: Creating tablespace and datafile system tables.
2014-09-11 22:43:48 9148 [Note] InnoDB: Tablespace and datafile system tables created.
2014-09-11 22:43:48 9148 [Note] InnoDB: Waiting for purge to start
2014-09-11 22:43:48 9148 [Note] InnoDB: 5.6.19 started; log sequence number 0
A random root password has been set. You will find it in '/root/.mysql_secret'.
2014-09-11 22:43:49 9148 [Note] Binlog end
2014-09-11 22:43:49 9148 [Note] InnoDB: FTS optimize thread exiting.
2014-09-11 22:43:49 9148 [Note] InnoDB: Starting shutdown...
2014-09-11 22:43:50 9148 [Note] InnoDB: Shutdown completed; log sequence number 1625977
2014-09-11 22:43:50 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2014-09-11 22:43:50 9171 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-09-11 22:43:50 9171 [Note] InnoDB: The InnoDB memory heap is disabled
2014-09-11 22:43:50 9171 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-09-11 22:43:50 9171 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-09-11 22:43:50 9171 [Note] InnoDB: Using Linux native AIO
2014-09-11 22:43:50 9171 [Note] InnoDB: Using CPU crc32 instructions
2014-09-11 22:43:50 9171 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-09-11 22:43:50 9171 [Note] InnoDB: Completed initialization of buffer pool
2014-09-11 22:43:50 9171 [Note] InnoDB: Highest supported file format is Barracuda.
2014-09-11 22:43:50 9171 [Note] InnoDB: 128 rollback segment(s) are active.
2014-09-11 22:43:50 9171 [Note] InnoDB: Waiting for purge to start
2014-09-11 22:43:50 9171 [Note] InnoDB: 5.6.19 started; log sequence number 1625977
2014-09-11 22:43:51 9171 [Note] Binlog end
2014-09-11 22:43:51 9171 [Note] InnoDB: FTS optimize thread exiting.
2014-09-11 22:43:51 9171 [Note] InnoDB: Starting shutdown...
2014-09-11 22:43:52 9171 [Note] InnoDB: Shutdown completed; log sequence number 1625987
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test database.
This is strongly recommended for production servers.
See the manual for more instructions.
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as /usr/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server
SQL節點安裝
[root@linux147 mysql]# rpm -ivh MySQL-Cluster-client-gpl-7.3.6-2.el6.x86_64.rpm
安裝完成後
[root@linux149 mysql]# chmod -R 1777 /usr/lib64/mysql
[root@linux149 mysql]# chmod -R 1777 /usr/bin/mysql
[root@linux149 mysql]# chmod -R 1777 /usr/share/mysql
# 重啓服務器
1 | init 6 |
五、配置節點
5.1 配置管理節點(10.40.20.147)
# 刪除自帶的 /etc/my.cnf 【切記:一定要刪除掉! ndb_mgmd啓動的時候,也會加上/etc/my.cnf的配置內容】
1 | rm -rf /etc/my.cnf |
# 新建配置文件存放目錄
1 | mkdir /usr/local/mysql/etc |
2 | chown mysql:mysql /usr/local/mysql/etc |
創建mysql用戶及用戶組
[root@linux147]# groupadd mysql
[root@linux147]#useradd -g mysql -s /usr/sbin/nologin mysql
[root@linux147]#mkdir /usr/local/mysql
[root@linux147 local]# chown -R mysql:mysql /usr/local/mysql
mysql用戶密碼:mysql
修改mysql的root密碼
[root@linux150 ~]# mysql -u root
mysql> set password for 'root'@'localhost'=password('root');
# 創建配置文件 config.ini
1 | vim /usr/local/mysql/etc/config.ini |
# config.ini內容如下:
[ndb_mgmd default]
datadir = /home/mysql/data
[ndbd default]
NoOfReplicas = 2
DataMemory = 3G
IndexMemory = 1G
datadir = /home/mysql/data
[ndb_mgmd]
NodeId = 1
HostName = 10.40.20.147
[ndbd]
NodeId = 11
HostName = 10.40.20.148
[ndbd]
NodeId = 12
HostName = 10.40.20.149
[mysqld]
NodeId = 21
HostName = 10.40.20.149
[mysqld]
NodeId = 22
HostName = 10.40.20.150
5.2 配置數據節點(10.40.20.148,10.40.20.149)
# 修改配置文件
1 | vim /etc/my.cnf |
# my.cnf內容如下:
[mysqld]
ndbcluster
ndb-connectstring = 10.40.20.147
[mysql_cluster]
ndb-connectstring = 10.40.20.147
5.3 配置SQL節點(10.40.20.149,10.40.20.150)
# 修改配置文件
1 | vim /etc/my.cnf |
# my.cnf內容如下:
[client]
Socket=/usr/lib64/mysql/mysql.sock
[mysqld]
Max_connections=100
socket=/usr/lib64/mysql/mysql.sock
datadir = /usr/lib64/mysql
ndbcluster
ndb-connectstring = 10.40.20.147
[mysql_cluster]
ndb-connectstring = 10.40.20.147
六、啓動節點
啓動需要按照如下順序進行:
Management Node > Data Node > SQL Node
6.1 啓動管理節點(10.40.20.147)
# --initial 首次加載、/usr/local/mysql/etc/config.ini文件有改變,其它時候不要加,除非是在備份、恢復或配置變化後重啓時,其他時候不要加,不然數據就清空。。。
啓動管理節點(10.40.20.147)
[root@linux147 /]# ndb_mgmd -f /usr/local/mysql/etc/config.ini --initial (首次加載 加參數--initial)
正常啓動
[root@linux147 /]# ndb_mgmd -f /usr/local/mysql/etc/config.ini
其他操作:
#查看ndb_mgmd是否啓動
[root@linux147 /]# ps -ef|grep ndb_mgmd
#ndb_mgmd默認啓動1186端口查看
[root@linux147 /]# netstat -ntlp |grep ndb_mgmd
查看集羣狀態
[root@linux147 /]# ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm> show
關閉管理節點:ndb_mgm -e shutdown (安全關閉管理節點和數據節點)
6.2 啓動數據節點(10.40.20.148,10.40.20.149)
# 只是在第一次啓動或在備份/恢復或配置變化後重啓ndbd時,才加–initial參數!
啓動數據節點(10.40.20.147,10.40.20.148)
ndbd -initial
#正常啓動方式
ndbd
#查看ndbd啓動狀態
ps-ef|grep ndbd
6.3 啓動SQL節點(10.40.20.149,10.40.150)
#啓動sql節點
root@linux149 mysql]# mysqld_safe --defaults-file=/etc/my.cnf --explicit_defaults_for_timestamp &
方法2:[root@linux149 mysql]# mysqld --defaults-file=/etc/my.cnf --user=root
[root@linux149 mysql]# /usr/bin/mysql_install_db --user=mysql
service mysql start
# 啓動 SQL 節點
1 | service mysqld start |
# 關閉 SQL 節點
1 | service mysqld stop |
# 重啓 SQL 節點
1 | service mysqld restart |
# 檢驗mysql是否運行
1 | service mysqld status |
# 爲sql指定密碼
1 | /usr/mysql/bin/mysqladmin -u root password 'new-password' |
2 |
|
3 | /usr/mysql/bin/mysqladmin -u root -h 'host' password 'new-password' |
# 啓動命令行窗口
1 | /usr/mysql/bin/mysql -u root –p |
--修改mysql root密碼方法:
mysqld_safe --user=mysql --skip-grant-tables --skip-networking&
Mysql -u root
Mysql> uses mysql;
Mysql>update user set password=password('root') where user='root';
mysql> flush privileges;
基於ndb的簡單集羣環境測試
創建數據庫
mysql> show databases;
mysql> create database bjf;
mysql> use bjf;
mysql> create table test1(id int ,name varchar(200)) engine=ndb;
Query OK, 0 rows affected (0.19 sec)
mysql> insert into test1 values(1,'aaa');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test1 values(2,'bbbba');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test1 values(3,'cccc');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test1;
+------+-------+
| id | name |
+------+-------+
| 1 | aaa |
| 3 | cccc |
| 2 | bbbba |
+------+-------+
3 rows in set (0.00 sec)
查看10.40.20.149和10.40.20.150 sql節點的數據庫信息完全一致。
注意事項:
1、事務處理(寫入,修改,刪除時超出範圍會提示)
MaxNoOfConcurrentOperations設置的值默認爲32768
可以通過修改config.ini
配套修改MaxNoOfLocalOperations值,建議該值爲前值的1.1倍
REPLICATION複製
在主mysql節點上,創建一個用戶
mysql> grant replication slave,replication client on *.* to 'slave'@'10.40.20.148' identified by 'slave';
mysql> grant file,select,replication slave on *.* to [email protected] identified by 'slave';
MY.CNF文件配置
(master)
[client]
socket=/usr/lib64/mysql/mysql.sock
user=root
password=root
[mysqld]
server-id=1
log-bin=mysqlmaster-bin.log
sync_binlog=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1
default-storage-engine=ndbcluster
max_connections=100
socket=/usr/lib64/mysql/mysql.sock
datadir=/usr/lib64/mysql
ndbcluster
ndb-connectstring=10.40.20.147
[mysql_cluster]
ndb-connectstring=10.40.20.147
(slave)
[client]
socket=/usr/lib64/mysql/mysql.sock
user=root
password=root
[mysqld]
server-id=2
log_bin=mysql-bin
relay_log=mysql-relay-bin
log_slave_updates=1
read_only=1
max_connections=100
socket=/usr/lib64/mysql/mysql.sock
datadir=/usr/lib64/mysql
ndbcluster
ndb-connectstring=10.40.20.147
[mysql_cluster]
ndb-connectstring=10.40.20.147
啓動slave
mysql> change master to master_host='10.40.20.147',
-> master_user='slave',
-> master_password='slave',
-> master_log_file='mysqlmaster-bin.000009',
-> master_log_pos=0;
用show slavestatus查看slave的設置是否正確。
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 10.40.20.147
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 4
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: No
Slave_SQL_Running: No
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: 4
Relay_Log_Space: 120
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: NULL
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: 0
Master_UUID:
Master_Info_File: /usr/lib64/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
mysql> show processlist \G
*************************** 1. row ***************************
Id: 1
User: system user
Host:
db: NULL
Command: Daemon
Time: 508
State: Waiting for ndbcluster to start
Info: NULL
*************************** 2. row ***************************
Id: 3
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
*************************** 3. row ***************************
Id: 5
User: system user
Host:
db: NULL
Command: Connect
Time: 69
State: Slave has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
3 rows in set (0.00 sec)
主從成功查看:
mysql> mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.40.20.147
Master_User: slave
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysqlmaster-bin.000015
Read_Master_Log_Pos: 1546
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 994
Relay_Master_Log_File: mysqlmaster-bin.000015
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: 1546
Relay_Log_Space: 1167
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: 1
Master_UUID: 82bb784a-4306-11e4-ab58-005056a1a2f1
Master_Info_File: /usr/lib64/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
Slave端出現了不同步情況後,先stop slave需要重新找到pos點和文件,mysql下修改master_log_file=
和master_log_pos=
再start slave
Show processlist 語句檢查i/o和sql線程的status字段值。
MYSQL-PROXY 讀寫分離測試
Mysql-proxy 實現讀寫分離。主數據庫處理事務性查詢、從數據庫處理select查詢。
生產上需要建立ha方式。
通過rw-splitting.lua腳本實現讀寫分離,穩定性欠佳,在高併發或有錯誤連接的情況下,進程很容易自動關閉,keepalive參數進程進行自動恢復,可以在每個服務器上安裝一個提供自身使用,效率低。
Mysql-proxy安裝在10.40.20.149上
安裝:通過源碼方式安裝
安裝後 /OPT/MYSQL-PROXY 配置etc/mysql-proxy.cnf 文件
[mysql-proxy]
admin-username=root
admin-password=root
daemon=true
keepalive=true
log-file=/opt/mysql-proxy/log
log-level=debug
proxy-backend-addresses=10.40.20.147:3306
proxy-read-only-backend-address=10.40.20.148:3306
proxy-lua-script=/opt/mysql-proxy/scripts/rw-splitting.lua
admin-lua-script=/opt/mysql-proxy/scripts/admin.lua
配置mysql-proxy.sh啓動腳本
#!/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/bin
prog="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="--proxy-read-only-backend-addresses=10.40.20.148:3306 --proxy-backend-addresses=10.40.20.147:3306 --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
fi
PATH=$PATH:/usr/bin:/usr/local/bin:$PROXY_PATH
# By default it's all good
RETVAL=0
# See how we were called.
case "$1" in
start)
# Start daemon.
echo -n $"Starting $prog: "
$NICELEVEL $PROXY_PATH/mysql-proxy $PROXY_OPTIONS --daemon --user=mysql --pid-file=$PROXY_PID --log-level=warning --log-file=/opt/mysql-proxy/log/mysql-proxy.log
RETVAL=$?
echo
if [ $RETVAL = 0 ]; then
touch /var/lock/subsys/mysql-proxy
fi
;;
stop)
# Stop daemons.
echo -n $"Stopping $prog: "
killproc $prog
RETVAL=$?
echo
if [ $RETVAL = 0 ]; then
rm -f /var/lock/subsys/mysql-proxy
rm -f $PROXY_PID
fi
;;
restart)
$0 stop
sleep 3
$0 start
;;
condrestart)
[ -e /var/lock/subsys/mysql-proxy ] && $0 restart
;;
status)
status mysql-proxy
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|restart|status|condrestart}"
RETVAL=1
;;
esac
exit $RETVAL
讀寫分離腳本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.sh start
測試過程:
主服務器10.40.20.147,授權給10.40.20.149
Mysql>grant all on *.* to ‘root’@’10.40.20.149’ identified by ‘root’
登錄slave,關閉slave同步進程
Stop slave;
遠程連接到mysql-proxy服務器
Mysql -uroot -proot -P4040 -h 10.40.20.149
Mysql> use test;
Insert into test(name) values(‘bjf’);
Insert into test(name) values(‘bjf02’);
退出後在登錄,執行查詢
Mysql>select * from test;
無法看到剛纔插入的記錄。
再登錄主服務器10.40.20.147
Mysql>select * from test;
可以看到新加的記錄
登錄到slave服務器
Mysql>select *from test;
無法查看到新加記錄。
由此驗證,已經實現了mysql的讀寫分離,所有的寫操作在10.40.20.147,所有的讀操作在10.40.20.148 ,分擔主服務器壓力。
Amoeba (變形蟲)mysql的讀寫分離
目前還不支持事務
暫時不支持存儲過程(近期會支持)
不適合從amoeba導數據的場景或者對大數據量查詢的query並不合適(比如一次請求返回10w以上甚至更多的數據場合)
暫時不支持分庫分表,目前只能做到分數據庫實例,每個被切分的節點需要保持庫結構一致
具有負載均衡、高可用性、sql過濾、讀寫分離、可路由相關的query到達目標庫,可併發請求多臺數據庫合併結果。
主要解決:
降低數據切分帶來複雜多數據庫結構
提供切分規則並降低數據切分規則給應用帶來的影響
降低db與客戶端的連接數
讀寫分離
Jdk 安裝,最好是1.6及1.7版本
1、設置etc/profile文件,增加java_home 利用find / 查找出.dt.jar路徑
JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64
PAHT=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
啓用
[root@linux150 bin]# ./launcher
./launcher: line 11: JAVA_HOME: command not found
2014-09-28 23:10:43 [INFO] Project Name=Amoeba-MySQL, PID=8652 , starting...
log4j:WARN log4j config load completed from file:/tmp/amoeba-mysql-3.0.5-RC/conf/log4j.xml
2014-09-28 23:10:44,834 INFO context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-3.0.4-BETA
log4j:WARN ip access config load completed from file:/tmp/amoeba-mysql-3.0.5-RC/conf/access_list.conf
2014-09-28 23:10:45,207 INFO net.ServerableConnectionManager - Server listening on 0.0.0.0/0.0.0.0:8066.
主從服務器授權給amoeba服務器
Mysql>grant all on *.* to 'root'@10.40.20.149 identified by 'root';
(主從都需要設置)
連接到amoeba服務器:
[root@linux148 ~]# mysql -uroot -proot -h10.40.20.150 -P 8066
連接到10.40.20.148 slave機器:
Stop slave ;關閉複製
從10.40.20.148連接到150的amoeba服務器
Use test;
Insert into test(name) values(‘0000’);
Select * from test;
再到10.40.20.148 mysql查看
Select * from test; 無插入數據
再到10.40.20.147 master
基於amoeba的數據垂直切分和數據水平切分
擴展性極強