码迷,mamicode.com
首页 > 数据库 > 详细

MySQL数据库实现主从同步与读写分离(实战!可跟做!)

时间:2019-11-24 22:29:31      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:密码   exe   轮询   ast   ecif   dev   数据读取   ica   信息   

实验拓扑图:

技术图片

MySQL主从同步:

一、安装NTP服务,同步时间

1、在MySQL主服务器上安装NTP服务

[root@master ~]# yum install ntp -y
.........//省略过程
[root@master ~]#

2、修改NTP服务配置文件

[root@master ~]# vim /etc/ntp.conf 
server 127.127.52.0      //本地时钟源
fudge 127.127.52.0 stratum 8     //设置时间层级为8
[root@master ~]#

3、开启服务NTP服务,关闭防火墙

[root@master ~]# systemctl start ntpd   //开启服务
[root@master ~]# systemctl stop firewalld.service    //关闭防火墙
[root@master ~]# setenforce 0   //关闭增强性安全功能

4、在MySQL从服务器1上,安装NTP服务,向主服务器同步时间

[root@slave1 ~]# yum install ntp ntpdate -y
........//省略过程
[root@slave1 ~]# systemctl start ntpd    //开启服务
[root@slave1 ~]# systemctl stop firewalld.service    //关闭防火墙
[root@slave1 ~]# setenforce 0   //关闭增强性安全功能
[root@slave1 ~]# /usr/sbin/ntpdate 192.168.52.133    //同步时间
22 Nov 14:43:50 ntpdate[46222]: the NTP socket is in use, exiting
[root@slave1 ~]# 

5、在MySQL从服务器2上,安装NTP服务,向主服务器同步时间

[root@slave2 ~]# yum install ntp ntpdate -y
........//省略过程
[root@slave2 ~]# systemctl start ntpd    //开启服务
[root@slave2 ~]# systemctl stop firewalld.service     //关闭防火墙
[root@slave2 ~]# setenforce 0   //关闭增强性安全功能
[root@slave2 ~]# /usr/sbin/ntpdate 192.168.52.133    //同步时间
22 Nov 14:46:12 ntpdate[69300]: the NTP socket is in use, exiting
[root@slave2 ~]# 

二、源码编译安装MySQL服务

1、将宿主机上的工具包共享出去

技术图片

2、将MySQL源码包解压到“/opt/”目录下

[root@master ~]# mkdir /mnt/tools    //创建目录
[root@master ~]# mount.cifs //192.168.100.50/tools /mnt/tools/    //挂载共享目录
Password for root@//192.168.100.50/tools:  
[root@master ~]# cd /mnt/tools/MySQL/
[root@master MySQL]# ls
amoeba-mysql-binary-2.2.0.tar.gz  jdk-6u14-linux-x64.bin  mysql-5.7.17.tar.gz
boost_1_59_0.tar.gz               mysql-5.5.24.tar.gz
[root@master MySQL]# tar zxvf mysql-5.5.24.tar.gz -C /opt/    //解压
........//省略过程

3、安装编译所需环境包

[root@master MySQL]# cd /opt/mysql-5.5.24/
[root@master mysql-5.5.24]# yum -y install > ncurses > ncurses-devel > bison > cmake > make > gcc > gcc-c++

4、创建mysql用户和安装目录

[root@master mysql-5.5.24]# useradd -s /sbin/nologin mysql    //创建用户
[root@master mysql-5.5.24]# mkdir /usr/local/mysql     //创建目录
[root@master mysql-5.5.24]# 

5、配置MySQL服务

[root@master mysql-5.5.24]# cmake > -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \    //安装路径
> -DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock \    //定义sock文件连接数据库文件
> -DDEFAULT_CHARSET=utf8  \    //指定字符集,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 \     //用户
> -DMSQL_TCP_PROT=3306   //端口

6、编译安装MySQL

[root@master mysql-5.5.24]# make && make install
.........//省略过程
[root@master mysql-5.5.24]#

7、对MySQL服务进行相关优化

[root@master mysql-5.5.24]# chown -R mysql.mysql /usr/local/mysql/   //修改属主属组
[root@master mysql-5.5.24]# 
[root@master mysql-5.5.24]# vim /etc/profile   //进入环境变量配置文件
export PATH=$PATH:/usr/local/mysql/bin/    //添加mysql环境变量
[root@master mysql-5.5.24]# source /etc/profile    //重新加载文件
[root@master mysql-5.5.24]# 
[root@master mysql-5.5.24]# cp support-files/my-medium.cnf /etc/my.cnf   //复制配置文件
cp: overwrite ‘/etc/my.cnf’? yes
[root@master mysql-5.5.24]# cp support-files/mysql.server /etc/init.d/mysqld    //复制管理文件
[root@master mysql-5.5.24]# 
[root@master mysql-5.5.24]# chmod 755 /etc/init.d/mysqld    //添加执行权限
[root@master mysql-5.5.24]# chkconfig --add /etc/init.d/mysqld    //让service服务能识别mysqld
[root@master mysql-5.5.24]# chkconfig mysqld --level 35 on   //在级别3、5中启动
[root@master mysql-5.5.24]#

8、初始化数据库

[root@master mysql-5.5.24]# /usr/local/mysql/scripts/mysql_install_db > --user=mysql \   //用户
> --ldata=/var/lib/mysql > --basedir=/usr/local/mysql \    //工作目录
> --datadir=/home/mysql    //数据目录

9、修改管理文件

[root@master mysql-5.5.24]# vim /etc/init.d/mysqld 
basedir=/usr/local/mysql    //指定工作目录
datadir=/home/mysql    //指定数据目录

10、开启MySQL服务

[root@master mysql-5.5.24]# service mysqld start 
Starting MySQL.. SUCCESS! 
[root@master mysql-5.5.24]# 

11、测试登录数据库

[root@master mysql-5.5.24]# mysqladmin -u root password ‘abc123‘   //设置登录用户密码
[root@master mysql-5.5.24]# mysql -u root -pabc123    //登录数据库
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
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> \q   //退出
Bye
[root@master mysql-5.5.24]# 

12、在从服务器slave1上安装MySQL数据库(过程同上,不多解释)

[root@slave1 ~]# mkdir /mnt/tools
[root@slave1 ~]# mount.cifs //192.168.100.50/tools /mnt/tools/
Password for root@//192.168.100.50/tools:  
[root@slave1 ~]# cd /mnt/tools/MySQL/
[root@slave1 MySQL]# ls
amoeba-mysql-binary-2.2.0.tar.gz  jdk-6u14-linux-x64.bin  mysql-5.7.17.tar.gz
boost_1_59_0.tar.gz               mysql-5.5.24.tar.gz
[root@slave1 MySQL]# tar zxvf mysql-5.5.24.tar.gz -C /opt/
.......//省略过程
[root@slave1 MySQL]# cd /opt/mysql-5.5.24/
[root@slave1 mysql-5.5.24]# useradd -s /sbin/nologin mysql
[root@slave1 mysql-5.5.24]# yum -y install > ncurses > ncurses-devel > bison > cmake > make > gcc > gcc-c++
..........//省略过程
[root@slave1 mysql-5.5.24]# 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 > -DMSQL_TCP_PROT=3306
...........//省略过程
[root@slave1 mysql-5.5.24]# make && make install
...........//省略过程
[root@slave1 mysql-5.5.24]# chown -R mysql.mysql /usr/local/mysql/
[root@slave1 mysql-5.5.24]# 
[root@slave1 mysql-5.5.24]# vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin/
[root@slave1 mysql-5.5.24]# source /etc/profile
[root@slave1 mysql-5.5.24]# 
[root@slave1 mysql-5.5.24]# cp support-files/my-medium.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? yes
[root@slave1 mysql-5.5.24]# cp support-files/mysql.server /etc/init.d/mysqld
[root@slave1 mysql-5.5.24]# 
[root@slave1 mysql-5.5.24]# chmod 755 /etc/init.d/mysqld 
[root@slave1 mysql-5.5.24]# chkconfig --add /etc/init.d/mysqld 
[root@slave1 mysql-5.5.24]# chkconfig mysqld --level 35 on
[root@slave1 mysql-5.5.24]# 
[root@slave1 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@slave1 mysql-5.5.24]# vim /etc/init.d/mysqld 
basedir=/usr/local/mysql
datadir=/home/mysql
[root@slave1 mysql-5.5.24]# service mysqld start 
Starting MySQL.. SUCCESS! 
[root@slave1 mysql-5.5.24]# 
[root@slave1 mysql-5.5.24]# mysqladmin -u root password ‘abc123‘
[root@slave1 mysql-5.5.24]# mysql -uroot -pabc123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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@slave1 mysql-5.5.24]# 

13、在从服务器slave2上安装MySQL数据库(过程同上,不多解释)

[root@slave2 ~]# mkdir /mnt/tools
[root@slave2 ~]# mount.cifs //192.168.100.50/tools /mnt/tools/
Password for root@//192.168.100.50/tools:  
[root@slave2 ~]# cd /mnt/tools/MySQL/
[root@slave2 MySQL]# ls
amoeba-mysql-binary-2.2.0.tar.gz  jdk-6u14-linux-x64.bin  mysql-5.7.17.tar.gz
boost_1_59_0.tar.gz               mysql-5.5.24.tar.gz
[root@slave2 MySQL]# tar zxvf mysql-5.5.24.tar.gz -C /opt/
...........//省略过程
[root@slave2 MySQL]# cd /opt/mysql-5.5.24/
[root@slave2 mysql-5.5.24]# useradd -s /sbin/nologin mysql
[root@slave2 mysql-5.5.24]# yum -y install > ncurses > ncurses-devel > bison > cmake > make > gcc > gcc-c++
...........//省略过程
[root@slave2 mysql-5.5.24]# 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 > -DMSQL_TCP_PROT=3306
...........//省略过程
[root@slave2 mysql-5.5.24]# make && make install
...........//省略过程
[root@slave2 mysql-5.5.24]# chown -R mysql.mysql /usr/local/mysql/
[root@slave2 mysql-5.5.24]# 
[root@slave2 mysql-5.5.24]# vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin/
[root@slave2 mysql-5.5.24]# source /etc/profile
[root@slave2 mysql-5.5.24]# 
[root@slave2 mysql-5.5.24]# cp support-files/my-medium.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? yes
[root@slave2 mysql-5.5.24]# cp support-files/mysql.server /etc/init.d/mysqld
[root@slave2 mysql-5.5.24]# 
[root@slave2 mysql-5.5.24]# chmod 755 /etc/init.d/mysqld 
[root@slave2 mysql-5.5.24]# chkconfig --add /etc/init.d/mysqld 
[root@slave2 mysql-5.5.24]# chkconfig mysqld --level 35 on
[root@slave2 mysql-5.5.24]# 
[root@slave2 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@slave2 mysql-5.5.24]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/home/mysql
[root@slave2 mysql-5.5.24]# service mysqld start 
Starting MySQL.. SUCCESS! 
[root@slave2 mysql-5.5.24]# 
[root@slave2 mysql-5.5.24]# mysqladmin -u root password ‘abc123‘
[root@slave2 mysql-5.5.24]# mysql -uroot -pabc123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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> \q
Bye
[root@slave2 mysql-5.5.24]# 

三、配置MySQL主服务器主从同步

1、修改MySQL主服务器配置文件

[root@master mysql-5.5.24]# vim /etc/my.cnf
log-bin=master-bin    //日志文件
log-slave-updates=true    //允许从服务器同步
server-id       = 11    //服务器id
[root@master mysql-5.5.24]# service mysqld restart    //重启服务
Shutting down MySQL. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@master mysql-5.5.24]#

2、创建一个myslave用户来让从服务器同步时使用

[root@master mysql-5.5.24]# mysql -u root -p
Enter password: 
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> grant replication slave on *.* to ‘myslave‘@‘192.168.52.%‘ 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.000008 |      338 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

mysql> quit    //退出
Bye
[root@master mysql-5.5.24]# 

四、配置MySQL从服务器1主从同步

1、修改MySQL主服务器配置文件

[root@slave1 mysql-5.5.24]# vim /etc/my.cnf
server-id       = 22    //服务器id
relay-log=relay-log-bin   //中继日志
relay-log-index=slave-relay-bin.index   //中继日志索引
[root@slave1 mysql-5.5.24]# service mysqld restart     //重启服务
Shutting down MySQL. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@slave1 mysql-5.5.24]# 

2、进入数据库,开启从服务器功能

[root@slave1 mysql-5.5.24]# mysql -u root -p
Enter password: 
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> change master to master_host=‘192.168.52.133‘,master_user=‘myslave‘,master_password=‘123456‘,master_log_file=‘master-bin.000008‘,master_log_pos=338;
//配置需要进行同步的MySQL主服务器的IP地址、同步用户、用户密码、日志文件、位置节点
Query OK, 0 rows affected (0.01 sec)

mysql> start slave;    //开启从服务器的同步服务
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;   //查看状态
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.52.133
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000008
          Read_Master_Log_Pos: 338
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-bin.000008
             Slave_IO_Running: Yes    //确认开启
            Slave_SQL_Running: Yes   //确认开启
              ..............//省略部分内容
             Master_Server_Id: 11
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

五、配置MySQL从服务器2主从同步

1、修改MySQL主服务器配置文件

[root@slave2 mysql-5.5.24]# vim /etc/my.cnf
server-id       = 23    //服务id
relay-log=relay-log-bin    //中继日志
relay-log-index=slave-relay-bin.index    //中继日志索引
[root@slave2 mysql-5.5.24]# service mysqld restart     //重启服务
Shutting down MySQL. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@slave2 mysql-5.5.24]# 

2、进入数据库,开启从服务器功能

[root@slave2 mysql-5.5.24]# mysql -u root -p
Enter password: 
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> change master to master_host=‘192.168.52.133‘,master_user=‘myslave‘,master_password=‘123456‘,master_log_file=‘master-bin.000008‘,master_log_pos=338;
//配置需要进行同步的MySQL主服务器的IP地址、同步用户、用户密码、日志文件、位置节点
Query OK, 0 rows affected (0.03 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.52.133
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000008
          Read_Master_Log_Pos: 338
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-bin.000008
             Slave_IO_Running: Yes    //确认开启
            Slave_SQL_Running: Yes    //确认开启
               ...........//省略部分内容
             Master_Server_Id: 11
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

六、验证主从同步

1、查看master服务器的数据库

mysql> show databases;   //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

2、查看slave1服务器的数据库

mysql> show databases;   //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)

mysql> 

3、查看slave2服务器的数据库

mysql> show databases;   //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)

mysql> 

4、在master服务器创建一个master数据库

mysql> create database master;    //创建数据库master
Query OK, 1 row affected (0.00 sec)

mysql> show databases;   //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| master             |       //创建成功
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> 

5、再次查看slave1服务器的数据库

mysql> show databases;   //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| master             |             //同步成功
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> 

6、再次查看slave2服务器的数据库

mysql> show databases;   //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| master             |             //同步成功
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> 

MySQL动静分离

一、安装jdk

1、关闭amoeba服务器防火墙

[root@amoeba ~]# systemctl stop firewalld.service     //关闭防火墙
[root@amoeba ~]# setenforce 0    //关闭增强性安全功能
[root@amoeba ~]# 

2、安装jdk1.6

[root@amoeba ~]# mkdir /mnt/tools    //创建挂载目录
[root@amoeba ~]# mount.cifs //192.168.100.50/tools /mnt/tools/     //挂载共享目录
Password for root@//192.168.100.50/tools:  
[root@amoeba ~]# cd /mnt/tools/MySQL/
[root@amoeba MySQL]# ls
amoeba-mysql-binary-2.2.0.tar.gz  jdk-6u14-linux-x64.bin  mysql-5.7.17.tar.gz
boost_1_59_0.tar.gz               mysql-5.5.24.tar.gz
[root@amoeba MySQL]# cp jdk-6u14-linux-x64.bin /usr/local/    //复制
[root@amoeba MySQL]# cd /usr/local/
[root@amoeba local]# ls
bin  games    jdk-6u14-linux-x64.bin  lib64    sbin   src
etc  include  lib                     libexec  share
[root@amoeba local]# ./jdk-6u14-linux-x64.bin      //直接执行
..............//省略介绍信息,回车即可
Do you agree to the above license terms? [yes or no]
yes      //同意条款

Press Enter to continue.....

Done.
[root@amoeba local]# 

3、重新命名jdk目录

[root@amoeba local]# ls
bin  games    jdk1.6.0_14             lib    libexec  share
etc  include  jdk-6u14-linux-x64.bin  lib64  sbin     src
[root@amoeba local]# mv jdk1.6.0_14/ /usr/local/jdk1.6
[root@amoeba local]# ls
bin  games    jdk1.6                  lib    libexec  share
etc  include  jdk-6u14-linux-x64.bin  lib64  sbin     src
[root@amoeba local]# 

4、配置环境变量

[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    //重新加载文件,让配置生效
[root@amoeba local]# 

二、安装amoeba

1、解压amoeba压缩包

[root@amoeba local]# mkdir /usr/local/amoeba   //创建工作目录
[root@amoeba local]# cd -     //回到到上次切换过来目录
/mnt/tools/MySQL
[root@amoeba MySQL]# tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/    //解压

2、修改目录权限

[root@amoeba MySQL]# cd -   //回到切换前目录
/usr/local
[root@amoeba local]# chmod -R 755 /usr/local/amoeba/    //修改目录权限,-R表示递归
[root@amoeba local]# /usr/local/amoeba/bin/amoeba
amoeba start|stop     //表示安装成功
[root@amoeba local]# 

3、分别给三台MySQL服务器添加amoeba服务器的访问权限

MySQL主服务器:master
mysql> grant all on *.* to test@‘192.168.52.%‘ identified by ‘123abc‘;
//添加一个test用户
Query OK, 0 rows affected (0.01 sec)

mysql> 
MySQL从服务器:slave1
mysql> grant all on *.* to test@‘192.168.52.%‘ identified by ‘123abc‘;
//添加一个test用户
Query OK, 0 rows affected (0.00 sec)

mysql> 
MySQL从服务器:slave2
mysql> grant all on *.* to test@‘192.168.52.%‘ identified by ‘123abc‘;
//添加一个test用户
Query OK, 0 rows affected (0.00 sec)

mysql> 

4、修改配置文件“amoeba.xml”

[root@amoeba local]# cd /usr/local/amoeba/
[root@amoeba amoeba]# vim conf/amoeba.xml
 30                                         <property name="user">amoeba</property>    //客户端用来访问amoeba的用户
 31 
 32                                         <property name="password">123456</property>   //用户密码

115                 <property name="defaultPool">master</property>    //默认MySQL池
116 
117                 <!-- -->
118                 <property name="writePool">master</property>   //写入数据池
119                 <property name="readPool">slaves</property>    //读取数据池
[root@amoeba amoeba]# 

5、修改配置文件“dbServers.xml”

[root@amoeba amoeba]# vim conf/dbServers.xml
 26                         <property name="user">test</property>    //用来访问数据库的用户
 27 
 28                         <!--  mysql password -->
 29                         <property name="password">123abc</property>   //用户密码

 42                 </poolConfig>
 43         </dbServer>
 44 
 45         <dbServer name="master"  parent="abstractServer">   //配置MySQL主服务器名
 46                 <factoryConfig>
 47                         <!-- mysql ip -->
 48                         <property name="ipAddress">192.168.52.133</property>   //配置主服务器IP地址
 49                 </factoryConfig>
 50         </dbServer>
 51 
 52         <dbServer name="slave1"  parent="abstractServer">   //配置MySQL从服务器名
 53                 <factoryConfig>
 54                         <!-- mysql ip -->
 55                         <property name="ipAddress">192.168.52.134</property>   //配置从服务器IP地址
 56                 </factoryConfig>
 57         </dbServer>
 58 
 59         <dbServer name="slave2"  parent="abstractServer">   //配置MySQL从服务器名
 60                 <factoryConfig>
 61                         <!-- mysql ip -->
 62                         <property name="ipAddress">192.168.52.148</property>   //配置从服务器IP地址
 63                 </factoryConfig>
 64         </dbServer>
 65 
 66         <dbServer name="slaves" virtual="true">   //数据池名
 67                 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
 68                         <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED     , 3=HA-->
 69                         <property name="loadbalance">1</property>
 70 
 71                         <!-- Separated by commas,such as: server1,server2,server1 -    ->
 72                         <property name="poolNames">slave1,slave2</property>    //数据池包含的服务器
 73                 </poolConfig>
 74         </dbServer>
[root@amoeba amoeba]#

6、后台开启amoeba服务

[root@amoeba amoeba]# /usr/local/amoeba/bin/amoeba start&   //开启服务,&表示在后台运行
[1] 98847
[root@amoeba amoeba]# log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
2019-11-24 13:42:31,951 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
2019-11-24 13:42:32,235 INFO  net.ServerableConnectionManager - Amoeba for Mysql listening on 0.0.0.0/0.0.0.0:8066.
2019-11-24 13:42:32,236 INFO  net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:34743.

7、重新开启一个终端,检查端口,确认服务是否开启

[root@amoeba ~]# netstat -ntap | grep 8066
tcp6       0      0 :::8066                 :::*                    LISTEN      98847/java          
[root@amoeba ~]# 

三、测试MySQL动静分离

1、关闭客户端防火墙

[root@client ~]# systemctl stop firewalld.service     //关闭防火墙
[root@client ~]# setenforce 0   //关闭增强性安全功能
[root@client ~]# 

2、直接用yum仓库安装数据库

[root@client ~]# yum install mysql -y
..............//省略安装过程
[root@client ~]#

3、用客户端通过amoeba服务器,连接数据库

[root@client ~]# mysql -u amoeba -p123456 -h 192.168.52.135 -P8066
//登录数据库,-u指定用户,-p输入密码,-h为amoeba服务器IP地址,-P跟端口
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1884603565
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)]> show databases;   //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| master             |               //之前创建的数据库
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

MySQL [(none)]> use master;    //使用数据库master
Database changed
MySQL [master]> show tables;   //查看表
Empty set (0.00 sec)    //数据库为空,没有表

MySQL [master]> 

4、分别查看MySQL主服务器和两个从服务器的master数据库

MySQL主服务器:master
mysql> show databases;    //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| master             |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> use master;   //使用数据库master
Database changed
mysql> show tables;   //查看表,为空
Empty set (0.00 sec)

mysql> 
MySQL从服务器:slave1
mysql> show databases;   //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| master             |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> use master;   //使用数据库master
Database changed
mysql> show tables;   //查看表,为空
Empty set (0.00 sec)

mysql> 
MySQL从服务器:slave2
mysql> show databases;   //查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| #mysql50#.mozilla  |
| master             |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> use master;   //使用数据库master
Database changed
mysql> show tables;   //查看表,为空
Empty set (0.00 sec)

mysql> 

5、在客户端创建一个表info

MySQL [master]> create table info (
    -> id int(4) not null primary key,
    -> name varchar(10) not null,
    -> score decimal(4,1) not null);
        //创建表info
Query OK, 0 rows affected (0.02 sec)

MySQL [master]> show tables;   //查看表
+------------------+
| Tables_in_master |
+------------------+
| info             |       //创建表成功
+------------------+
1 row in set (0.00 sec)

MySQL [master]> 

6、分别查看三台MySQL服务器的master数据库的表

MySQL主服务器:master
mysql> show tables;   //查看表
+------------------+
| Tables_in_master |
+------------------+
| info             |       //表存在
+------------------+
1 row in set (0.00 sec)

mysql> 
MySQL从服务器:slave1
mysql> show tables;   //查看表
+------------------+
| Tables_in_master |
+------------------+
| info             |      //表存在
+------------------+
1 row in set (0.00 sec)

mysql> stop slave;   //关闭主从同步
Query OK, 0 rows affected (0.01 sec)

mysql> 
MySQL从服务器:slave2
mysql> show tables;    //查看表
+------------------+
| Tables_in_master |
+------------------+
| info             |        //表存在
+------------------+
1 row in set (0.00 sec)

mysql> stop slave;    //关闭主从同步
Query OK, 0 rows affected (0.00 sec)

mysql> 

7、在服务端的info表,添加一条信息

MySQL [master]> insert into info (id,name,score) values (1,‘zhangsan‘,88);    //添加信息到info表
Query OK, 1 row affected (0.02 sec)

MySQL [master]> select * from info;   //查看info表信息
Empty set (0.00 sec)
//info表的信息为空,因为我们做了读写分离,数据只写入到了MySQL主服务器,
数据读取时在从服务器中读取的,而我们用关闭了从服务器主从同步服务,
此时从服务器中没有同步到数据,所以客户端这边读取不到数据。
MySQL [master]> 

8、再分别查看,三个MySQL服务器的info表信息

MySQL主服务器:master
mysql> select * from info;     //查看info表信息
+----+----------+-------+
| id | name     | score |
+----+----------+-------+
|  1 | zhangsan |  88.0 |    //信息写入成功
+----+----------+-------+
1 row in set (0.00 sec)

mysql> 
MySQL从服务器:slave1
mysql> select * from info;   //查看info表信息
Empty set (0.01 sec)   //无数据

mysql> start slave;   //开启主从同步
Query OK, 0 rows affected (0.00 sec)

mysql> 
MySQL从服务器:slave2
mysql> select * from info;   //查看info表信息
Empty set (0.01 sec)   //无数据

mysql> start slave;   //开启主从同步
Query OK, 0 rows affected (0.00 sec)

mysql> 

9、再次在服务端查看info表的数据

服务端
MySQL [master]> select * from info;   //查看info表数据
+----+----------+-------+
| id | name     | score |
+----+----------+-------+
|  1 | zhangsan |  88.0 |   //读取数据成功,因为开启主从同步后,从服务器同步到数据
+----+----------+-------+
1 row in set (0.00 sec)

MySQL [master]> 

10、分别在两个MySQL从服务器info表中添加不同数据

MySQL从服务器:slave1
mysql> insert into info (id,name,score) values (2,‘lisi‘,76);   //添加数据
Query OK, 1 row affected (0.02 sec)

mysql> select * from info;   //查看表数据
+----+----------+-------+
| id | name     | score |
+----+----------+-------+
|  1 | zhangsan |  88.0 |
|  2 | lisi     |  76.0 |          //添加数据成功
+----+----------+-------+
rows in set (0.00 sec)

mysql> 
MySQL从服务器:slave2
mysql> insert into info (id,name,score) values (3,‘wangwu‘,66);   //添加数据
Query OK, 1 row affected (0.00 sec)

mysql> select * from info;   //查看表数据
+----+----------+-------+
| id | name     | score |
+----+----------+-------+
|  1 | zhangsan |  88.0 |
|  3 | wangwu   |  66.0 |    //数据添加成功
+----+----------+-------+
2 rows in set (0.00 sec)

mysql> 

11、在客户端多次查看info表数据

服务端
MySQL [master]> select * from info;   //查看info表数据
+----+----------+-------+
| id | name     | score |
+----+----------+-------+
|  1 | zhangsan |  88.0 |
|  2 | lisi     |  76.0 |
+----+----------+-------+
2 rows in set (0.01 sec)
//从服务器slave1中数据
MySQL [master]> select * from info;   //查看info表数据
+----+----------+-------+
| id | name     | score |
+----+----------+-------+
|  1 | zhangsan |  88.0 |
|  3 | wangwu   |  66.0 |
+----+----------+-------+
2 rows in set (0.00 sec)
//从服务器slave2中数据
MySQL [master]> 
//可以看到客户端读取数据,是分别从两台从服务器中轮询读取的

谢谢观看!

MySQL数据库实现主从同步与读写分离(实战!可跟做!)

标签:密码   exe   轮询   ast   ecif   dev   数据读取   ica   信息   

原文地址:https://blog.51cto.com/14449541/2453072

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!