Linux-MySQL5.6主从复制与读写分离

Posted Vampire-Min

tags:

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

主机

操作系统

IP地址

主要软件

Master

Centos7.3x86_64

192.168.1.101

Cmake,mysql

Slave01

Centos7.3x86_64

192.168.1.102

Cmake,Mysql

Slave02

Centos7.3x86_64

192.168.1.103

Cmake,Mysql

Amoeba

Centos7.3x86_64

192.168.1.104

Amoeba

Client

Centos7.3x86_64

192.168.1.105

 

 

 

 

 

 

 

 

 

 

 

 

实验环境:

注:请提前配置好防火墙规则和本地yum源以及selinux

主从复制:

1         建立时间同步环境在master上面安装时间同步服务器

1.1         安装NTP

[root@master ~]# yum -y install ntp

1.2         配置NTP

[root@master ~]# vim /etc/ntp.conf      #添加如下两行

server 127.127.1.0

fudge 127.127.1.0stratum 8

1.3         重启服务

[root@master ~]# systemctl restart ntpd

2         在从节点(slave01和都slave02都要同步)上面进行时间同步  

[root@slave01 ~]# yum -y install ntpdate    #此处以slave01为例

[root@slave01 ~]# ntpdate 192.168.1.101

3         在Master,slave01和slave02上面编译安装Mysql

3.1         编译安装(在三个节点安装的步骤相同,此处以master为例)

    [root@master ~]# cd opt

[root@master opt]# yum install ncurses-devel -y

[root@master opt]# tar xf cmake-2.8.6.tar.gz

[root@master opt]# cd cmake-2.8.6

[root@master cmake-2.8.6]# ./configure && gmake && gmake install

[root@master cmake-2.8.6]# cd ..

[root@master opt]# tar xf mysql-5.6.36.tar.gz

[root@master opt]# cd mysql-5.6.36/

[root@master mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc && make && make install

3.2         优化调整

[root@master mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf

[root@master mysql-5.6.36]# cp support-files/mysql.server /etc/init.d/mysqld

[root@master mysql-5.6.36]# chmod +x /etc/init.d/mysqld

[root@master mysql-5.6.36]# chkconfig --add mysqld

[root@master mysql-5.6.36]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile

[root@master mysql-5.6.36]# . /etc/profile

3.3         初始化数据库

[root@master mysql-5.6.36]# groupadd mysql

[root@master mysql-5.6.36]# useradd -M -s /sbin/nologin mysql -g mysql

[root@master mysql-5.6.36]# chown -R mysql: /usr/local/mysql

[root@master mysql-5.6.36]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

3.4         启动MySQL服务

[root@master scripts]# systemctl start mysqld

[root@master scripts]# chkconfig mysqld on

[root@master scripts]# mysqladmin -uroot password ‘pwd123‘ #修改root密码

Warning: Using a password on the command line interface can be insecure.

4         配置MySQL Master主服务器

4.1         修改/etc/my.cnf文件,增加以下内容

[root@master ~]# vim /etc/my.cnf

     server_id = 11

log-bin = master-bin

log-slave-updates = true

4.2         重启MySQL服务

[root@master etc]# systemctl restart mysqld

4.3         登录MySQL程序,给服务器授权

[root@master ~]# mysql -uroot -ppwd123

mysql> GRANT REPLICATION SLAVE ON *.* TO ‘mysql‘@‘192.168.1.%‘ IDENTIFIED BY ‘123456‘;

mysql> FLUSH PRIVILEGES;

mysql> show master status;

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

| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| master-bin.000001 |      408 |              |                  |                   |

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

1 row in set (0.00 sec)

5         配置从服务器

5.1         修改/etc/my.cnf增加以下内容:

[root@slave01 ~]# vim /etc/my.cnf

server_id = 22

relay-log = relay-log-bin

relay-log-index = slave-relay-bin.index

5.2         重启服务

[root@slave01 ~]# systemctl restart mysqld

5.3         登录MySQL,配置同步

[root@slave01 ~]# mysql -uroot -ppwd123

mysql> change master to master_host=‘192.168.1.101‘,master_user=‘mysql‘,master_password=‘123456‘,master_log_file=‘master-bin.000001‘,master_log_pos=408;

5.4         启动同步

5.4.1    mysql> start slave;

5.5         查看Slave状态,确保以下两个值为YES

mysql> show slave statusG;

   Slave_IO_Running: Yes

   Slave_SQL_Running: Yes

6         验证主从复制

6.1         在主,从服务器上登录MySQL

[root@master ~]# mysql -uroot -ppwd123

mysql> show databases;

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

| Database           |

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

| information_schema |      

| mysql              |

| performance_schema |

| test               |

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

4 rows in set (0.00 sec)

6.2         在主服务器上面新建db_test数据库

mysql> create database db_test charset ‘utf8‘;

Query OK, 1 row affected (0.00 sec)

6.3         在主从数据库上分别查看数据库,显示数据库相同,则主从复制成功

mysql> show databases;

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

| Database           |

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

| information_schema |

| db_test            |

| mysql              |

| performance_schema |

| test               |

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

5 rows in set (0.01 sec)

 

 

读写分离:

1         在Amoeba主机安装java环境

因为Amoeba是基于jdk1.5开发的,所以官方推荐使用jdk1.5或jdk1.6,高版本不建议使用

[root@amoeba ~]# mv jdk-1_5_0_21-linux-amd64.bin /usr/local/

[root@amoeba ~]# cd /usr/local/

[root@amoeba local]# chmod +x /usr/local/jdk-1_5_0_21-linux-amd64.bin

[root@amoeba local]# ./jdk-1_5_0_21-linux-amd64.bin       #根据提示按enter输入即可

[root@amoeba local]# mv jdk1.5.0_21/ jdk1.5

[root@amoeba ~]#vim /etc/profile          # 添加以下配置:

export JAVA_HOME=/usr/local/jdk1.5

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/bin

[root@amoeba ~]# source /etc/profile

[root@amoeba ~]# java -version

java version "1.5.0_21"

Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_21-b01)

Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_21-b01, mixed mode)

2         安装并配置Amoeba软件

张三

[root@amoeba ~]# mkdir /usr/local/amoeba

[root@amoeba ~]# tar xf /opt/amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/

[root@amoeba ~]# chmod -R 755 /usr/local/amoeba/

[root@amoeba ~]# /usr/local/amoeba/bin/amoeba

amoeba start|stop            ##显示此内容说明Amoeba安装成功

3         配置Amoeba读写分离,两个slave读负载均衡

3.1         Master,slave01,slave02开放权限给amoeba访问

grant all on *.* to ‘test‘@‘192.168.1.%‘ identified by ‘123.com‘;

flush privileges;

3.2         编辑amoeba.xml配置文件

修改内容为下列标记位置:

                        <property name="authenticator">

                                <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

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

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

 

                                        <property name="filter">

                                                <bean class="com.meidusa.amoeba.server.IPAccessController">

                                                        <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>

                                                </bean>

                                        </property>

                                </bean>

                        </property>

修改内容为以下加粗部分,注意删除注释!!!

        <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">

                <property name="ruleLoader">

                        <bean class="com.meidusa.amoeba.route.TableRuleFileLoader">

                                <property name="ruleFile">${amoeba.home}/conf/rule.xml</property>

                                <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>

                        </bean>

                </property>

                <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>

                <property name="LRUMapSize">1500</property>

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

               

                <property name="writePool">master</property>

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

 

                <property name="needParse">true</property>

        </queryRouter>

3.3         编辑dbServer.xml配置

[root@amoeba amoeba]# vim conf/dbServers.xml

修改内容为以下加粗部分:

                        <!-- mysql user -->

                        <property name="user">test</property>

 

                        <!--  mysql password-->

                        <property name="password">123.com</property>

                </factoryConfig>

 

 

以下修改后的内容为加粗的部分:

        <dbServer name="master"  parent="abstractServer">

                <factoryConfig>

                        <!-- mysql ip -->

                        <property name="ipAddress">192.168.1.101</property>

                </factoryConfig>

        </dbServer>

 

        <dbServer name="slave01"  parent="abstractServer">

                <factoryConfig>

                        <!-- mysql ip -->

                        <property name="ipAddress">192.168.1.102</property>

                </factoryConfig>

        </dbServer>

 

        <dbServer name="slave02"  parent="abstractServer">

                <factoryConfig>

                        <!-- mysql ip -->

                        <property name="ipAddress">192.168.1.103</property>

                </factoryConfig>

        </dbServer>

 

        <dbServer name="slaves" virtual="true">

                <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">

                        <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->

                        <property name="loadbalance">1</property>

 

                        <!-- Separated by commas,such as: server1,server2,server1 -->

                        <property name="poolNames">slave01,slave02</property>

                </poolConfig>

        </dbServer>

3.4         确认配置无误后,可以启动amoeba软件,其默认的端口是tcp8066

[root@amoeba amoeba]# ./bin/amoeba start&

[root@amoeba amoeba]# netstat -anptl |grep 8066

tcp6     0  0     :::8066     :::*         LISTEN      2663/java

4         测试

4.1         在client主机上面安装mysql命令进行测试

[root@client ~]# yum -y install mysql

可以通过代理访问MySQL:

[root@client ~]# mysql -uamoeba -p123456 -h 192.168.1.104 -P8066

Welcome to the MariaDB monitor.  Commands end with ; or g.

Your MySQL connection id is 96263158

Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 Source distribution

 

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

 

Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.

 

MySQL [(none)]>

4.2         在Master上面创建一个表,然后同步到各从服务器上,然后关闭各种从服务器的slave功能,再插入区别语句

mysql> use db_test;

Database changed

mysql> create table animal(id int(10) primary key,name varchar(20) not null,number int(10) not null);

Query OK, 0 rows affected (0.01 sec)

分别在两台从服务器上面关闭slave功能:

mysql> stop slave;

Query OK, 0 rows affected (0.00 sec)

然后在master服务器上面插入区别语句:

mysql> insert into animal values (1,‘elephant‘,500);

Query OK, 1 row affected (0.00 sec)

4.3         从服务器(slave)上面同步了表,手动插入其他内容

Slave01:

mysql> use db_test;

mysql> insert into animal values (‘2‘,‘tiger‘,102);

Query OK, 1 row affected (0.00 sec)

Slave02:

mysql> use db_test;

mysql> insert into animal values (3,‘dolphin‘,103);

Query OK, 1 row affected (0.00 sec)

4.4         测试读操作

在client主机上第一次查询结果如下:

MySQL [(none)]> select * from db_test.animal;

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

| id | name    | number |

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

|  3 | dolphin |    103 |

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

1 row in set (0.00 sec)

第二次查询结果如下:

MySQL [(none)]> select * from db_test.animal;

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

| id | name  | number |

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

|  2 | tiger |    102 |

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

1 row in set (0.01 sec)

第三次查询结果如下:

MySQL [(none)]> select * from db_test.animal;

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

| id | name    | number |

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

|  3 | dolphin |    103 |

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

1 row in set (0.00 sec)

4.5         测试写操作

在client主机上插入一条语句:

MySQL [(none)]> insert into db_test.animal values (4,‘seagull‘,‘105‘);

Query OK, 1 row affected (0.00 sec)

但在client上查询不到,最终只有在Master上才能查看到这条语句内容,说明写操作在Master服务器上

mysql> select * from db_test.animal;

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

| id | name     | number |

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

|  1 | elephant |    500 |

|  4 | seagull  |    105 |

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

2 rows in set (0.00 sec)

由此验证,已经实现了MySQL的读写分离,目前所有的写操作全部都在Master主服务器上,用来避免数据的不同步,所有的读操作都分摊给了Slave从服务器,用来分摊数据库的压力

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

MySQL 5.6 主从复制配置

MySQL高可用读写分离集群视频课程 MyCat教程

MySQL主从复制与读写分离

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

MySQL主从复制与读写分离

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