maxscale讀寫分離

今天,寫寫mysql兄弟公司maridb開發的一個讀寫分離,既然是兄弟,那也適用於mysql。mysql

一、安裝依賴包

yum -y install gcc gcc-c++ ncurses ncurses-devel cmake
 

二、下載軟件

cd /usr/local
wget http://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.14.tar.gz

三、添加mysql用戶和用戶組

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

四、解壓安裝mysql

 
# 建立數據目錄
mkdir
-p /data/mysql
# 解壓boost
tar xzf boost_1_59_0.tar.gz

# 解壓mysql,編譯安裝
tar xzf mysql-5.7.14.tar.gz
cd mysql
-5.7.14 cmake . \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/data/mysql \ -DDOWNLOAD_BOOST=1 \ -DWITH_BOOST=/usr/local/boost_1_59_0 \ -DSYSCONFDIR=/etc \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DENABLED_LOCAL_INFILE=1 \ -DENABLE_DTRACE=0 \ -DDEFAULT_CHARSET=utf8mb4 \ -DDEFAULT_COLLATION=utf8mb4_general_ci \ -DWITH_EMBEDDED_SERVER=1

make make install
# 複製到啓動項 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql chmod +x /etc/init.d/mysql chkconfig --add mysql chkconfig mysql on
# 複製配置文件
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

# 安裝
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

五、添加環境變量

cd ~

vi .profile_bash

PATH=/usr/local/mysql/bin:$PATH

export PATH

六、mysql主從複製(利用gtid特性)

# 關閉selinux和防火牆
systemctl stop firewalld.service
vi /etc/selinux/config
# 配置每一個節點配置文件my.cnf # 添加以下 log
-bin=mysql-bin server-id=3 gtid_mode=ON log_slave_updates enforce_gtid_consistency (注:除了server-id每一個節點不同,其它都同樣,也必須加,以通常主從複製區別,多了下面的3個參數)
# 主節點添加複製帳號 grant replication slave on
*.* to 'backup'@'%'identified by'backup';
# 從節點執行 change master to master_host
='192.168.10.140', master_user='backup', master_password='backup',master_port=3306,master_auto_position=1; start slave;

 

七、maxscale安裝

安裝依賴
yum install libaio.x86_64 libaio-devel.x86_64 novacom-server.x86_64 libedit -y

MaxScale 的下載地址:linux

https://downloads.mariadb.com/files/MaxScale

rpm -ivh maxscale-beta-2.0.0-1.centos.7.x86_64.rpm

 

在開始配置以前,須要在 master和backup 中爲 MaxScale 建立兩個用戶,用於監控模塊和路由模塊。

建立監控用戶c++

mysql> create user scalemon@'%' identified by "123456";

mysql> grant replication slave, replication client on *.* to scalemon@'%';

建立路由用戶 git

mysql> create user scaleroute@'%' identified by "123456";

mysql> grant select on mysql.* to scaleroute@'%';

用戶建立完成後,開始配置github

vi /etc/maxscale.cnf
[root@maxscale1
~]# cat /etc/maxscale.cnf # MaxScale documentation on GitHub: # https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Documentation-Contents.md # Global parameters # # Complete list of configuration options: # https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Getting-Started/Configuration-Guide.md [maxscale] threads=1 log_info=1 logdir=/tmp/ # Server definitions # # Set the address of the server to the network # address of a MySQL server. # [server1] type=server address=192.168.10.140 port=3306 protocol=MySQLBackend [server2] type=server address=192.168.10.141 port=3306 protocol=MySQLBackend [server3] type=server address=192.168.10.142 port=3306 protocol=MySQLBackend # Monitor for the servers # # This will keep MaxScale aware of the state of the servers. # MySQL Monitor documentation: # https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Monitors/MySQL-Monitor.md [MySQL Monitor] type=monitor module=mysqlmon servers=server1,server2,server3 user=scalemon passwd=123456 monitor_interval=10000 # Service definitions # # Service Definition for a read-only service and # a read/write splitting service. # # ReadConnRoute documentation: # https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Routers/ReadConnRoute.md # [Read-Only Service] # type=service # router=readconnroute # servers=server1 # user=myuser # passwd=mypwd # router_options=slave # ReadWriteSplit documentation: # https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Routers/ReadWriteSplit.md [Read-Write Service] type=service router=readwritesplit servers=server1,server2,server3 user=scaleroute passwd=123456 max_slave_connections=100% # This service enables the use of the MaxAdmin interface # MaxScale administration guide: # https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Reference/MaxAdmin.md [MaxAdmin Service] type=service router=cli # Listener definitions for the services # # These listeners represent the ports the # services will listen on. # # [Read-Only Listener] # type=listener # service=Read-Only Service # protocol=MySQLClient # port=4008 [Read-Write Listener] type=listener service=Read-Write Service protocol=MySQLClient port=4006 [MaxAdmin Listener] type=listener service=MaxAdmin Service protocol=maxscaled socket=default

啓動sql

maxscale --config=/etc/maxscale.cnf
查看
注意:這裏用的是最新的2.0版本,登陸已經不像老版本maxadmin --user=admin --password=mariadb這樣了,而是像下面同樣:
[root@maxscale1 ~]# maxadmin -S /tmp/maxadmin.sock 
MaxScale> list services
Services.
--------------------------+----------------------+--------+---------------
Service Name              | Router Module        | #Users | Total Sessions
--------------------------+----------------------+--------+---------------
Read-Write Service        | readwritesplit       |      1 |     1
MaxAdmin Service          | cli                  |      2 |     4
--------------------------+----------------------+--------+---------------
命令也有變化,請自行FQ到Google查看
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 192.168.10.140  |  3306 |           0 | Master, Running
server2            | 192.168.10.141  |  3306 |           0 | Slave, Running
server3            | 192.168.10.142  |  3306 |           0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------
測試
在其它遠程客戶端鏈接maxscale所在服務器的數據庫
mysql -h 192.168.10.140 -P4006 -uscalemon -p
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| maxscale1  |
+------------+
1 row in set (0.00 sec)
 
關了一個slave後
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 192.168.10.140  |  3306 |           1 | Master, Running
server2            | 192.168.10.141  |  3306 |           1 | Slave, Running
server3            | 192.168.10.142  |  3306 |           0 | Down
-------------------+-----------------+-------+-------------+--------------------

參考,我是開啓FQ的數據庫

相關文章
相關標籤/搜索