MySQL主从复制与读写分离

环境:

主数据库服务器:192.168.10.9
从数据库服务器:192.168.10.8
Amoeba服务器:192.168.10.3

一、主从复制配置

配置主数据库服务器

编辑mysql配置文件。

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

查看[mysqld]部分是否有如下配置,没有就给加上。

log-bin=mysql-bin
server-id       = 1

重启mysql服务。

[root@localhost ~]# systemctl start mysqld

通过命令行登录mysql。

[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.60-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

 在master上创建一个‘repl’用户,允许其他slave通过这个用户远程访问master的binlog文件实现数据同步(192.168.10.%,这个配置是指明replace用户所在的服务器,这里%是通配符,标识192.168.10这个网段的server都可以使用这个账户登录,也可以制定某一个ip地址)。

mysql> GRANT REPLICATION SLAVE ON *.* TO "repl"@"192.168.10.%" IDENTIFIED BY "112233";

查看master状态,记录binlog文件名和位置

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000012 |      263 |              |                  |
+------------------+----------+--------------+------------------+

 

配置从数据库服务器

修改mysql配置文件。

还是看看这两行有没有没有就加上,并将server-id修改为2(保证server-id是唯一的)。

log-bin=mysql-bin
server-id       = 2

重启mysql。

用命令行登录mysql执行同步语言。

mysql> CHANGE MASTER TO
    -> MASTER_HOST="192.168.10.9",
    -> MASTER_USER="repl",
    -> MASTER_PASSWORD="112233",
    -> MASTER_LOG_FILE="mysql-bin.000012",
    -> MASTER_LOG_POS=263;

启动slave同步进程。

mysql> start slave;

查看slave状态。

mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.10.9
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000012
          Read_Master_Log_Pos: 263
               Relay_Log_File: localhost-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000012
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
……

可以看到Slave_IO_running和Slave_SQL_Runing都未Yes了,那么主从同步应该是设置成功了,下边去试验一下

我们尝试在master的test数据库里面新增一张表first_db,并插入一行数据

mysql> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql> use test

Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> create table first_db(id int(3), name varchar(11));

Query OK, 0 rows affected (0.01 sec)

mysql> insert into first_db values(1,"hello");

Query OK, 1 row affected (0.01 sec)

mysql> select * from first_db;

+------+-------+
| id   | name  |
+------+-------+
|    1 | hello |
+------+-------+
1 row in set (0.00 sec)

去到slave看一看。

mysql> use test

Database changed
mysql> show tables;

+----------------+
| Tables_in_test |
+----------------+
| first_db       |
+----------------+
1 row in set (0.00 sec)

mysql> select * from first_db;

+------+-------+
| id   | name  |
+------+-------+
|    1 | hello |
+------+-------+
1 row in set (0.00 sec)

数据已经同步过来了,那么主从复制已经成功了!

 

二、使用Amoeba实现读写分离

配置 Java 环境

由于 Amoeba 是 Java 开发的所以还要配置jdk

先去下载 Java

https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

解压然后

[root@localhost src]# tar -xvf jdk-8u212-linux-x64.tar.gz\?AuthParam\=1555829703_d242bc119423c2899a2b79944b1cc6c6

解压后的文件夹移动到新的目录

[root@localhost src]# mv jdk1.8.0_212 /usr/local/java

添加环境变量

[root@localhost ~]# vim /etc/profile

把下面代码放到最后面去 

JAVA_HOME=/usr/local/java
JRE_HOME=$JAVA_HOME/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH

重新加载配置文件

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

成功了应该可以显示 Java 的版本

[root@localhost ~]# java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-b04)
OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)

下载并解压 Amoeba

[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget https://nchc.dl.sourceforge.net/project/amoeba/Amoeba%20for%20mysql/3.x/amoeba-mysql-3.0.5-RC-distribution.zip
[root@localhost src]# unzip amoeba-mysql-3.0.5-RC-distribution.zip 

把文件夹移动到新的目录

[root@localhost src]# mv amoeba-mysql-3.0.5-RC /usr/local/amoeba

配置 Amoeba

分别配置conf目录下的 dbServers.xml 和 amoeba.xml 文件。

先是 dbServers.xml。






        
        
    
        
            ${defaultManager}
            64
            128
                
            
            3306  #设置Amoeba要连接的mysql数据库的端口,默认是3306
            
            
            test  #设置缺省的数据库,当连接amoeba时,操作表必须显式的指定数据库名,即采用dbname.tablename的方式,不支持 use dbname指定缺省库,因为操作会调度到各个后端dbserver
            
            
            test  #设置amoeba连接后端数据库服务器的账号和密码,因此需要在所有后端数据库上创建该用户,并授权amoeba服务器可连接
            
            11111
        

        
            500  #最大连接数,默认500
            500    #最大空闲连接数
            1    #最新空闲连接数
            600000
            600000
            true
            true
            true
        
    

    master"  parent="abstractServer">  #设置一个后端可写的dbServer,这里定义为master,这个名字可以任意命名,后面还会用到
        
            
            192.168.10.9 #设置后端可写dbserver
        
    
    
    slave"  parent="abstractServer">  #设置后端可读dbserver
        
            
            192.168.10.8
        
    
    
    myslave" virtual="true">  #设置定义一个虚拟的dbserver,实际上相当于一个dbserver组,这里将可读的数据库ip统一放到一个组中,将这个组的名字命名为mySlave
        
            
            1  #选择调度算法,1表示复制均衡,2表示权重,3表示HA, 这里选择1
            
            
            slave  #mySlave组成员
        
    
        

再配 amoeba.xml。






    
    
        
        
            
            8066    #设置amoeba监听的端口,默认是8066
            
                #下面配置监听的接口,如果不设置,默认监听所以的IP
            
            
            
                
                    128
                    64
                
            
            
            
                
                    

# 提供客户端连接amoeba时需要使用这里设定的账号 (这里的账号密码和amoeba连接后端数据库服务器的密码无关)

                    root    

                    
                    123456
                    
                    
                        
                            ${amoeba.home}/conf/access_list.conf
                        
                    
                
            
            
        
        
        
            
            
            128
            
            
            500
            
            
            utf8
            
            
            60
        
        
    
    
    
    
        
            com.meidusa.toolkit.net.AuthingableConnectionManager
        
    
    
        
    
        ${amoeba.home}/conf/dbServers.xml
    
    
    
        
            
                ${amoeba.home}/conf/rule.xml
                ${amoeba.home}/conf/ruleFunctionMap.xml
            
        
        ${amoeba.home}/conf/functionMap.xml
        1500
        master  #设置amoeba默认的池,这里设置为writedb
        
        
        master  #这两个选项默认是注销掉的,需要取消注释,这里用来指定前面定义好的俩个读写池
        mySlave
        
        true
    

分别在 master 和 slave 上新建账号用作 Amoeba 连接。

mysql> grant all privileges on test.* to 'test'@'192.%' identified by '11111';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

启动 Amoeba

[root@localhost amoeba]# cd bin/
[root@localhost bin]# ./launcher

这里报错了,是由于stack size太小,导致JVM启动失败,需要去修改jvm.properties文件JVM_OPTIONS参数。

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=16m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=96m; support was removed in 8.0 

The stack size specified is too small, Specify at least 228k
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

 将内容:

JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"

替换为:

JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m" 

先关闭进程后,再次启动 Amoeba。

[root@localhost bin]# ./shutdown 
kill -15 1924
[root@localhost bin]# ./launcher
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=16m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=96m; support was removed in 8.0
 2019-04-21 22:30:54 [INFO] Project Name=Amoeba-MySQL, PID=1924 , starting...
log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
2019-04-21 22:30:55,669 INFO  context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-3.0.4-BETA
log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf
2019-04-21 22:30:56,292 INFO  net.ServerableConnectionManager - Server listening on 0.0.0.0/0.0.0.0:8066.
 2019-04-21 22:31:29 [INFO] Project Name=Amoeba-MySQL, PID=1924 , System shutdown ....
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=16m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=96m; support was removed in 8.0
 2019-04-21 22:31:38 [INFO] Project Name=Amoeba-MySQL, PID=1974 , starting...
log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
2019-04-21 22:31:39,165 INFO  context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-3.0.4-BETA
log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf
2019-04-21 22:31:39,818 INFO  net.ServerableConnectionManager - Server listening on 0.0.0.0/0.0.0.0:8066.

可以看到 Amoeba 已经在监听 8066 端口了

[root@localhost ~]# netstat -unlpt | grep java
tcp6       0      0 :::8066                 :::*                    LISTEN      1974/java

三、测试

使用在 Amoeba 配置文件 amoeba.xml 中设置的用户名、密码、端口号远程登录 MySQL

[root@localhost ~]# mysql -h192.168.10.7 -P8066 -utest -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1764378459
Server version: 5.1.45-mysql-amoeba-proxy-3.0.4-BETA

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

可以看到已经可以读出表了

mysql> use test;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_test  |
+-----------------+
| city            |
| curl            |
| employess       |
| grade           |
| match           |
| message         |
| message1        |
| migrations      |
+-----------------+
8 rows in set (0.01 sec)

新建一张表,并写入一些数据。

可以看到操作都成功了!

mysql> create table amoeba(id int unsigned not null auto_increment primary key,name varchar(20) not null) engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into amoeba set name='a';
Query OK, 1 row affected (0.01 sec)

mysql> select * from amoeba;
+----+------+
| id | name |
+----+------+
|  1 | a    |
+----+------+
1 row in set (0.01 sec)

为了验证是否真的读写分离,这里把 slave 的复制停止。

mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.10.9
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000033
          Read_Master_Log_Pos: 1055
               Relay_Log_File: localhost-relay-bin.000045
                Relay_Log_Pos: 1201
        Relay_Master_Log_File: mysql-bin.000033
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1055
              Relay_Log_Space: 1404
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

再次往新建的表里面插入一条数据,并查询数据。

可以看到新插入的数据没有被查询出来。

mysql> insert into amoeba set name='b';
Query OK, 1 row affected (0.01 sec)

mysql> select * from amoeba;
+----+------+
| id | name |
+----+------+
|  1 | a    |
+----+------+
1 row in set (0.01 sec)

进一步验证:

在 master 上查询,可以看到新插入的数据。

mysql> select * from amoeba;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
+----+------+
2 rows in set (0.00 sec)

而在 slave 上查询,没有看到新插入的数据。

mysql> select * from amoeba;
+----+------+
| id | name |
+----+------+
|  1 | a    |
+----+------+
1 row in set (0.00 sec)

OK ,成功实现读写分离了!

你可能感兴趣的:(MySQL)