首页 > 代码库 > Nginx负载均衡+mysql主从复制、读写分离+tomcat项目
Nginx负载均衡+mysql主从复制、读写分离+tomcat项目
Nginx负载均衡+mysql主从复制、读写分离+tomcat项目
架构:(因为资源有限,只有两台虚机)
1 192.168.137.3 Nginx、Mysql(主)、Tomcat1、Tomcat2
2 192.168.137.5 Mysql(从)
系统版本:centos 7
192.168.137.3
1、先安装 Nginx ,方便以后用
2、安装mysql
file /usr/share/mysql/charsets/swe7.xml from install of MySQL-server-5.6.13-1.linux_glibc2.5.x86_64 conflicts with file from package mariadb-libs-1:5.5.52-1.el7.x86_64
yum remove mariadb-libs
[root@localhost local]# rpm -ivh MySQL-server-5.6.13-1.linux_glibc2.5.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:MySQL-server-5.6.13-1.linux_glibc################################# [100%]
Can‘t locate Data/Dumper.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/bin/mysql_install_db line 42.
BEGIN failed--compilation aborted at /usr/bin/mysql_install_db line 42.
[root@localhost local]# yum install ‘perl(Data::Dumper)‘
[root@localhost local]# rpm -e MySQL-server
[root@localhost local]# rpm -ivh MySQL-server-5.6.13-1.linux_glibc2.5.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:MySQL-server-5.6.13-1.linux_glibc################################# [100%]
一定要记得初始化!!!!!真是醉了
[root@localhost ~]# mysql_install_db
[root@localhost ~]# cp /usr/share/mysql/my-default.cnf /etc/my.cnf
[root@localhost ~]# service mysql start
Starting MySQL. ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).
[root@localhost ~]# vi /etc/my.cnf 并在配置文件中添加log-error = /var/log/mysql.log ;log文件在初始化时生成
[root@localhost ~]# chown -R mysql:mysql /var/lib/mysql/
[root@localhost ~]# service mysql start
Starting MySQL. SUCCESS!
/usr/bin/mysqladmin -u root password ‘123123‘
3、安装tomcat
安装jdk
解压进入目录
JAVA_HOME=/usr/local/jdk1.7.0_09
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH
[root@localhost jdk1.7.0_09]# vi /etc/profile
[root@localhost jdk1.7.0_09]# source /etc/profile
[root@localhost jdk1.7.0_09]# java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
安装tomcat
解压进入目录
[root@localhost local]# mv apache-tomcat-6.0.44 tomcat
[root@localhost local]# vi /etc/profile
export TOMCAT_HOME=/usr/local/tomcat
安装tomcat2
解压进入目录
[root@localhost local]# mv apache-tomcat-6.0.44 tomcat2
[root@localhost local]# vi /etc/profile
export TOMCAT_HOME2=/usr/local/tomcat2
修改相应的端口
比如8081
关闭firewalld 客户端测试
192.168.137.5安装mysql(同上面安装一样)
4、配置mysql读写分离,先要设置主从
主mysql:
#GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO ‘cp‘@‘%‘ IDENTIFIED BY ‘123123‘ WITH GRANT OPTION;
注意:以上这行不能使用!!!!在配置读写分离的时候,操作数据库权限不够,用以下语句!!!!!!
grant all privileges on *.* to ‘cp‘@‘%‘ identified by ‘123123‘ WITH GRANT OPTION;
从mysql:
编辑配置文件
server-id = 222
relay-log = /data/relaylogs/relay-bin
mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.137.3‘,MASTER_PORT=3306,MASTER_USER=‘cp‘,MASTER_PASSWORD=‘123123‘,MASTER_LOG_FILE=‘mysql-bin.000004‘,MASTER_LOG_POS=2186726;
ERROR 29 (HY000): File ‘/data/relaylogs/relay-bin.index‘ not found (Errcode: 13 - Permission denied)
上述原因是:/data/relaylogs/relay-bin.index这个文件没有找到,需要touch这个文件!!!!
上述命令成功后,start slave
然后用:show slave status\G 查看是否正确
问题1:在更改已经存在的库,从mysql报错Slave_SQL_Running: No
解决办法:
mysql> reset slave ;
mysql> stop slave ;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> start slave ;
问题2:在master上已经存在的数据库,无法同步到slave上
解决办法:
重新配置主从!!!!!!!!!!!!!!!
重新配置主从---接着上面的操作
1、进入主库,进行锁表,防止数据写入
mysql> flush tables with read lock;
2、在从库上,直接备份远程数据库到本地目录下
mysqldump -h 192.168.137.3 -ucp -p123123 --databases erpBack >erpBack.bak.sql
3、在从库上,停止slave
mysql> stop slave ;
4、新建erpBack库,并导入数据
mysql> create database erpBack ;
mysql> use erpBack ;
mysql> source /root/erpBack.bak.sql
5、在主库上,解锁表;并查看二进制日志信息;并记录
mysql> unlock tables ;
mysql> show master status;
6、在从库上,重新配置连接信息
CHANGE MASTER TO MASTER_HOST=‘192.168.137.3‘,MASTER_PORT=3306,MASTER_USER=‘cp‘,MASTER_PASSWORD=‘123123‘,MASTER_LOG_FILE=‘mysql-bin.000006‘,MASTER_LOG_POS=120;
7、启动slave;查看主从状态
mysql> start slave ;
mysql> show slave status\G
8、在主库上,更新erpBack库数据;并在从库上查看是否运行正常!!
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
问题3:第二天重启系统后,show slave status\G 显示错误:
Slave_IO_Running: No
Slave_SQL_Running: No
启动start slave,显示错误:
ERROR 29 (HY000): File ‘/data/relaylogs/relay-bin.index‘ not found (Errcode: 13 - Permission denied)
解决:show master status; 重新配置主从
mysql> reset slave ;重置slave,再重新配置slave
mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.137.3‘,MASTER_PORT=3306,MASTER_USER=‘cp‘,MASTER_PASSWORD=‘123123‘,MASTER_LOG_FILE=‘mysql-bin.000009‘,MASTER_LOG_POS=467;
mysql> start slave ;
mysql> show slave status\G 查看是否正确
配置mysql读写分离,利用amoeba
https://sourceforge.net/projects/amoeba/files/
下载最新版;并解压
unzip amoeba-mysql-3.0.4-BETA-distribution.zip
mv amoeba-mysql-3.0.4-BETA amoeba
cd amoeba
chmod +x bin/
vi conf/amoeba.xml
#设置客户端连接的参数:端口、用户名、密码等信息
<property name="user">root</property>
<property name="password">root</property>
#设置主从的写与读;原配置文件以下内容是注释掉的,这里需要开启
<property name="writePool">server1</property>
<property name="readPool">server2</property>
vi conf/dbServers.xml
#这里设置连接主从数据库的参数:端口、数据库、用户名、密码
<!-- mysql port -->
<property name="port">3306</property>
<!-- mysql schema -->
<property name="schema">erpBack</property>
<!-- mysql user -->
<property name="user">cp</property>
<property name="password">123123</property>
eg:这里需要注意,主从mysql都需要开启远程访问功能,并设置的用户名与密码相同!!
#设置主从的ip地址;注意:本机的话也IP地址代替
<dbServer name="server1" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.137.3</property>
</factoryConfig>
</dbServer>
<dbServer name="server2" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.137.5</property>
</factoryConfig>
</dbServer>
启动amoeba
bin/launcher &
查看logs
tail -f logs/net.log
客户端测试:
mysql -h 192.168.137.3 -P8066 -uroot -proot
配置Nginx代理
#servers
upstream test.com {
ip_hash;
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
#以上是每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
proxy_pass http://test.com;
proxy_redirect default;
最后,在客户端测试!
本文出自 “mysky” 博客,转载请与作者联系!
Nginx负载均衡+mysql主从复制、读写分离+tomcat项目