mysql主从同步读写分离
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql主从同步读写分离相关的知识,希望对你有一定的参考价值。
https://pan.baidu.com/s/1tm_FQ4C8heQqzx01URr85A //软件连接百度网盘
三台mysql数据库:主数据库服务器:192.168.80.100
从数据库服务器1:192.168.80.101
从数据库服务器2:192.168.80.102
一台amoeba调度器:192.168.80.103
一台mariadb客户端: 192.168.80.104
注:五台虚拟机第一步都写以下两行
systemctl stop firewalld //关闭防火墙
setenforce 0 //关闭监控
配置MySQL主从环境准备
配置NTP服务器-----时间同步
[[email protected] ~]# yum install -y ntp //安装时间同步程序
[[email protected] ~]# service ntpd start
#注:如果跑错就 service ntpd stop
[[email protected] ~]# ntpdate -u cn.pool.ntp.org //同步网络时间
[[email protected] ~]# vi /etc/ntp.conf //配置时间同步
在合适位置新增以下三行:
restrict 192.168.80.0 mask 255.255.255.0 nomodify
server 127.127.1.0
fudge 127.127.1.0 stratum 8
在从服务器上:
[[email protected] ~]# yum install -y ntp
[[email protected] ~]# ntpdate 192.168.80.100
[[email protected] ~]# echo ‘*/30 * * * * /usr/sbin/ntpdate 192.168.80.100‘ >> /var/spool/cron/root //设置计划任务,每隔30分钟同步一次
[[email protected] ~]# crontab -l
然后配置mysql服务器主从复制
在主mysql服务器上:192.168.80.100
[[email protected] ~]# vi /etc/my.cnf
在socket下面插入:
server-id=11
log_bin=master_bin
log_slave_updates=true
[[email protected] ~]# service mysqld restart
如果没有设置root密码使用下面一行代码
mysqladmin -u root -p password "abc123"
[[email protected] ~]# mysql -u root -p //进入数据库
mysql> GRANT REPLICATION SLAVE ON *.* TO ‘myslave‘@‘192.168.80.%‘ IDENTIFIED BY ‘123456‘;
//为所有从服务器授权所有数据库
mysql> FLUSH PRIVILEGES; //刷新权限
mysql> show master status G
在从服务器1上设置:192.168.80.101
[[email protected] ~]# vi /etc/my.cnf
修改并新增以下内容:
server-id = 22
relay_log=relay-log-bin
relay_log_index=slave-relay-bin.index
[[email protected] ~]# systemctl restart mysqld
如果没有设置root密码使用下面一行代码
mysqladmin -u root -p password "abc123"
[[email protected] ~]# mysql -u root -p
mysql> change master to master_host=‘192.168.80.100‘,master_user=‘myslave‘,master_password=‘123456‘,master_log_file=‘master_bin.000007‘,master_log_pos=154;
mysql> start slave;
mysql> show slave status G
在从服务器2上设置:
[[email protected] ~]# vi /etc/my.cnf
修改并新增以下内容:
server-id = 33
relay_log=relay-log-bin
relay_log_index=slave-relay-bin.index
[[email protected] ~]# service mysqld restart
如果没有设置root密码使用下面一行代码
mysqladmin -u root -p password "abc123"
[[email protected] ~]# mysql -u root -p
mysql> change master to master_host=‘192.168.80.100‘,master_user=‘myslave‘,master_password=‘123456‘,master_log_file=‘master_bin.000007‘,master_log_pos=154;
mysql> start slave;
mysql> show slave status G
测试,在主服务器上创建一个空库,看一下从服务器上有木有
在主服务上
mysql> create database ab;
在从服务器1上:
mysql> show databases;
在服务器2上:
mysql> show databases;
在前端服务器上:192.168.80.103
[[email protected] ~]# yum install -y ntpdate
[[email protected] ~]# ntpdate 192.168.80.100
[[email protected] ~]# echo ‘*/30 * * * * /usr/sbin/ntpdate 192.168.80.100‘ >> /var/spool/cron/root
[[email protected] ~]# crontab -l
[[email protected] ~]# tar xf jdk-8u144-linux-x64.tar.gz -C /opt/
[[email protected] ~]# cd /opt/
[[email protected] opt]# cp -rv jdk1.8.0_144/ /usr/local/java
[[email protected] opt]# vi /etc/profile
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
[[email protected] opt]# source /etc/profile
[[email protected] opt]# java -version
现在搭建好了Java环境了
yum -y install zip unzip
[[email protected] ~]# unzip amoeba-mysql-3.0.5-RC-distribution.zip -d /opt/
[[email protected] ~]# cd /opt/
[[email protected] opt]# mv amoeba-mysql-3.0.5-RC/ /usr/local/amoeba
[[email protected] opt]# chmod -R 755 /usr/local/amoeba/
[[email protected] opt]# vi /usr/local/amoeba/jvm.properties
新增:
JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k"
[[email protected] opt]# vi /etc/init.d/amoeba //编辑系统服务脚本
#!/bin/bash
#chkconfig: 35 62 62
#
export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
NAME=Amoeba
AMOEBA_BIN=/usr/local/amoeba/bin/launcher
SHUTDOWN_BIN=/usr/local/amoeba/bin/shutdown
PIDFILE=/usr/local/amoeba/Amoeba-MySQL.pid
SCRIPTNAME=/etc/init.d/amoeba
case "$1" in
start)
echo -n "Starting $NAME... "
$AMOEBA_BIN
echo " done"
;;
stop)
echo -n "Stoping $NAME... "
$SHUTDOWN_BIN
echo " done"
;;
restart)
$SHUTDOWN_BIN
sleep 1
$AMOEBA_BIN
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}"
exit 1
;;
esac
[[email protected] opt]# chmod +x /etc/init.d/amoeba //给予执行权限
[[email protected] opt]# chkconfig --add amoeba //添加到系统管理中
[[email protected] opt]# service amoeba start //开启amoeba服务
显示这个就等于到这步没有问题
--------配置读写分离-------------
----在三台mysql数据库中为amoeba授权-------
mysql> GRANT ALL ON *.* TO [email protected]‘192.168.80.%‘ IDENTIFIED BY ‘123123‘;
mysql> FLUSH PRIVILEGES;
--------在amoeba前端服务器上----
[[email protected] amoeba]# cd /usr/local/amoeba
[[email protected] amoeba]# vi conf/amoeba.xml
-----设置客户端连接amoeba前端服务器时使用的用户名和密码----28行
<property name="user">amoeba</property>
<property name="password">123456</property>
----同时把默认的server1改成master,把默认的servers改成 slaves-----83行
<property name="defaultPool">slaves</property>
<property name="writePool">master</property>
<property name="readPool">slaves</property>
cd conf/
[[email protected] conf]# vi dbServers.xml 26行
<property name="user">test</property>
<property name="password">123123</property> //指定数据库的登录用户名和密码
修改以下内容
<dbServer name="master" parent="abstractServer">
<property name="ipAddress">192.168.80.100</property>
<dbServer name="slave1" parent="abstractServer">
<property name="ipAddress">192.168.80.101</property>
<dbServer name="slave2" parent="abstractServer">
<property name="ipAddress">192.168.80.102/property>
<dbServer name="slaves" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
<property name="poolNames">slave1,slave2</property>
</poolConfig>
[[email protected] amoeba]# service amoeba restart
[[email protected] amoeba]# netstat -anpt | grep java
在客户端服务器上:192.168.80.104
[[email protected] ~]# yum install -y mysql
[[email protected] ~]# mysql -u amoeba -p123456 -h 192.168.80.103 -P8066
在客户端上使用命令在test创建一个表:
MySQL [(none)]> use test;
MySQL [test]> create table zhang (id int(10),name varchar(10),address varchar(20));
MySQL [test]> show tables;
80.100
80.104
以上是关于mysql主从同步读写分离的主要内容,如果未能解决你的问题,请参考以下文章