MySQL database (master-slave synchronization, separate read and write real)

Demo1: MySQL master-slave synchronization

lab environment:

The application side: CentOS 7-2 (192.168.18.149, has been installed MySQL)
Amoeba: CentOS 7-1 (192.168.18.128, no MySQL)
Master: centOS 7-3 (192.168.18.148, has been installed MySQL)
from the server: CentOS 7-4 (192.168.18.145, has been installed MySQL)

The primary server settings:

Installation time synchronization service:

[root@master ~]# yum install ntp -y
[root@master ~]# vim /etc/ntp.conf      #修改ntp配置文件
    server 127.127.195.0        //本地是时钟源,195段落//
    fudge 127.127.195.0 stratum 8       //设置时间层级为8//
[root@master ~]# systemctl start ntpd

Modify MySQL main configuration file:

[root@master ~]# vim /etc/my.cnf
    server-id 10
    log-bin=zhu-bin
    log-slave-updates=ture    ##开启主从同步
[root@master ~]# systemctl restart mysqld
[root@master ~]# systemctl stop firewalld

Modify the content into the database:

[root@master ~]# mysql -u root -p
grant replication slave on *.* to 'myslave'@'192.168.18.%' identified by 'abc123';
#允许从服务器使用myslave账户在主服务器上进行复制操作
show master status;   #查看主服务器信息
mysql> mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |      868 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

From the server settings:

Install the synchronization service client ntpd:

[root@slave ~]# yum install ntp ntpdate -y
[root@slave ~]# systemctl start ntpd
[root@slave ~]# systemctl stop firewalld
[root@slave ~]# /usr/sbin/ntpdate 主服务器地址

Modify MySQL main configuration file:

[root@slave ~]# vim /etc/ntp.conf
    server-id = 20
    relay-log = relay-bin      ###同步日志文件到本地
    relay-log-index = slave-bin.index    ##定义日志文件类型及名称
[root@slave ~]# systemctl restart mysqld

Into the database modifications:

[root@slave ~]# mysql -u root -p123123
change master to master_host='192.168.18.148',master_user='myslave',master_password='abc123',master_log_file='mysql-bin.000005',master_log_pos=865;
    ##授予权限
start slave;
###查询同步是否成功
show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Reconnecting after a failed master event read
                  Master_Host: 192.168.142.148
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1633
               Relay_Log_File: master1-relay-bin.000011
                Relay_Log_Pos: 1126
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes        ##IO连接
            Slave_SQL_Running: Yes        ##SQL连接
            ......

common problem:

Sometimes the shots will meet from synchronization Slave_SQL_Running:NOproblems, the following is a workaround:

MariaDB [(none)]> stop slave;                                                       
MariaDB [(none)]> SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE;
MariaDB [(none)]> start slave; 
MariaDB [(none)]> show slave status\G 

The above is from MySQL master synchronization process, following separation of static and dynamic!


Demo2: MySQL separate read and write

实验基于主从同步的基础上进行

Amoeba server settings:

Jdk installation environment:

[root@amoeba ~]# systemctl stop firewalld.service   #关闭防火墙
[root@amoeba ~]# setenforce 0
[root@amoeba mnt]# cp -p jdk-6u14-linux-x64.bin /usr/local/  #移动事先准备好的jdk包
[root@amoeba mnt]# cd /usr/local/
[root@amoeba local]# ./jdk-6u14-linux-x64.bin       #安装jdk环境
[root@amoeba local]# mv jdk1.6.0_14/ jdk1.6
##配置jdk环境变量
[root@amoeba local]# vim /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/bin
[root@amoeba local]# source /etc/profile

Unpack, install Amoeba Kit:

[root@amoeba local]# mkdir amoeba
[root@amoeba local]# tar zxf /mnt/amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba
[root@amoeba local]# chmod -R 755 amoeba/

[root@amoeba local]# /usr/local/amoeba/bin/amoeba           #检查是否安装成功
amoeba start|stop     #出现以下命令行即为成功

Modifying the master configuration file amoeba.xml:

[root@amoeba local]# vim amoeba/conf/amoeba.xml
#增加客户机访问amoeba的账户、密码
<property name="user">amoeba</property>
<property name="password">123123</property>
---------------------------------------------------------
#开启默认池、读取池、写入池
<property name="defaultPool">master</property>
<property name="writePool">master</property>
<property name="readPool">slaves</property>

Modify amoeba profile dbserver.xml about your database:

[root@amoeba local]# vim amoeba/conf/dbServers.xml
#修改amoeba访问节点服务器的账户、密码
<!-- mysql user -->
    <property name="user">test</property>
<!--  mysql password  -->
    <property name="password">asd123</property>
-------------------------------------------------------------
#指定主、从服务器节点地址
<dbServer name="master"  parent="abstractServer">
    <property name="ipAddress">192.168.18.148</property>
<dbServer name="slave"  parent="abstractServer">
    <property name="ipAddress">192.168.18.145</property>
--------------------------------------------------------------
#将从服务器划入读取地址池中
<dbServer name="slaves" virtual="true">
    <property name="poolNames">slave</property>

Return to the main, from the database server:

[root@slave ~]# mysql -u root -p123123     #进入主服务器数据库(从服务器与主服务器操作相同)
grant all on *.* to test@'192.168.18.%'identified by 'abc123';  #开放test用户访问权限

Open amoeba server:

[root@amoeba conf]# /usr/local/amoeba/bin/amoeba start &   #调入后台持续运行

Application client operations:

Under the premise installed MySQL, enter mysql -u amoeba -p123123 -h 192.168.18.128 -P8066to successfully access the amoeba server. At this time, the primary server when the master write read and write, read from the server using the slave.

Guess you like

Origin blog.51cto.com/14464303/2460848