[root@localhost opt]# vim /etc/my.cnf
30 log_bin=master-bin
31 log_slave=updates=true32 server_id=21
【重启服务】
[root@localhost opt]# service mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL. SUCCESS!
修改从服务器配置文件
[root@localhost opt]# vim /etc/my.cnf
log-bin=mysql-bin 【开启二进制日志文件】
server_id=22 【slave1为22,slave2为23】
relay-log=relay-log-bin 【从服务器上同步日志文件记录到本地】
relay-log-index=slave-relay-bin.index 【确定relay-log的位置和名称(index索引)】
[root@localhost opt]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
配置数据库规则(主服务器授权)
[root@localhost opt]# mysql -uroot -p5514
mysql>grantreplication slave on*.*to'myslave'@'192.168.131.%' identified by '111222';
Query OK,0rows affected (0.00 sec)
mysql> flush privileges;
Query OK,0rows affected (0.00 sec)
【grant:授权】
【replication:复制】
【给给从服务器提权,允许其复制所有数据库的所有表(使用myslave的身份进行)】
【允许192.168.131网段,使用的密码为111222(第一个账户)】
【查看master数据库状态】
mysql>show master status;+-------------------+----------+--------------+------------------+-------------------+|File| Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+-------------------+----------+--------------+------------------+-------------------+| master-bin.000001|412||||+-------------------+----------+--------------+------------------+-------------------+
【新搭建的MySQL数据库的节点就为412】
【设置完从服务器的指向后查看】
mysql>show processlist;+----+---------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+| Id |User| Host | db | Command |Time| State | Info |+----+---------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+|2| root | localhost |NULL| Query |0| init |show processlist ||3| myslave |192.168.131.12:33438|NULL| Binlog Dump|93| Master has sent all binlog to slave; waiting for binlog to be updated |NULL||4| myslave |192.168.131.13:53300|NULL| Binlog Dump|93| Master has sent all binlog to slave; waiting for binlog to be updated |NULL|+----+---------+----------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+
配置数据库规则(从服务器指向)
[root@localhost opt]# mysql -uroot -p5514
mysql> change master to master_host='192.168.131.9',master_user='myslave',master_password='111222',master_log_file='master-bin.000001',master_log_pos=412;
mysql>start slave;
【查看下面两项为yes则同步成功(I/O,SQL,DUMP)】
mysql>show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4.5 验证主从复制
主服务器(master)
mysql>createdatabase qz;
mysql>use qz;
mysql>createtable qz1(id int(8),name varchar(10),age int(3));
mysql>insertinto qz1 values(1,'qq',20),(2,'ww',40);
mysql>select*from qz1;+------+------+------+| id | name | age |+------+------+------+|1| qq |20||2| ww |40|+------+------+------+
从服务器(slave)
mysql>showdatabases;+--------------------+|Database|+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+
【当主服务器创建完相关信息后再查看】
mysql>showdatabases;+--------------------+|Database|+--------------------+| information_schema || mysql || performance_schema || qz || test |+--------------------+
mysql>use qz;
mysql>showtables;+--------------+| Tables_in_qz |+--------------+| qz1 |+--------------+
mysql>select*from qz1;+------+------+------+| id | name | age |+------+------+------+|1| qq |20||2| ww |40|+------+------+------+
4.5 搭建读写分离
[root@localhost~]# systemctl stop firewalld.service
[root@localhost~]# setenforce 0[root@localhost~]# systemctl disable firewalld.service
[root@localhost~]# cd /opt/[root@localhost opt]# rz -E
rz waiting to receive.[root@localhost opt]# ls
amoeba-mysql-binary-2.2.0.tar.gz jdk-6u14-linux-x64.bin rh
[root@localhost opt]# cp jdk-6u14-linux-x64.bin /usr/local/[root@localhost opt]# cd /usr/local/[root@localhost local]# ls
bin etc games include jdk-6u14-linux-x64.bin lib lib64 libexec sbin share src
[root@localhost local]# ./jdk-6u14-linux-x64.bin 【可执行的二进制文件】
[root@localhost local]# chmod +x jdk-6u14-linux-x64.bin 【给该文件加可权限】
[root@localhost local]# ./jdk-6u14-linux-x64.bin
[root@localhost local]# ls
bin etc games include jdk1.6.0_14 jdk-6u14-linux-x64.bin lib lib64 libexec sbin share src
[root@localhost local]# mv jdk1.6.0_14//usr/local/jdk1.6[root@localhost local]# ls
bin etc games include jdk1.6 jdk-6u14-linux-x64.bin lib lib64 libexec sbin share src
[root@localhost local]# export JAVA_HOME=/usr/local/jdk1.6[root@localhost local]# export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
[root@localhost local]# export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin/:$PATH:$HOME/bin
[root@localhost local]# export AMOEBA_HOME=/usr/local/amoeba
[root@localhost local]# export PATH=$PATH:$AMOEBA_HOME/bin
[root@localhost local]# source /etc/profile
4.6 配置amoeba
[root@localhost local]# mkdir /usr/local/amoeba
[root@localhost local]# cd /opt/
【解压amoeba压缩包】
[root@localhost opt]# tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/[root@localhost opt]# chmod -R 755/usr/local/amoeba/
【测试是否安装成功】
[root@localhost opt]# /usr/local/amoeba/bin/amoeba
amoeba start|stop
【在三台MySQL服务器添加授权开发给amoeba访问】
mysql>grant all on *.* to qz2@'192.168.131.%' identified by '222333';[root@localhost opt]# cd /usr/local/amoeba/conf/[root@localhost conf]# ls
access_list.conf amoeba.dtd amoeba.xml dbserver.dtd dbServers.xml function.dtd functionMap.xml log4j.dtd log4j.xml rule.dtd ruleFunctionMap.xml rule.xml
[root@localhost conf]# vim amoeba.xml
【修改30行的账户名(数据库访问amoeba服务器时使用的账号)】
30<property name="user">amoeba</property>
【修改32行的账户密码(数据库访问amoeba服务器时使用账号时的密码)】
32<property name="password">333444</property>
【修改115行的默认池为主服务器】
115<property name="defaultPool">master</property>
【将117-120的注释“ <!---->”取消,即把120号复制到117号,然后修改118行的读池为主服务器,119行写池为从服务器】
117<!---->118<property name="writePool">master</property>119<property name="readPool">slaves</property>[root@localhost conf]# vim dbServers.xml
【将23行text修改为mysql】
23<property name="schema">mysql</property>
【将26行修改成amoeba访问三台数据库的账号】
26<property name="user">qz2</property>
【将28-30行的访问数据库密码修改成222333并取消注释】
28<!-- mysql password -->29<property name="password">222333</property>30
【将45行修改为数据库主服务器名】
45<dbServer name="master" parent="abstractServer">
【将48行修改成master服务器的ip】
48<property name="ipAddress">192.168.131.9</property>
【将52行修改成从服务器名】
52<dbServer name="slave1" parent="abstractServer">53<factoryConfig>54<!-- mysql ip -->
【将55行修改成从服务器地址】
55<property name="ipAddress">192.168.131.12</property>56</factoryConfig>57</dbServer>
【复制52-57行并添加第二台从服务器名和ip地址】
585960<dbServer name="slave2" parent="abstractServer">61<factoryConfig>62<!-- mysql ip -->63<property name="ipAddress">192.168.131.13</property>64</factoryConfig>65</dbServer>
【将66行的多个服务器池的名称修改为slaves】
66<dbServer name="slaves" virtual="true">
【在72行添加两个从服务器的服务器名(slave1,slave2)】
72<property name="poolNames">slave1,slave2</property>