------------客户-----------amoeba----------mysql主-----------mysql从1----------mysql2-------
---192.168.1.10---192.168.1.100----192.168.1.200--------192.168.1.201-----192.168.1.202---
都在NAT模式下,主装有mysql,从没有装。
主从搭建:
Mysql主:
[root@lhy 桌面]# /etc/init.d/NetworkManager stop
停止 NetworkManager 守护进程: [确定]
[root@lhy 桌面]# ifconfig eth0 192.168.1.200
[root@lhy 桌面]# vim /etc/my.cnf
[root@lhy 桌面]# service mysqld restart
Shutting down MySQL. [确定]
Starting MySQL... [确定]
[root@lhy 桌面]# mysql -uroot -p123.com
mysql> grant replication slave on *.* to 'slave'@'192.168.1.%' identified by '123.com';
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 | 336 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
Mysql从1:
[root@lhy 桌面]# /etc/init.d/NetworkManager stop
停止 NetworkManager 守护进程: [确定]
[root@lhy 桌面]# ifconfig eth0 192.168.1.201
[root@lhy 桌面]# yum -y install mysql mysql-server
[root@lhy 桌面]# service mysqld start
[root@lhy 桌面]# mysqladmin -u root password '123.com'
[root@lhy 桌面]# vim /etc/my.cnf
[root@lhy 桌面]# service mysqld restart
停止 mysqld: [确定]
正在启动 mysqld: [确定]
[root@lhy 桌面]# mysql -uroot -p123.com
mysql> Change master to master_host='192.168.1.200',master_user='slave',master_password='123.com',master_log_file='master-bin.000001',master_log_pos=336;
Query OK, 0 rows affected (0.08 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.1.200
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 336
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@lhy 桌面]# scp /etc/my.cnf 192.168.1.202:/etc/my.cnf
The authenticity of host '192.168.1.202 (192.168.1.202)' can't be established.
RSA key fingerprint is 58:df:14:66:b9:07:ec:a5:ab:fd:74:55:46:59:ad:5c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.202' (RSA) to the list of known hosts.
root@192.168.1.202's password:
my.cnf 100% 333 0.3KB/s 00:00
Mysql从2:
[root@lhy 桌面]# /etc/init.d/NetworkManager stop
停止 NetworkManager 守护进程: [确定]
[root@lhy 桌面]# ifconfig eth0 192.168.1.202
[root@lhy 桌面]# mysqladmin -u root password '123.com'
[root@lhy 桌面]# yum -y install mysql mysql-server
[root@lhy 桌面]# service mysqld start
正在启动 mysqld: [确定]
[root@lhy 桌面]# vim /etc/my.cnf
[root@lhy 桌面]# service mysqld restart
停止 mysqld: [确定]
正在启动 mysqld: [确定]
[root@lhy 桌面]# mysql -uroot -p123.com
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
ysql> Change master to master_host='192.168.1.200',master_user='slave',master_password='123.com',master_log_file='master-bin.000001',master_log_pos=336;
Query OK, 0 rows affected (0.04 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.1.200
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 336
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
一主两从搭建成功 !!!
Amoeba:
[root@lhy 桌面]# /etc/init.d/NetworkManager stop
停止 NetworkManager 守护进程: [确定]
[root@lhy 桌面]# ifconfig eth0 192.168.1.100
[root@lhy 桌面]# iptables -F
[root@lhy 桌面]# setenforce 0
Windows共享目录到虚拟机
[root@lhy 桌面]# cd /mnt/hgfs/anzhuangbao/
[root@lhy anzhuangbao]# chmod +x jdk-6u14-linux-x64.bin
[root@lhy anzhuangbao]# ./jdk-6u14-linux-x64.bin
一路回车,输入yes
出现下图表示成功
root@lhy anzhuangbao]# rm -rf /usr/bin/java //删除原有的Java
[root@lhy anzhuangbao]# rm -rf /usr/bin/javac //删除原有的javac
[root@lhy anzhuangbao]# mv jdk1.6.0_14/ /usr/local/java
[root@lhy anzhuangbao]# mkdir /usr/bin/java
[root@lhy anzhuangbao]# ln -s /usr/local/java/bin/* /usr/bin/java/
[root@lhy anzhuangbao]# vim /etc/profile
最后一行添加四行环境变量
export JAVA_HOME=/usr/local/java
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/lib:$JAVA_HOME/bin:$PATH:$JAVA_HOME/bin
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME/bin
保存退出
启动一下
[root@lhy anzhuangbao]# source /etc/profile
查看版本 1.60_14说明是正确的
[root@lhy anzhuangbao]# java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)
安装amoeba(变形虫)
[root@lhy anzhuangbao]# mkdir /usr/local/amoeba
[root@lhy anzhuangbao]# tar -zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
给权限
[root@lhy anzhuangbao]# chmod -R 755 /usr/local/amoeba/
[root@lhy anzhuangbao]# amoeba //验证amoeba是否安装成功
amoeba start|stop
[root@lhy anzhuangbao]# amoeba start & //启动amoeba
[1] 3323
[root@lhy anzhuangbao]# log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
2018-08-15 23:15:20,057 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-08-15 23:15:20,985 INFO net.ServerableConnectionManager - Amoeba for Mysql listening on 0.0.0.0/0.0.0.0:8066.
2018-08-15 23:15:21,064 INFO net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:56540.
[root@lhy anzhuangbao]# netstat -antp |grep java
tcp 0 0 ::ffff:127.0.0.1:56540 :::* LISTEN 3323/java
tcp 0 0 :::8066 :::* LISTEN 3323/java
去三台mysql上都给授权(操作一样)
mysql> grant all on *.* to 'root'@'192.168.1.%' identified by '123.com';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
Amoeba上安装mysql:
[root@lhy anzhuangbao]# yum -y install mysql
登录后端mysql库
[root@lhy anzhuangbao]# mysql -uroot -h 192.168.1.200 -p123.com
[root@lhy anzhuangbao]# mysql -uroot -h 192.168.1.201 -p123.com
[root@lhy anzhuangbao]# mysql -uroot -h 192.168.1.202 -p123.com
验证成功!!!
修改amoeba配置文件
[root@lhy anzhuangbao]# cd /usr/local/amoeba/
[root@lhy amoeba]# ls
benchmark bin changelogs.txt conf lib LICENSE.txt logs README.html
[root@lhy amoeba]# ls conf/
access_list.conf dbserver.dtd functionMap.xml rule.dtd
amoeba.dtd dbServers.xml log4j.dtd ruleFunctionMap.xml
amoeba.xml function.dtd log4j.xml rule.xml
[root@lhy amoeba]# ls conf/amoeba.xml
conf/amoeba.xml
[root@lhy amoeba]# ls conf/dbServers.xml
conf/dbServers.xml
[root@lhy amoeba]# vim conf/amoeba.xml
[root@lhy amoeba]# vim conf/dbServers.xml
[root@lhy amoeba]# amoeba stop &
[2] 3449
[root@lhy amoeba]# amoeba server shutting down with port=56540
2018-08-15 23:53:50,800 WARN net.ServerableConnectionManager - Amoeba for Mysql shutdown completed!
2018-08-15 23:53:50,801 WARN net.ServerableConnectionManager - Amoeba Monitor Server shutdown completed!
[1]- Done amoeba start (wd: /mnt/hgfs/anzhuangbao)
(wd now: /usr/local/amoeba)
[2]+ Done amoeba stop
[root@lhy amoeba]# amoeba start &
[1] 3465
[root@lhy amoeba]# log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
2018-08-15 23:54:04,033 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-08-15 23:54:04,948 INFO net.ServerableConnectionManager - Amoeba for Mysql listening on /192.168.1.100:8066.
2018-08-15 23:54:04,968 INFO net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:34847.
[root@lhy amoeba]# netstat -antp |grep java
tcp 0 0 ::ffff:127.0.0.1:34847 :::* LISTEN 3465/java
tcp 0 0 ::ffff:192.168.1.100:8066 :::* LISTEN 3465/java
tcp 0 0 ::ffff:192.168.1.100:50180 ::ffff:192.168.1.201:3306 ESTABLISHED 3465/java
tcp 0 0 ::ffff:192.168.1.100:51439 ::ffff:192.168.1.200:3306 ESTABLISHED 3465/java
tcp 0 0 ::ffff:192.168.1.100:56809 ::ffff:192.168.1.202:3306 ESTABLISHED 3465/java
Client:
[root@lhy 桌面]# /etc/init.d/NetworkManager stop
停止 NetworkManager 守护进程: [确定]
[root@lhy 桌面]# ifconfig eth0 192.168.1.10
[root@lhy 桌面]# iptables -F
[root@lhy 桌面]# setenforce 0
[root@lhy 桌面]# yum -y install mysql
[root@lhy 桌面]# mysql -u amoeba -p123.com -h 192.168.1.100 -P8066
主库:mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.04 sec)
mysql> create database mysqldb;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mysqldb |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use mysqldb;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table biao1 (id int(5))
-> ;
Query OK, 0 rows affected (0.06 sec)
mysql> show tables;
+-------------------+
| Tables_in_mysqldb |
+-------------------+
| biao1 |
+-------------------+
1 row in set (0.00 sec)
去从库中进行验证查看biao1,两个从库一样。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mysqldb |
| test |
+--------------------+
4 rows in set (0.01 sec)
mysql> use mysqldb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> use mysqldb;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_mysqldb |
+-------------------+
| biao1 |
+-------------------+
1 row in set (0.00 sec)
验证成功!!!
验证‘写’
去客户端的mysqldb库中创建表‘biao2’
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mysqldb |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.04 sec)
mysql> use mysqldb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> use mysqldb;
Database changed
mysql> create table biao2 (id int(5));
Query OK, 0 rows affected (0.15 sec)
mysql> show tables;
+-------------------+
| Tables_in_mysqldb |
+-------------------+
| biao1 |
| biao2 |
+-------------------+
2 rows in set (0.00 sec)
去主数据可和从数据库的mysqldb库中查看,都出现了表‘biao2’
说明是写入了主库中
验证‘读’
去从(192.168.1.201)中的mysqldb中在表‘biao2’中插入一条数据,并查询一下
mysql> insert into biao2 values(1);
Query OK, 1 row affected (0.01 sec)
mysql> select * from biao2;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
去从2(192.168.1.202)中添加数据(2)
mysql> use mysqldb;
Database changed
mysql> insert into biao2 values(2);
Query OK, 1 row affected (0.00 sec)
mysql> select * from biao2;
+------+
| id |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
此时两个从mysqldb库的表biao2的数据是不一样的
去客户端验证查看
mysql> select * from biao2;
+------+
| id |
+------+
| 2 |
+------+
1 row in set (0.02 sec)
mysql> select * from biao2;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.01 sec)
出现轮询效果,说明‘读’的时候是读的从库
至此,读写分离完成
删除表:drop table 删除的表名
删除表里面的数据:delete from 表名 where id=?
搭建好数据库以后直接mysql 进库
Set password=password (‘123.com’);
Exit
重新进库,输入密码!