Mysql基础拓扑图:
Mysql环境准备:
一台mysql主服务器(安装mysql)
两台mysql从服务器(安装mysql)
一台mysql代理(安装amoeba和java环境)
一台mysql客户端(mysql客户端)
部署前先关闭所有的iptables,selinux
Mysql的主从复制读写分离所需安装包:
cmake-2.8.6.tar.gz
mysql-5.5.22.tar.gz
amoeba-mysql-binary-2.2.0.tar.gz
jdk-7u65-linux-x64.tar.gz
jdk-6u14-linux-x64.bin
ncurses-devel
部署一个时间服务器来进行时间同步:
yum -y install ntp
编辑配置文件添加如下行:
server 127.127.1.0
fudge 127.127.1.0 stratum 8
重启服务:
客户端安装软件同步:
yum -y install ntp-date
/usr/sbin/ntpdate 99.99.99.22
安装mysql独特的编译安装软件cmake:
tar zxf /root/cmake-2.8.6.tar.gz -C /root/
cd /root/cmake-2.8.6
./configure && gmake && gmake install
安装mysql依赖包:
yum -y install ncurses-devel
编译安装mysql
tar zxf /root/mysql-5.5.22.tar.gz -C /usr/src
cd /usr/src/mysql-5.5.22
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all
make && make install
授权数据库用户:
useradd -M -s /sbin/nologin mysql -g mysql
chown -R mysql:mysql /usr/local/mysql
初始化数据库:
/usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data/
优化数据库
cp -rf /usr/src/$MY_Q/support-files/my-medium.cnf /etc/my.cnf
cp -rf /usr/src/$MY_Q/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
启动数据库并设置密码:
echo "PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
source /etc/profile
/etc/init.d/mysqld start
mysqladmin -uroot password '123123'
配置Mysql主服务器
vim /etc/my.cnf
server-id = 11 #改
log-bin=master-bin #改
log-slave-updates=true #添加
重启服务:
/etc/init.d/mysqld restart
登陆Mysql给服务器授权
[root@cjl ~]# mysql -uroot -p123123 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.5.22-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'@'99.99.99.%' identified by '123123'; 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.000002 | 336 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
从服务器的数据库与主服务器一致,不需要配置任何命令。
配置第一个从服务器添加如下行:
vim /etc/my.cnf
server-id = 22 #改
relay-log=relay-log-bin #添加
relay-log-index=slave-relay-bin.index #添加
重启服务:
service mysqld restart
配置数据库:
[root@cjl ~]# mysql -uroot -p123123 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.5.22-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='99.99.99.22',master_user='myslave',master_password='123123',master_log_file='master-bin.000001',master_log_pos=336; start slave; show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes
配置第二个从服务器添加如下行:
vim /etc/my.cnf
server-id = 33 #改
relay-log=relay-log-bin #添加
relay-log-index=slave-relay-bin.index #添加
重启服务:
/etc/init.d/mysqld restart
配置mysql数据库:
[root@cjl ~]# mysql -uroot -p123123 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.5.22-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='99.99.99.22',master_user='myslave',master_password='123123',master_log_file='master-bin.000001',master_log_pos=336; start slave; show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes
验证主从复制的效果
登陆主服务器,创建数据库:
从服务器查看:
实现Mysql的读写分离
登陆mysql数据库对用户授权:
grant all on *.* to cjl@'99.99.99.%' identified by '123456';
给予脚本执行权限:
chmod +x jdk-6u14-linux-x64.bin
执行
./jdk-6u14-linux-x64.bin
移动jdk1.6.0_14/ /usr/local/java
编辑配置文件添加java的全局变量:
export JAVA_HOME=/usr/local/java
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
使用当前shell执行
source /etc/profile
查看java的版本:
java -version
安装配置amoeba(变形虫),实现数据库的负载均衡读写分离。
mkdir /usr/local/amoeba
tar zxf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba
chmod -R 755 /usr/local/amoeba
常识amoeba这个命令,查看是否安装成功。
/usr/local/amoeba/bin/amoeba
配置amoeba.xml的配置文件添加代理用户和密码,和pool:
cd /usr/local/amoeba
vim conf/amoeba.xml
注意如果图片所示内容中出现这样的注释符号请去掉:
配置dbServer.xml添加授权的测试用户和添加pool中的服务器:
vim conf/dbServer.xml
配置主从服务器的池:
确认配置无误我们启动amoeba软件,查看8066端口是否监听。
测试读写分离和代理服务器:
连接代理服务器:
在master上创建一个表,同步到从服务器上,关闭slave主从复制再插入区别语句.
mysql> use hehe; Database changed mysql> create table cjl (id int(10),name varchar(10),address varchar(20)); Query OK, 0 rows affected (0.07 sec) mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql>
主mysql测试语法如下:
insert into cjl values('1','zhang','the_is_master');
从的mysql测试语法如下:
insert into cjl values('1','zhang','the_is_slave1');
从的mysql测试语法如下:
insert into cjl values('1','zhang','the_is_slave2');
客户端查看:
select * from cjl
上面的从服务器使用了RR算法,一人一次。
客户端插入一条语言测试:
客户端上竟然不能查询到数据,数据哪里去了???
然而他却在master上...
本次mysql的主从复制读写分离结束,有新错误请留言。