MySQL是开源的关系型数据库系统。
复制(Replication)是从一台MySQL数据库服务器(主服务器master)复制数据到另一个服务器(从服务器slave)的一个进程
MySQL Proxy有一项强大功能是实现“读写分离”,基本原理是让主数据库处理写方面事务,让从库处理SELECT查询。
实验环境
CentOS6.8_x64
mysql_master 192.168.1.109
mysql_slave 192.168.1.111
mysql_proxy 192.168.1.117
实验软件
cmake-3.7.2.tar.gz
mysql-5.6.24.tar.gz
mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
Atlas-2.2.1.el6.x86_64.rpm
软件安装
yum install -y ncurses-devel auto-conf openssh-clients lua lrzsz gcc make wget lsof
cp -pv /etc/hosts /etc/hosts.bak
echo "服务器ip 服务器主机名" > /etc/hosts master/slave端操作相同
tar zxvf cmake-3.7.2.tar.gz
cd cmake-3.7.2
./bootstrap && gmake && gmake install
groupadd mysql && useradd -g mysql mysql
tar zxvf mysql-5.6.24.tar.gz
cd mysql-5.6.24
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DMYSQL_USER=mysql \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_DEBUG=0 \
-DMYSQL_MAINTAINER_MODE=0 \
-DWITH_SSL:STRING=bundled \
-DWITH_ZLIB:STRING=bundled \
make -j4 && make install
chown -R mysql:mysql /usr/local/mysql/
cp -pv /root/mysql-5.6.24/support-files/my-default.cnf /etc/my.cnf
cp -pv /root/mysql-5.6.24/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld && chkconfig --level 35 mysqld on
cp -pv /etc/my.cnf /etc/my.cnf.bak
[mysqld]
server-id=1 master端配置
log-bin=mysql-bin master/slave端配置
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql & 初始化数据库
service mysqld start 启动数据库
cp -pv /etc/profile /etc/profile.bak
echo 'PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
source /etc/profile
mysql --version
mysql Ver 14.14 Distrib 5.6.24
mysqladmin -uroot password 数据库密码
mysql -uroot -p 数据库密码
mysql> 以上操作master slave端都是一致
scp /root/cmake-3.7.2.tar.gz mysql-5.6.24.tar.gz [email protected]:/root/
cd /usr/local/
tar czvf mysql.tar.gz mysql && scp /usr/local/mysql.tar.gz [email protected]:/root/ server端操作
scp /etc/my.cnf [email protected]:/etc/my.cnf master端操作
scp /etc/init.d/mysqld [email protected]:/etc/my.cnf master端操作
sed -i "s/server_id = 1/server_id = 2/g" /etc/my.cnf slave端操作
mysql -uroot -p
mysql> grant replication slave on *.* to 'root'@'192.168.1.111' identified by '123456'; 192.168.1.111为slave ip
mysql> grant all on *.* to 'proxy'@'192.168.1.181' identified by '123.com'; 创建proxy共享账号
mysql> flush privileges;
mysql> show master status;
| mysql-bin.000001 | 323 | master端操作
mysql -uroot -p slave端操作
mysql> stop slave;
mysql> reset slave;
mysql> change master to
-> master_host='192.168.1.109', master端ip
-> master_user='root', master端数据库用户名
-> master_password='123456', master端数据库密码
-> master_log_file='mysql-bin.000001',
-> master_log_pos=323; 这两个文件按照master端实际情况而定,次操作为slave端
mysql> start slave;
mysql> show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes 这两行必须是yes实现主从复制
测试主从同步
mysql> create database test_1 master端操作
mysql> show databases;
| test_1 | master端
mysql> show databases;
| test_1 | slave端
tar zxvf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
mv mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-proxy
mkdir -pv /usr/local/mysql-proxy/{lua,logs}
touch /usr/local/mysql-proxy/logs/mysql-proxy.log
cp -pv /usr/local/mysql-proxy/share/doc/mysql-proxy/{rw-splitting.lua,admin-sql.lua} /etc/lua
cp -pv /usr/local/mysql-proxy/share/doc/mysql-proxy/{rw-splitting.lua,admin-sql.lua} /usr/local/mysql-proxy/lua/
cat > /etc/mysql-proxy.cnf << EOF 默认没有配置文件需要手动创建
> [mysql-proxy]
> user=root 运行mysql-proxy用户
> admin-username=proxy 主从mysql共享用户
> admin-password=123.com mysql共享用户密码
> proxy-address=192.168.1.117:4040 mysql-proxy server ip 端口号默认4040
> proxy-read-only-backend-addresses=192.168.1.111 mysql_slave端 ip
> proxy-backend-addresses=192.168.1.109 mysql_master端ip
> proxy-lua-script=/usr/local/mysql-proxy/lua/rw-splitting.lua 指定读写分离脚本位置
> admin-lua-script=/usr/local/mysql-proxy/lua/admin-sql.lua 指定管理脚本
> log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log 指定日志文件位置
> log-level=info log日志级别,由高到低分别有(error|warning|info|message|debug)
> daemon=true 以守护进程方式运行
> keeplive=true mysql-proxy 崩溃时,尝试重启
> EOF
chmod 660 /etc/mysql-proxy.cnf
chown -R root:root /usr/local/mysql-proxy/
cp -pv /usr/local/mysql-proxy/lua/rw-splitting.lua /usr/local/mysql-proxy/lua/rw-splitting.lua.bak
sed -i "s/min_idle_connections = 4,/min_idle_connections = 1,/g" /usr/local/mysql-proxy/lua/rw-splitting.lua
sed -i "s/max_idle_connections = 8,/max_idle_connections = 1,/g" /usr/local/mysql-proxy/lua/rw-splitting.lua
ln -s /usr/local/mysql-proxy/bin/mysql-proxy /usr/bin/mysql-proxy
mysql-proxy --daemon --defaults-file=/etc/mysql-proxy.cnf & 启动服务
cp -pv /etc/rc.d/rc.local /etc/rc.d/rc.local.bak
echo "mysql-proxy --daemon --defaults-file=/etc/mysql-proxy.cnf &" >> /etc/rc.d/rc.local 设置开机启动
killall -9 mysql-proxy 杀死进程
netstat -tuplna | grep 4040
tcp 0 0 192.168.10.181:4040 0.0.0.0:* LISTEN 47402/mysql-proxy
netstat -tuplna | grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 22233/mysqld
tcp 0 0 192.168.1.109:3306 192.168.1.111:41406 ESTABLISHED 22233/mysqld
tail -f /usr/local/mysql-proxy/logs/mysql-proxy.log
2019-01-22 11:02:24: (critical) plugin proxy 0.8.5 started
2019-01-22 11:02:24: (message) proxy listening on port 192.168.1.117.:4040
2019-01-22 11:02:24: (message) added read/write backend: 192.168.1.111
2019-01-22 11:02:24: (message) added read-only backend: 192.168.10.109
mysql -uproxy -h192.168.1.109 -p4040 -p123.com proxy代理服务器用户名 -h master_mysql ip -p proxy密码,测试如果可以连接说明配置成功
root用户用于实验,生产环境不可做此操作
Atlas实现数据库读写分离
rpm -ivh /root/Atlas-2.2.1.el6.x86_64.rpm
/usr/local/mysql-proxy/bin/encrypt 123.com 加密密码
tF5TeinkMj8=
cat /usr/local/mysql-proxy/conf/test.cnf
[mysql-proxy]
admin-username=atlas
admin-password=atlas
proxy-backend-addresses=192.168.1.109:3306
proxy-read-only-backend-addresses=192.168.1.111:3306
pwds=root:tF5TeinkMj8=
daemon=true
keepalive=true
event-threads=90
log-level=message
log-path=/usr/local/mysql-proxy/log
sql-log=ON
#sql-log-slow = 10
#instance = test
proxy-address=0.0.0.0:3307
admin-address=0.0.0.0:2345
#tables = person.mt.id.3
charset=utf8
#client-ips = 127.0.0.1, 192.168.1
#lvs-ips = 192.168.1.1
client-ips=
ln -s /usr/local/mysql-proxy/bin/mysql-proxyd /bin/
mysql-proxyd test start/restart/stop
mysql -u atlas -patlas -h127.0.0.1 -P2345 -P为大写
mysql> select * from backends;
+-------------+--------------------+-------+------+
| backend_ndx | address | state | type |
+-------------+--------------------+-------+------+
| 1 | 192.168.1.109:3306 | up | rw |
| 2 | 192.168.1.111:3306 | up | ro |
+-------------+--------------------+-------+------+
netstat -tuplna | grep 2345
tcp 0 0 0.0.0.0:2345 0.0.0.0:* LISTEN 2294/mysql-proxy
netstat -tuplna | grep 3307
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 2294/mysql-prox