MySQL-Proxy读写分离

阅读: 587 | 文章分类: Databases | Tag: mysql-proxy   MySQL  

# 环境申明 操作系统:CentOS7.5.1804(最小化安装) 软件: mariadb mysql-proxy 0.8.5 网络: Server-1 mysql-proxy 10.0.10.1 Server-2 mariadb(主) 10.0.10.2 Server-3 mariadb(从) 10.0.10.3 Server-4 mariadb(客户端) 10.0.10.4 > 实验前请将防火墙全部关闭 # 相关链接 [mysql-proxy官网](https://proxysql.com/) [mysql官网proxy下载](https://downloads.mysql.com/archives/proxy/) [mysql-proxy下载地址](https://cdn.mysql.com/archives/mysql-proxy/mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz) # MySQL主库 Server-2服务器是主库,所以要安装mariadb ``` [root@Server-2 ~]# yum -y install mariadb-server ``` 配置主库 ``` # 编辑my.cnf文件, [root@Server-2 ~]# vim /etc/my.cnf [mysqld] server-id=1 # 新增server id log-bin=mysql-bin # 新增开启二进制日志 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d ``` 启动数据库 ``` [root@Server-2 ~]# systemctl start mariadb ``` 设置主从读写 ``` [root@Server-2 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.60-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)]> GRANT REPLICATION SLAVE ON *.* to 'sync'@'10.0.10.%' identified by '123123'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 395 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) MariaDB [(none)]> ``` # MySQL从库 Server-3服务器是从库,所以也要安装mariadb ``` [root@Server-3 ~]# yum -y install mariadb-server ``` 配置从库 ``` [mysqld] server-id=2 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d ``` 启动服务 ``` [root@Server-3 ~]# systemctl start mariadb ``` 进入数据库中配置slave ``` [root@Server-3 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 5.5.60-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)]> change master to -> master_host='10.0.10.2', -> master_user='sync', -> master_password='123123', -> master_log_file='mysql-bin.000005', -> master_log_pos=245; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.01 sec) ``` 查看主从状态 ``` MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.10.2 Master_User: sync Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 395 Relay_Log_File: mariadb-relay-bin.000004 Relay_Log_Pos: 529 Relay_Master_Log_File: mysql-bin.000003 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: 395 Relay_Log_Space: 825 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 1 row in set (0.00 sec) ``` # 验证主从 主库中查看所有数据库 ``` MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) ``` 从库中查看所有数据库 ``` MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) ``` 在出库中创建数据库 ``` MariaDB [(none)]> create database gabe; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | gabe | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) ``` 在从库中查看所有数据库 ``` MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | gabe | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) ``` 添加root用户允许所有人可以链接 ``` MariaDB [(none)]> GRANT ALL ON *.* TO 'root'@'10.0.10.%'; Query OK, 0 rows affected (0.00 sec) ``` # 配置mysql-proxy 下载mysql-proxy ``` [root@Server-1 ~]# wget https://cdn.mysql.com/archives/mysql-proxy/mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz ``` 解压 ``` [root@Server-1 ~]# mv mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz /usr/local/ [root@Server-1 ~]# cd /usr/local/ [root@Server-1 local]# tar xf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz [root@Server-1 local]# mv mysql-proxy-0.8.5-linux-el6-x86-64bit mysql-proxy ``` 启动mysql-peoxy ``` [root@Server-1 local]# /usr/local/mysql-proxy/bin/mysql-proxy -P 10.0.10.1:3306 -b 10.0.10.2:3306 -r 10.0.10.3:3306 --daemon -s /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua [root@Server-1 local]# 2019-01-05 17:40:34: (critical) plugin proxy 0.8.5 started ``` 查看端口 ``` [root@Server-1 local]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 10.0.10.1:3306 0.0.0.0:* LISTEN 1874/mysql-proxy tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1024/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1198/master tcp6 0 0 :::22 :::* LISTEN 1024/sshd tcp6 0 0 ::1:25 :::* LISTEN 1198/master ``` # 验证 在server-4服务器上安装mysql客户端 ``` [root@Server-4 ~]# yum -y install mariadb ``` 登陆server-1服务器进行验证 ``` [root@Server-4 ~]# mysql -uroot -h10.0.10.1 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 6 Server version: 5.5.60-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)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | gabe | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.01 sec) MariaDB [(none)]> ``` > 关于读写分离这块我也不好做验证,所以大家可以自行写入数据 # 附录:mysql-proxy 关于mysql-proxy的详细参数说明,可以使用--help-all查看 ``` [root@Server-1 local]# /usr/local/mysql-proxy/bin/mysql-proxy --help-all Usage: mysql-proxy [OPTION...] - MySQL Proxy Help Options: -?, --help Show help options --help-all Show all help options --help-proxy Show options for the proxy-module proxy-module -P, --proxy-address=<host:port> listening address:port of the proxy-server (default: :4040) -r, --proxy-read-only-backend-addresses=<host:port> address:port of the remote slave-server (default: not set) -b, --proxy-backend-addresses=<host:port> address:port of the remote backend-servers (default: 127.0.0.1:3306) --proxy-skip-profiling disables profiling of queries (default: enabled) --proxy-fix-bug-25371 fix bug #25371 (mysqld > 5.1.12) for older libmysql versions -s, --proxy-lua-script=<file> filename of the lua script (default: not set) --no-proxy don't start the proxy-module (default: enabled) --proxy-pool-no-change-user don't use CHANGE_USER to reset the connection coming from the pool (default: enabled) --proxy-connect-timeout connect timeout in seconds (default: 2.0 seconds) --proxy-read-timeout read timeout in seconds (default: 8 hours) --proxy-write-timeout write timeout in seconds (default: 8 hours) Application Options: -V, --version Show version --defaults-file=<file> configuration file --verbose-shutdown Always log the exit code when shutting down --daemon Start in daemon-mode --user=<user> Run mysql-proxy as user --basedir=<absolute path> Base directory to prepend to relative paths in the config --pid-file=<file> PID file in case we are started as daemon --plugin-dir=<path> path to the plugins --plugins=<name> plugins to load --log-level=(error|warning|info|message|debug) log all messages of level ... or higher --log-file=<file> log all messages in a file --log-use-syslog log all messages to syslog --log-backtrace-on-crash try to invoke debugger on crash --keepalive try to restart the proxy if it crashed --max-open-files maximum number of open files (ulimit -n) --event-threads number of event-handling threads (default: 1) --lua-path=<...> set the LUA_PATH --lua-cpath=<...> set the LUA_CPATH ```