LAMP读写分离

1、下载MYSQL-Proxy软件版本,解压并重命名至/usr/local/mysql-proxy,命令如下:

wget http://ftp.ntu.edu.tw/pub/MySQL/Downloads/MySQL-Proxy/mysql-proxy-0.8.4-linux-el6-x86-64bit.tar.gz 
useradd  -r  mysql-proxy
tar  zxvf  mysql-proxy-0.8.4-linux-el6-x86-64bit.tar.gz  -C  /usr/local
mv  /usr/local/mysql-proxy-0.8.4-linux-el6-x86-64bit  /usr/local/mysql-proxy

2、环境变量配置文件/etc/profile中加入如下代码保存退出,然后执行source /etc/profile使环境变量配置生效即可:

export  PATH=$PATH:/usr/local/mysql-proxy/bin/

3、 启动MYSQL-Proxy中间件,命令如下:

mysql-proxy --daemon --log-level=debug --user=mysql-proxy --keepalive --log-file=/var/log/mysql-proxy.log --plugins="proxy" --proxy-backend-addresses="192.168.1.10:3306" --proxy-read-only-backend-addresses="192.168.1.11:3306" --proxy-lua-script="/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua" --plugins=admin --admin-username="admin" --admin-password="admin" --admin-lua-script="/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua"

4 MYSQL-Proxy启动后,在服务器端查看端口,其中4040为proxy代理端口用于WEB应用连接,4041位管理端口用于SA或者DBA管理

netstat -ntnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:4040            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:4041            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     

5、基于4041端口MySQL-Proxy查看读写分离状态,登录4041管理端口,命令如下:

mysql  -h192.168.1.12  -uadmin  -padmin  -P 4041

6、以4041管理口登录,然后执行select命令,如图12-18所示state均为up状态,type类型为rw、ro,则证明读写分离状态成功。如果状态为unknown未知状态,可以4040端口登录执行:show databases;命令,直到state变成up状态为止

MySQL [(none)]> select  *  from  backends;
+-------------+-------------------+---------+------+------+-------------------+
| backend_ndx | address           | state   | type | uuid | connected_clients |
+-------------+-------------------+---------+------+------+-------------------+
|           1 | 192.168.1.10:3306 | up      | rw   | NULL |                 0 |
|           2 | 192.168.1.11:3306 | unknown | ro   | NULL |                 0 |
+-------------+-------------------+---------+------+------+-------------------+
# 可见192.168.1.11(从数据库)为unknown,在从数据库以4040登录执行show database;知道状态为up

mysql  -h192.168.1.12  -uroot  -p123456  -P 4040
若登陆不进可直接登录本地数据库创建一个用户供登录(MariaDB [(none)]> grant all on *.* to proxy@'%' identified by 'proxy123';)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| discuz             |
| mysql              |
| performance_schema |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)**

# 多执行几次即可发现状态从unknown转变为up
[root@localhost ~]#  mysql -h 192.168.1.12 -uadmin -padmin -P4041
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> sELECT * FROM backends;
+-------------+-------------------+-------+------+------+-------------------+
| backend_ndx | address           | state | type | uuid | connected_clients |
+-------------+-------------------+-------+------+------+-------------------+
|           1 | 192.168.1.10:3306 | up    | rw   | NULL |                 0 |
|           2 | 192.168.1.11:3306 | up    | ro   | NULL |                 0 |
+-------------+-------------------+-------+------+------+-------------------+
2 rows in set (0.01 sec)

7、 读写分离数据测试,以3306端口登录到从库,进行数据写入和测试,在丛库上创建jfedu_test测试库,并写入内容

[root@localhost ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database wrzx;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> use wrzx;
Database changed
MariaDB [wrzx]> create table t1(id char(20),name char(20));
Query OK, 0 rows affected (0.04 sec)

MariaDB [wrzx]> insert into t1 values (01,'wrzx.net');
Query OK, 1 row affected (0.00 sec)

#读写分离数据测试,以4040代理端口登录,执行如下命令,可以查看到数据即证明读写分离成功。
[root@localhost ~]# mysql -h 192.168.1.12 -uproxy -pproxy123 -P4040
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> select * from wrzx.t1;
+------+----------+
| id   | name     |
+------+----------+
| 1    | wrzx.net |
+------+----------+
1 row in set (0.01 sec)

登录Apache WEB服务器,修改Discuz PHP网站发布/usr/local/apache2/htdcos目录全局配置文件config_global.php,查找dbhost段,将192.168.1.10 改成192.168.1.12:40404

[root@localhost ~]# vim /var/www/html/config/config_global.php 
 6 // ----------------------------  CONFIG DB  ----------------------------- //
 7 $_config['db']['1']['dbhost'] = '192.168.1.12:4040';
 8 $_config['db']['1']['dbuser'] = 'proxy';
 9 $_config['db']['1']['dbpw'] = 'proxy123';

你可能感兴趣的:(Linux,数据库,linux,mysql)