1.建立时间同步环境,在主节点上搭建时间同步服务器。
1)安装NTP
[root@mysql ~]# yum -y install ntp
2)配置NTP
[root@mysql ~]# vim /etc/ntp.conf //添加如下两行
server 127.127.1.0
fudge 127.127.1.0 stratum 8
3)重启服务并设置为开机启动
[root@mysql ~]# systemctl restart ntpd
[root@mysql ~]# systemctl enable ntpd
2.在从节点上进行时间同步。
[root@mysql ~]# yum -y install ntpdate
[root@mysql ~]# ntpdate 192.168.1.101 //同步主时间服务器
1)安装mysql数据库。在master,slave1,slave2上安装。
编译安装mysql
2)优化调整。
3)初始化数据库。
4)启动mysql服务
[root@mysql ~]# systemctl restart mysqld
[root@mysql ~]# msqladmin -u root password 'pwd123' //为root用户设置密码
3.配置mysql master主服务器
[root@mysql ~]# vim /etc/my.cnf
server_id = 11 //修改
log_bin = master-bin //修改
log-slave-updates = true //增加
重启mysql服务
[root@mysql ~]# systemctl restart mysqld.service
1)登录mysql程序,给从服务器以授权。
[root@mysql ~]# mysql -u root -p
mysql> grant replication slave on *.* to 'myslave'@'192.168.1%' identified by '123456';
mysql> flush privileges;
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 409 | | | |
+-------------------+----------+--------------+------------------+-------------------+
4.配置从服务器
[root@mysql ~]# vim /etc/my.cnf
server_id = 22 //修改
relay-log=relay-log-bin //增加
relay-log-index=slave-relay-bin.index //增加
重启mysql服务
[root@mysql ~]# systemctl restart mysqld
1)登录mysql,配置同步
[root@mysql ~]# mysql -u root -p
mysql> change master to master_host='192.168.1.101',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=409;
2)启动同步
mysql> start slave;
3)查看slave状态,确保以下两个值为yes
mysql> show slave status\G;
5.验证主从复制效果
1)在主,从服务器上登录mysql
[root@mysql ~]# mysql -u root -p
mysql> show databases;
2)在主服务器上新建数据库如db_test
mysql> create database db_test;
Query OK, 1 row affected (0.00 sec)
3)在主,从服务器上分别查看数据库,显示数据库相同,则主从复制成功。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_test |
| mysql |
| performance_schema |
| test |
| tttttt |
+--------------------+
1.2搭建mysql读写分离
1)在主机amoeba上安装java环境
[root@localhost ~]# mount /dev/cdrom /media/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost ~]# cd /media/
[root@localhost media]# ls
amoeba-mysql-binary-2.2.0.tar.gz jdk-6u14-linux-x64.bin
[root@localhost media]# cp * /usr/src/
[root@localhost media]# cd /usr/src/
[root@localhost src]# chmod +x jdk-6u14-linux-x64.bin
[root@localhost src]# ./jdk-6u14-linux-x64.bin
[root@localhost src]# mv jdk1.6.0_14/ /usr/local/jdk1.6
[root@localhost src]# vim /etc/profile
2)配置amoeba读写分离,两个slave读负载均衡
master,slave1,slave2中开放权限amoeba访问
mysql> grant all on *.* to test@'192.168.1.%' identified by '123.com';
[root@localhost ~]# cd /usr/local/amoeba/
[root@localhost amoeba]# vim conf/amoeba.xml
增加以下配置:
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 src]# source /etc/profile
[root@localhost src]# 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)
java环境已配置成功
3)安装并配置amoeba软件
[root@localhost ~]# mkdir /usr/local/amoeba
[root@localhost media]# tar zxf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba
[root@localhost ~]# chmod -R 755 /usr/local/amoeba/
[root@localhost ~]# /usr/local/amoeba/bin/amoeba
amoeba start|stop //显示此内容说明amoeba安装成功
1.配置amoeba读写分离,两个slave读写负载均衡。
master,slave1,slave2中开放权限给amoeba访问。
mysql> grant all on *.* to test@'192.168.1.%' identified by '123.com';
Query OK, 0 rows affected (0.00 sec
1)编辑amoeba.xml配置文件
[root@localhost ~]# cd /usr/local/amoeba/
[root@localhost amoeba]# vim conf/amoeba.xml
以下修改后的内容为带下划线的部分
以下修改后的内容为带下划线的部分,注意删除注释
2)编辑dbservers.xml配置文件
[root@localhost amoeba]# vim conf/dbServers.xml
以下修改后的内容为带下划线的部分
以下修改后的内容为带下划线的部分
3)配置无误后可以启动amoeba软件,其默认端口为tcp8066
[root@localhost amoeba]# bin/amoeba start&
2.测试 在Client主机上:
[root@localhost ~]# yum -y install mysql
Loaded plugins: fastestmirror, langpacks
aaa | 3.6 kB 00:00:00
(1/2): aaa/group_gz | 155 kB 00:00:00
(2/2): aaa/primary_db | 3.0 MB 00:00:00
Determining fastest mirrors
可以通过代理访问mysql:
[root@localhost ~]# mysql -u amoeba -p123456 -h 192.168.1.110 -P8066
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2089015486
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
1)在master上创建一个表,同步到从服务器上,然后关掉各从服务器的slave功能,在插入区别语句。
mysql> use db_test;
mysql> create table zang (id int(10),name varchar(10),address varchar(20));
Query OK, 0 rows affected (0.09 sec)
分别在两台从服务器上:
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
2)从服务器上同步了表,手动插入其他内容: