MySql主从复制与读写分离

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySql主从复制与读写分离相关的知识,希望对你有一定的参考价值。

.项目实验拓扑图(使用亿图专家绘图)

技术分享 

二、项目实验思路及重点内容(步骤及重难知识点)

1、实验思路:

   第一步

在主从节点搭建时间服务器

   第二步

在主从节点安装mysql

   第三步

配置mysql主从复制

   第四步

搭建mysql读写分离

   第五步

测试读写分离

2、实验环境:

   主机

  操作系统

    IP地址

         主机软件

Master

CentOS6.5

192.168.100.150

cmake-2.8.6.tar.gz

mysql-5.5.22.tar.gz

Slave1

CentOS6.5

192.168.100.151

cmake-2.8.6.tar.gz

mysql-5.5.22.tar.gz

Slave2

CentOS6.5

192.168.100.152

cmake-2.8.6.tar.gz

mysql-5.5.22.tar.gz

Amoeba

CentOS6.5

192.168.100.153

amoeba-mysql-binary-2.2.0.tar.gz

Jdk-6u14-linux-x64.bin

客户端

CentOS6.5

192.168.100.154


 

.项目实验步骤(操作截图和操作命令)

1、配置PCIP地址和源地址。

(略。。。)

2、搭建时间服务器。

 1)主节点上安装ntp时间服务:192.168.100.150

yum -y install ntp

sed -i ‘/^server/s/^/#/g‘ /etc/ntp.conf

cat <<END >>/etc/ntp.conf

server 127.127.1.0

fudge 127.127.1.0 stratum 8

END

/etc/init.d/ntpd restart

netstat -utpln |grep ntp

技术分享 

 2)从节点同步时间:192.168.100.151-152

yum -y install ntpdate

/usr/sbin/ntpdate 192.168.100.150       ##指主服务器IP进行同步时间

技术分享 

技术分享 

 

3、在主从节点安装MySQL

 1)在主从节点准备mysql的必要安装包。(红色为必要的安装包)

技术分享 

 2)在主从节点新建mysql安装脚本和mysql的配置脚本。

安装MySql脚本

[[email protected]]# cat mysql_install.sh      

#!/bin/bash

##第一配置yum,安装ncurses依赖包

yum -y install ncurses-*

#解压cmake,安装基础环境

tar zxvf /root/cmake-2.8.6.tar.gz -C /usr/src/

cd /usr/src/cmake-2.8.6

#配置,编译安装cmake

./configure &&gmake &&gmake install

##解压mysql

tar zxvf /root/mysql-5.5.22.tar.gz -C /usr/src/

cd /usr/src/mysql-5.5.22/

#cmake进行配置mysql

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql   #指定安装目录\

-DDEFAULT_CHARSET=utf8   #指定字符集为utf8 \

-DDEFAULT_COLLATION=utf8_general_ci   ##指定字符校验 \

-DWITH_EXTRA_CHARSETS=all   ##支持额外字符集\

-DSYSCONFDIR=/etc/  ##指定配置文件位置

make &&make install   #编译安装

if [ -e /usr/local/mysql ];then

echo "mysql install successfully."

fi

 

配置MySql脚本

[[email protected]]# cat mysql_config.sh

#!/bin/bash

#1.复制配置文件

cp /usr/src/mysql-5.5.22/support-files/my-medium.cnf /etc/my.cnf

#2.添加系统服务

cp /usr/src/mysql-5.5.22/support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig mysqld  on

#3.优化PATH路径,执行命令时方便,单引号双引号都行

grep mysql /etc/profile

if [ $? -eq 0 ];then

echo "PATH is set."

else

echo "export PATH=$PATH:/usr/local/mysql/bin"  >>/etc/profile

source /etc/profile  ##执行文件

fi

#4.初始化mysql,创建用户,赋权

useradd -M -s /sbin/nologin mysql

chown -R mysql:mysql /usr/local/mysql

/usr/local/mysql/scripts/mysql_install_db  \

--basedir=/usr/local/mysql \

--datadir=/usr/local/mysql/data --user=mysql

#5.启动mysql,并设置为开机启动

if [ -e /tmp/mysql.sock ];then

/etc/init.d/mysqld restart

else

/etc/init.d/mysqld start

fi

chkconfig mysqld on

#6.修改密码,并提示密码

mysqladmin -u root password ‘123123‘  &&echo "mysql root password is 123123"

 

在主从节点安装mysql完成后,重启计算机。

 

4、配置MySql主从复制。

1)配置主服务器:192.168.100.150

sed -i ‘s/^log-bin=.*/log-bin=master-bin\nlog-slave-updates=ture/g‘ /etc/my.cnf

sed -i ‘/^server-id/s/1/11/g‘ /etc/my.cnf    ##设置优先级

/etc/init.d/mysqld restart

mysql -uroot -p123123

mysql> grant replication slave on *.* to ‘myslave‘@‘192.168.100.%‘ identified by ‘123123‘;            ##设置数据库用户的复制从权限

mysql> flush privileges;     ##刷新用户授权信息

mysql> show master status;

  ##记住File的及Position的值,此处为master-bin.000001339

mysql> create database db_test;  ##创建测试数据库

mysql> quit

技术分享 

 

2)配置从服务器1192.168.100.151

sed -i ‘/^server-id/s/1/22/g‘ /etc/my.cnf

sed -i ‘/^server-id/arelay-log=relay-log-bin\nrelay-log-index=slave-relay-bin.index‘ /etc/my.cnf

/etc/init.d/mysqld restart

mysql -uroot -p123123

mysql> change master to master_host=‘192.168.100.150‘,master_user=‘myslave‘,master_password=‘123123‘,master_log_file=‘master-bin.000001‘,master_log_pos=339;

  ##指向主服务器的文件及目录

mysql> start slave;    ##启动同步复制

mysql> show slave status\G;

技术分享 

mysql> show databases;  ##验证数据库是否同步

mysql> quit

技术分享 

 

3)配置从服务器2192.168.100.152

sed -i ‘/^server-id/s/1/33/g‘ /etc/my.cnf

sed -i ‘/^server-id/arelay-log=relay-log-bin\nrelay-log-index=slave-relay-bin.index‘ /etc/my.cnf

/etc/init.d/mysqld restart

mysql -uroot -p123123

mysql> change master to master_host=‘192.168.100.150‘,master_user=‘myslave‘,master_password=‘123123‘,master_log_file=‘master-bin.000001‘,master_log_pos=339;

mysql> start slave;

mysql> show slave status\G;

技术分享 

mysql> show databases;  ##验证数据库是否同步

mysql> quit

技术分享 

 

5、搭建MySQL读写分离。

1)基本配置。

yum -y remove java

chmod +x jdk-6u14-linux-x64.bin

./jdk-6u14-linux-x64.bin

mv jdk1.6.0_14/ /usr/local/jdk1.6

vi  /etc/profile

export JAVA_HOME=/usr/local/jdk1.6

export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin/:$PATH:$HOME/bin

export AMOEBA_HOME=/usr/local/amoeba

export PATH=$PATH:$AMOEBA_HOME

:wq

source /etc/profile

java -version

mkdir /usr/local/amoeba

tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/

chmod -R 755 /usr/local/amoeba/

/usr/local/amoeba/bin/amoeba  ##验证

 

2)数据授权给amoeba读写权限:

mysql> grant all on *.* to [email protected]‘192.168.100.%‘ identified by ‘123123‘;   ##Master上完成

mysql> show grants for [email protected]‘192.168.100.%‘;   ##192.168.100.151-152上查看是否同步了权限

技术分享 

 

3)修改配置文件:192.168.100.153

vim /usr/local/amoeba/conf/amoeba.xml

 30       <property name="user">amoeba</property>

 31

 32       <property name="password">123456</property>

115       <property name="defaultPool">master</property>

116

117      <property name="writePool">master</property>   ##注意删除<!--  -->的注释

118       <property name="readPool">slaves</property>

:set nu  ##显示行号

:wq

 

vim /usr/local/amoeba/conf/dbServers.xml

 25    <!-- mysql user -->

 26    <property name="user">linuxfan</property>   ##该用户必须是上一步授权的用户

 27                         

 28     <!--  mysql password -->   ##修改

 29     <property name="password">123123</property>  ##删除下一行的“-->

 44    <dbServer name="master"  parent="abstractServer">   ##修改为master

 45           <factoryConfig>

 46                  <!-- mysql ip -->

 47     <property name="ipAddress">192.168.100.150</property>  ##指定正确的masterip

 51      <dbServer name="slave1"  parent="abstractServer">  ##修改为slave1

 52                 <factoryConfig>

 53                         <!-- mysql ip -->

 54    <property name="ipAddress">192.168.100.151</property>  ##指定slave1ip地址

 55                 </factoryConfig>

 56         </dbServer>

 57         <dbServer name="slave2"  parent="abstractServer">  ##添加如下6行,指定slave2ip

 58                 <factoryConfig>

 59                         <!-- mysql ip -->

 60       <property name="ipAddress">192.168.100.152</property>

 61                 </factoryConfig>

 62         </dbServer>

 64         <dbServer name="slaves" virtual="true">  ##修改为slaves

 70        <property name="poolNames">slave1,slave2</property>  ##修改集群的成员名称用逗号隔开

:wq

/usr/local/amoeba/bin/amoeba start&   ##启动代理服务

netstat -utpln |grep 8066  ##验证

技术分享 

 

6、测试读写分离。

1)验证主从复制:192.168.100.154

yum -y install mysql

mysql -uamoeba -p123456 -h 192.168.100.153 -P 8066   

##登录db集群192.168.100.154

技术分享 

mysql>show databases;

mysql> use db_test;

mysql> create table linuxfan(id int(10),name varchar(10),address varchar(20));

技术分享 

192.168.100.151-152上查看结果:

mysql -uroot -p123123

mysql> use db_test;

mysql> show tables;   ##同步

技术分享 

 

2)关闭slave1slave2的复制功能:192.168.100.151-152

mysql> stop slave;

 

3)分别在masterslave1slave2上创建不同的数据:

master:

mysql> use db_test;

mysql> insert into linuxfan values(1,‘hehe‘,‘this is master‘);

slave1:

mysql> use db_test;

mysql> insert into linuxfan values(2,‘hehe‘,‘this is slave1‘);

slave2:

mysql> use db_test;

mysql> insert into linuxfan values(3,‘hehe‘,‘this is slave2‘);

 

4)应用客户端验证读:192.168.100.157

mysql> select * from linuxfan;  ##第一次查询

+------+------+----------------+

| id   | name | address        |

+------+------+----------------+

|    2 | hehe | this is slave1 |

+------+------+----------------+

1 row in set (0.02 sec)

 

mysql> select * from linuxfan;   ##第二次查询

+------+------+----------------+

| id   | name | address        |

+------+------+----------------+

|    3 | hehe | this is slave2 |

+------+------+----------------+

1 row in set (0.01 sec)

mysql> select * from linuxfan;   ##第三次查询

+------+------+----------------+

| id   | name | address        |

+------+------+----------------+

|    2 | hehe | this is slave1 |

+------+------+----------------+

1 row in set (0.00 sec)

 

5)应用客户端上验证写:

mysql> insert into linuxfan values(4,‘hehe‘,‘app write test‘);  ##写入数据

Query OK, 1 row affected (0.02 sec)

 

mysql> select * from linuxfan;   ##查不到刚写入的数据

+------+------+----------------+

| id   | name | address        |

+------+------+----------------+

|    3 | hehe | this is slave2 |

+------+------+----------------+

1 row in set (0.01 sec)

 

master上验证:

mysql> select * from linuxfan;   ##查到数据

+------+------+----------------+

| id   | name | address        |

+------+------+----------------+

|    1 | hehe | this is master |

|    4 | hehe | app write test |

+------+------+----------------+

2 rows in set (0.00 sec)

 

app写入数据时,amoeba会将数据路由到master上进行存储,app读取数据时,amoeba会将读的请求一轮询的方式发给slaves组(slave1+slave2),实现读写分离。

masterslaves间配置了主从复制,保证了数据的一致性。

 


以上是关于MySql主从复制与读写分离的主要内容,如果未能解决你的问题,请参考以下文章

MySQL主从复制与读写分离

MySQL主从复制与读写分离的理论+实操(配有详细图释)

MySQL主从复制与读写分离

MySQL 主从复制 与 读写分离

运维必懂的MySQL主从复制与读写分离解析和图文详细步骤!

部署MySQL主从复制与读写分离