mysql-proxy读写分离
实验环境:
1.安装apache服务(192.168.2.131)
yum install openssl-devel
[root@LAP ~]# tar zxvf httpd-2.2.25.tar.gz -C /usr/src/
[root@LAP ~]# cd /usr/src/httpd-2.2.25/
[root@LAP httpd-2.2.25]# ./configure --prefix=/usr/local/httpd --enable-so --enable-cgi --enable-rewrite --enable-ssl
root@LAP httpd-2.2.25]# make
root@LAP httpd-2.2.25]# make install
[root@LAP httpd-2.2.25]# /usr/local/httpd/bin/apachectl start
2.部署mysql数据库(单独机器部署/ps:两台数据库为master:192.168.2.134;slave:192.168.2.135)
[root@mysql-master ~]# yum install -y gcc gcc-c++ glibc glibc-devel cmake ncurses-devel ncurses
[root@mysql-master ~]# tar zxvf mysql-5.5.38.tar.gz -C /usr/src/
[root@mysql-master ~]# cd /usr/src/mysql-5.5.38/
[root@mysql-master mysql-5.5.38]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock
-DMYSQL_DATADIR=/data/mysql
-DSYSCONFDIR=/etc -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
-DWITH_XTRADB_STORAGE_ENGINE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_PARTITION_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_MYISAM_STORAGE_ENGINE=1
-DWITH_READLINE=1
-DENABLED_LOCAL_INFILE=1
-DWITH_EXTRA_CHARSETS=1
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DEXTRA_CHARSETS=all
-DWITH_BIG_TABLES=1 -DWITH_DEBUG=0
[root@mysql-master mysql-5.5.38]# make
[root@mysql-master mysql-5.5.38]# make install
[root@mysql-master mysql-5.5.38]# useradd -M -s /sbin/nologin mysql
[root@mysql-master mysql-5.5.38]# chown -R mysql.mysql /usr/local/mysql/
[root@mysql-master mysql-5.5.38]# cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf
[root@mysql-master mysql-5.5.38]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
[root@mysql-master mysql-5.5.38]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql-master mysql-5.5.38]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@mysql-master mysql-5.5.38]# source /etc/profile
[root@mysql-master mysql-5.5.38]# chkconfig --add mysqld
[root@mysql-master mysql-5.5.38]# chkconfig mysqld on
[root@mysql-master mysql-5.5.38]# vim /etc/my.cnf
[root@mysql-master mysql-5.5.38]# /etc/init.d/mysqld start
[root@mysql-master mysql-5.5.38]# mysqladmin -u root password '123.com'
[root@mysql-master mysql-5.5.38]# mysql -u root -p
2.1:mysql-master端
2.2:mysql-slave端
###########mysql正常启动##################
3.安装php服务(ps:与apache同台机器)
[root@LAP ~]# yum install -y zlib-devel libxml2-devel
[root@LAP ~]# tar zxvf php-5.3.28.tar.gz -C /usr/src/
[root@LAP ~]# cd /usr/src/php-5.3.28/
[root@LAP php-5.3.28]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-apxs2=/usr/local/httpd/bin/apxs
[root@LAP php-5.3.28]# make -j8
[root@LAP php-5.3.28]# make -j8 install
[root@LAP ~]# vim /usr/local/httpd/conf/httpd.conf
[root@LAP ~]# vim /usr/local/httpd/htdocs/index.php
[root@LAP ~]# /usr/local/httpd/bin/apachectl restart
验证php与apache是否整合
#######################mysql主从####################
开启master端bin-log日志功能
[root@mysql-master mysql-5.5.38]# vim /etc/my.cnf
[root@mysql-master mysql-5.5.38]# /etc/init.d/mysqld restart
Shutting down MySQL. [确定]
Starting MySQL.. [确定]
[root@mysql-master mysql-5.5.38]# mysql -u root -p
mysql> grant replication slave on *.* to 'slave'@'192.168.2.135' identified by '123456'; #主从复制授权
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
mysql-slave端
[root@mysql-slave mysql-5.5.38]# vim /etc/my.cnf
[root@mysql-slave mysql-5.5.38]# /etc/init.d/mysqld restart
mysql> change master to
-> master_host='192.168.2.134',
-> master_user='slave',
-> master_password='123456',
-> master_log_file='mysql-bin.000004',
-> master_log_pos=630;
Query OK, 0 rows affected (0.03 sec)
mysql> slave start;
mysql> show slave status\G
mysql主从同步完成!!!!!接下来实现mysql-proxy实现读写分离
mysql-proxy端(192.168.2.129)##############
[root@mysql-proxy ~]# wget http://ftp.ntu.edu.tw/pub/MySQL/Downloads/MySQL-Proxy/mysql-proxy-0.8.4-linux-el6-x86-64bit.tar.gz
[root@mysql-proxy ~]# tar zxvf mysql-proxy-0.8.4-linux-el6-x86-64bit.tar.gz -C /usr/local
[root@mysql-proxy ~]# cd /usr/local/
[root@mysql-proxy local]# mv mysql-proxy-0.8.4-linux-el6-x86-64bit/ mysql-proxy
[root@mysql-proxy local]# useradd -r mysql-proxy
[root@mysql-proxy local]# vim /etc/profile
#环境变量配置文件/etc/profile中加入代码之后, 执行生效
[root@mysql-proxy local]# source /etc/profile
[root@mysql-proxy 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.2.134" --proxy-read-only-backend-addresses="192.168.2.135" --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"
(1) MYSQL-Proxy启动后,在服务器端查看端口,其中4040为proxy代理端口用于WEB应用连接,4041位管理端口用于SA或者DBA管理
[root@mysql-proxy mysql-proxy]# netstat -anputl | grep mysql-proxy
tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN 3995/mysql-proxy
tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 3995/mysql-proxy
mysql-master端授权
[root@localhost ~]# mysql -h192.168.20.129 -uadmin -padmin -P 4041
[root@mysql-master mysql-5.5.22]# mysql -u root -p123.com
mysql> grant all on *.* to xiaoyu@'192.168.2.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
[root@localhost ~]# mysql -h192.168.2129 -uxiaoyu -p123456 -P4040 -e "show databases;"