简介:

配置要求:Linux(CentOS7) 五台

原理图:一台变形虫(调度器)一台客户端、三台MySQL服务器(一主二从)

MySQL读写分离_第1张图片

五台服务器IP我已经设定好了(五台服务器都设置成NAT模式:需要在线安装包)

变形虫amoeba:192.168.120.128

主服务器master:192.168.120.129

从服务器slave01:192.168.120.130

从服务器slave02:192.168.120.131

客户端client:192.168.120.132

MySQL读写分离_第2张图片

实验要求的安装包在百度云盘中:

MySQL5.5压缩包:链接:https://pan.baidu.com/s/1U6XGW1s6tuOvExPYQ7hAfQ 密码:sffl

amoeba压缩包:链接:https://pan.baidu.com/s/1zdYScsdEUpPlOfloA5vUVA 密码:o32b

jdk压缩包:链接:https://pan.baidu.com/s/1QeRWpSCGcLXeRuW8lV5DUA 密码:zbdz

具体实验步骤:

在主服务器master上操作

第一步关闭防火墙:

[root@localhost ~]# systemctl stop firewalld.service

[root@localhost ~]# setenforce 0

因为系统盘自带ntp,所以不用我们装直接进入配置文件(如果没装用yum install ntp -y自动装)

[root@localhost ~]# vim /etc/ntp.conf

在配置文件最下方插入下面两条语句

server 127.127.120.0                    //本地是时钟源//
fudge 127.127.120.0 stratum 8          //设置时间层级为8//

设置完成后,保存退出开启服务

[root@localhost ~]# systemctl start ntpd.service

到从服务器1上操作:

关闭防火墙

[root@localhost ~]# systemctl stop firewalld.service

[root@localhost ~]# setenforce 0

[root@localhost ~]# systemctl start ntpd.service
[root@localhost ~]#  //usr/sbin/ntpdate 192.168.120.129
  9 Sep 09:52:41 ntpdate[3062]: the NTP socket is in use, exiting

主服务器的时间源同步过去了

从2服务器进行从1服务器的同样操作,关闭防火墙,开启服务,进行时间同步

===================================================================================

安装MySQL(在三台服务器上安装MySQL:主1和从1从2)

创建挂载点abc

[root@localhost ~]# mkdir /abc

从宿主机192.168.100.1上挂载MySQL5.5的安装包

[root@localhost ~]# mount.cifs //192.168.100.1/mysql5.5 /abc

将安装包解压到opt目录中

[root@localhost LAMP]# tar zxvf mysql-5.5.24.tar.gz -C /opt/  

安装编译环境

[root@localhost LAMP]# cd /opt/mysql-5.5.24/

[root@localhost mysql-5.5.24]# yum install gcc gcc-c++ cmake –y

安装开发包

[root@localhost mysql-5.5.24]# yum install ncurses-devel bison libaio-devel –y

安装完成后进行编译:

[root@localhost mysql-5.5.24]# useradd -s /sbin/nologin mysql
[root@localhost mysql-5.5.24]# mkdir /usr/local/mysql

直接粘贴下面语句进行编译

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql      
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock           
-DDEFAULT_CHARSET=utf8                            
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_EXTRA_CHARSETS=all                         
-DWITH_MYISAM_STORAGE_ENGINE=1                     
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_MEMORY_STORAGE_ENGINE=1
-DWITH_READLINE=1                                 
-DENABLED_LOCAL_INFILE=1                           
-DMYSQL_DATADIR=/home/mysql                        
-DMYSQL_USER=mysql                                   
-DMYSQL_TCP_PORT=3306


[root@localhost mysql-5.5.24]# make

[root@localhost mysql-5.5.24]# make install

等待安装100%完成

(以上步骤三台都要同样操作)

===============================================================

安装完成后在主1上操作

[root@localhost mysql-5.5.24]# chown -R mysql.mysql /usr/local/mysql

\*开机时刷新*\
[root@localhost mysql-5.5.24]# export PATH=$PATH:/usr/local/mysql/bin/

[root@localhost mysql-5.5.24]# cp support-files/my-medium.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
[root@localhost mysql-5.5.24]# cp support-files/mysql.server /etc/init.d/mysqld

进行优化:

加权

[root@localhost mysql-5.5.24]# chmod 755 /etc/init.d/mysqld

开机自启动
[root@localhost mysql-5.5.24]# chkconfig --add /etc/init.d/mysqld

优先级
[root@localhost mysql-5.5.24]# chkconfig mysqld --level 35 on

--------------初始化数据库---------------------------------------------------------------

[root@localhost mysql-5.5.24]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --ldata=/var/lib/mysql --basedir=/usr/local/mysql --datadir=/home/mysql

/*直接建立软连接*/

[root@localhost mysql-5.5.24]# ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock

编辑配置文件

[root@localhost mysql-5.5.24]# vim /etc/init.d/mysqld

basedir=/usr/local/mysql                                         #在47行
datadir=/home/mysql                                              #在48行

开启服务

[root@localhost mysql-5.5.24]# systemctl start mysqld.service
Warning: mysqld.service changed on disk. Run 'systemctl daemon-reload' to reload units.

开启服务后直接使用mysql就能登录了(退出用quit、或者exit)

[root@localhost mysql-5.5.24]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.24-log Source distribution

Copyright (c) 2000, 2011, 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> quit
Bye

给管理员设置密码登录

[root@localhost mysql-5.5.24]# mysqladmin -u root password 'abc123'

[root@localhost mysql-5.5.24]# mysql -u root –p

Enter password: (密码为你自己设置的abc123)

MySQL5.5就安装完成了(主从三台同装)

===========================mysql主服务器配置=====================================

[root@localhost mysql-5.5.24]# vim /etc/my.cnf

server-id       = 11
log-bin=master-bin                         //主服务器日志文件//
log-slave-updates=true                     //从服务器更新二进制日志//(这条找不到可以自己在上一条log-bin下面插入)

改完配置文件重启服务

[root@localhost mysql-5.5.24]# systemctl restart mysqld.service
Warning: mysqld.service changed on disk. Run 'systemctl daemon-reload' to reload units.

登录MySQL

提权

mysql> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.120.%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

刷新,即时生效

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

查看主服务器状态

mysql> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      571 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

-----------------------------------------------------------mysql从服务器配置---------------------------------------------------

从1上操作:

[root@localhost mysql-5.5.24]# vim /etc/my.cnf

server-id       = 22                                        //id不能相同

在id下面添加这两句话

relay-log=relay-log-bin                         //从主服务器上同步日志文件记录到本地//
relay-log-index=slave-relay-bin.index           //定义relay-log的位置和名称//

重启服务

[root@localhost mysql-5.5.24]# systemctl restart mysqld.service
Warning: mysqld.service changed on disk. Run 'systemctl daemon-reload' to reload units.

mysql> change master to master_host='192.168.120.129',master_user='myslave',master_password='123456',master_log_file='master-bin.000s001',master_log_pos=571;
Query OK, 0 rows affected (0.01 sec)

开启

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

查看状态

mysql> show slave status\G;
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.120.129
                   Master_User: myslave
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: master-bin.000001
           Read_Master_Log_Pos: 571
                Relay_Log_File: relay-log-bin.000002
                 Relay_Log_Pos: 254
         Relay_Master_Log_File: master-bin.000001
              Slave_IO_Running: Yes                                            //必须为yes
             Slave_SQL_Running: Yes                                          //必须为yes
               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: 571
               Relay_Log_Space: 408
               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: 0
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: 11
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

(另一台从2和从1同样操作包括配置文件里的id,必须一致)

--------------------------------------验证主从同步--------------------------------------------------

在主服务器上操作

创建一个school的库

mysql> create database school;
Query OK, 1 row affected (0.00 sec)

查看数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql>

到两个从服务器上查看

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql>

主从同步没有问题

=====================安装amoeba服务器==================================

在amoeba服务器上操作(IP192.168.120.128)

关闭防火墙

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0

挂载

[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.100.1/Y2C /abc

[root@localhost ~]# cd /abc/Y2C

[root@localhost Y2C]# cp jdk-6u14-linux-x64.bin /usr/local/
[root@localhost Y2C]# cd /usr/local/
[root@localhost local]# ls
bin  etc  games  include  jdk-6u14-linux-x64.bin  lib  lib64  libexec  sbin  share  src

执行jdk(回车后不停回车向下翻,最后一个yes就完成了)
[root@localhost local]# ./jdk-6u14-linux-x64.bin

[root@localhost local]# mv jdk1.6.0_14/ /usr/local/jdk1.6

[root@localhost 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@localhost local]# source /etc/profile

[root@localhost local]# mkdir /usr/local/amoeba
[root@localhost local]#cd /abc/Y2C/

[root@localhost Y2C]# tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/

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

执行一下

[root@localhost local]# /usr/local/amoeba/bin/amoeba
amoeba start|stop(显示amoeba start|stop说明安装就成功了)

===============================================================

在三台mysql上添加权限开放给amoeba访问

三台MySQL都要输入这条命令

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

回到amoeba服务器上

[root@localhost local]# cd amoeba/

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

---30行--

amoeba
----32行---------
  123456

---117-去掉注释-
  master
  master
  slaves

====================================================

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

--26-29--去掉注释--
test
   
  123.com

-----42-主服务器地址---

  192.168.120.129
-------50行从1服务器--------------


                       
  192.168.120.130

        

-------57行从2服务器--------------

                 
                       
                        192.168.120.131
               

        

----------64行------------
 
 


--末尾70行--
slave1,slave2
 

保存退出

开启服务

[root@localhost amoeba]# /usr/local/amoeba/bin/amoeba start&
[1] 49453
[root@localhost amoeba]# log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
2018-09-09 13:28:18,721 INFO  context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-2.2.0
log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf
2018-09-09 13:28:25,023 INFO  net.ServerableConnectionManager - Amoeba for Mysql listening on 0.0.0.0/0.0.0.0:8066.
2018-09-09 13:28:25,026 INFO  net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:2582.
^C

查看java端口
[root@localhost amoeba]# netstat -anpt | grep java
tcp6       0      0 127.0.0.1:2582          :::*                    LISTEN      49453/java         
tcp6       0      0 :::8066                 :::*                    LISTEN      49453/java         
tcp6       0      0 192.168.120.128:41852   192.168.120.131:3306    ESTABLISHED 49453/java         
tcp6       0      0 192.168.120.128:33458   192.168.120.129:3306    ESTABLISHED 49453/java         
tcp6       0      0 192.168.120.128:37036   192.168.120.130:3306    ESTABLISHED 49453/java         
tcp6       0      0 192.168.120.128:41850   192.168.120.131:3306    ESTABLISHED 49453/java         
tcp6       0      0 192.168.120.128:33460   192.168.120.129:3306    ESTABLISHED 49453/java        

====================================客户端测试==========================================================

在客户端那台操作

第一步关闭防火墙

[root@localhost ~]# systemctl stop firewalld.service

[root@localhost ~]# setenforce 0

在线装MySQL

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

安装完毕后直接登录(IP为amoebaIP)

[root@localhost ~]# mysql -u amoeba -p123456 -h 192.168.120.128 -P8066
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1760388653
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 Source distribution

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

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

MySQL [(none)]>

代理服务器搭建就完成了

测试:

在master1主1上操作:

进入之前创建的school库:

mysql> use school;
Database changed

创建class表:
mysql> create table class (id int(10),name varchar(10),address varchar(20));
Query OK, 0 rows affected (0.04 sec)

到两个从里查看:

mysql> use school;
Database changed
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| class            |
+------------------+
1 row in set (0.00 sec)

mysql>

然后关闭同步:

stop slave;

到主服务器上插入数据

mysql> insert into class values('1','zhang','this_is_master');
Query OK, 1 row affected (0.00 sec)

到从1上插入数据

mysql> use school;
Database changed

mysql> insert into class values('2','zhang','this_is_slave1');
Query OK, 1 row affected (0.01 sec)

到从2上插入数据

mysql> use school;
Database changed
mysql> insert into class values('3','zhang','this_is_slave2');
Query OK, 1 row affected (0.01 sec)

在客户端上查看

Database changed
MySQL [school]> select * from class;
+------+-------+----------------+
| id   | name  | address        |
+------+-------+----------------+
|    3 | zhang | this_is_slave2 |
+------+-------+----------------+
1 row in set (0.01 sec)

MySQL [school]> select * from class;
+------+-------+----------------+
| id   | name  | address        |
+------+-------+----------------+
|    2 | zhang | this_is_slave1 |
+------+-------+----------------+
1 row in set (0.00 sec)

MySQL [school]> select * from class;
+------+-------+----------------+
| id   | name  | address        |
+------+-------+----------------+
|    3 | zhang | this_is_slave2 |
+------+-------+----------------+
1 row in set (0.01 sec)

在客户端上插入一条数据

MySQL [school]> insert into class values('5','zhang','write_test');
Query OK, 1 row affected (0.02 sec)

到主从上分别查看

主上:

mysql> select * from class;
+------+-------+----------------+
| id   | name  | address        |
+------+-------+----------------+
|    1 | zhang | this_is_master |
|    5 | zhang | write_test     |
+------+-------+----------------+
2 rows in set (0.00 sec)

从上:

mysql> select * from class;
+------+-------+----------------+
| id   | name  | address        |
+------+-------+----------------+
|    2 | zhang | this_is_slave1 |
+------+-------+----------------+
1 row in set (0.00 sec)

数据没更新

这就是主从复制,读写分离(现实中我们不会关闭从的同步,这是为了查看实验效果,实验完成后start slave;开启服务)


好了简单的读写分离就完成了,实验步骤较多,一定要仔细查看。