一、环境
mysql-db1(192.168.1.251)
mysql-db2(192.168.1.252)
writer vip 192.168.1.180
read vip 192.168.1.181 192.168.1.182
二、软件安装
1、mysql安装及用户添加
tar -zxvf mysql-5.1.31.tar.gz
cd mysql-5.1.31
./configure --prefix=/usr/local/mysql --with-charset=utf8 --with-extra-charsets=all --enable-assembler --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=innobase
make;make install
groupadd mysql
useradd -g mysql mysql
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
sed -i 's/skip-federated/#skip-federated/g' /etc/my.cnf
/usr/local/mysql/bin/mysql_install_db --user=mysql
chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql/.
cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig --level 345 mysql on
echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
ldconfig
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
service mysql start
/usr/local/mysql/bin/mysqladmin -u root password root
service mysql stop
service mysql start
cd ..
grant all on *.* to root@'%' identified by 'root'; //远程连接用的
grant replication slave,file on *.* to replicant@'%' identified by 'slave'; //主主用的
grant super,replication client,process on *.* to mmm_agent@'%' identified by 'RepAgent'; //mmm用的
grant replication client on *.* to mmm_monitor@'%' identified by 'RepMonitor'; //也是mm用的
2、mysql主主安装
[root@jboss1 ~]#vi /etc/my.cnf
....
server-id = 2
replicate-do-db=akin
replicate-ignore-db=
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
master-host = 192.168.1.252
master-user = replicant
master-password = slave
master-port = 3306
[root@jboss2 ~]# vi /etc/my.cnf
.....
server-id = 1
replicate-do-db=akin
replicate-ignore-db=
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
master-host = 192.168.1.251
master-user = replicant
master-password = slave
master-port = 3306
启动调试,至到主主没问题后,我们再来安装mysql-mmmm
3、mysql-mmm安装及配置
(1)升级perl模块
cpan -i Algorithm::Diff Class::Singleton DBI DBD::mysql Log::Dispatch Log::Log4perl Mail::Send Net::Ping Proc::Daemon Time::HiRes Params::Validate Net::ARP
这两个File::Basename File::stat模块好像安装不上,要升级5.12.2才可以,不安装也可以使用,Net:ARP必须要安装,要不然VIP会出不来的!
(2)下载并安装mysql-mmm
wget http://mysql-mmm.org/_media/:mmm2:mysql-mmm-2.2.1.tar.gz -O mysql-mmm-2.2.1.tar.gz
tar -zxvf mysql-mmm-2.2.1.tar.gz
cd mysql-mmm-2.2.1
make;make install
cd ..
(3)mysql-mmm配置
这个也非常简单,真接在配置文件里面修改就OK了!
[root@jboss1 mysql-mmm]# ls /etc/mysql-mmm/
mmm_agent.conf mmm_common.conf mmm_mon.conf mmm_tools.conf
[root@jboss1 mysql-mmm]# cat mmm_agent.conf
include mmm_common.conf
this db1
//如果在第二台机器上,db1那里做相应的更改
[root@jboss1 mysql-mmm]# cat mmm_common.conf
active_master_role writer
<host default>
cluster_interface eth0
pid_path /var/run/mmm_agentd.pid
bin_path /usr/lib/mysql-mmm/
replication_user replication
replication_password slave
agent_user mmm_agent
agent_password RepAgent
</host>
<host db1>
ip 192.168.1.251
mode master
peer db2
</host>
<host db2>
ip 192.168.1.252
mode master
peer db1
</host>
<role writer>
hosts db1, db2
ips 192.168.1.180
mode exclusive
</role>
<role reader>
hosts db1, db2
ips 192.168.1.181,192.168.1.182
mode balanced
</role>
//只有两种模式:exclusive是排他,在这种模式下任何时候只能一个host拥有该角色,balanced模式下可以多个host同时拥有此角色。一般writer是ex,reader是ba
[root@jboss1 mysql-mmm]# cat mmm_mon.conf
include mmm_common.conf
<monitor>
ip 127.0.0.1
pid_path /var/run/mmm_mond.pid
bin_path /usr/lib/mysql-mmm/
status_path /var/lib/misc/mmm_mond.status
auto_set_online 5
ping_ips 192.168.1.251,192.168.1.252
</monitor>
<host default>
monitor_user mmm_monitor
monitor_password RepMonitor
</host>
debug 0
//auto_set_online 5为自动切换的时候,单位为秒
scp mmm_common.conf root@192.168.1.252:/etc/mysql-mmm/
//把mmm_common.conf复制到DB2上!
配置db2
其他不用动,修改
[root@jboss2 mysql-mmm]# cat mmm_agent.conf
include mmm_common.conf
this db2
//this这里修改为db2就好了,,这里对应mmmm_common.conf里面的内容
(4)启动mmm的服务
DB1启动agent和mon
/etc/init.d/mysql-mmm-agent start
/etc/init.d/mysql-mmm-monitor start
DB2启动agent
/etc/init.d/mysql-mmm-agent start
(5)查看及相关
[root@jboss1 ~]# mmm_control ping
OK: Pinged successfully!
如果是连不上,,请查看mmm_mon.conf里面的配置!
[root@jboss1 ~]# mmm_control show
db1(192.168.1.251) master/ONLINE. Roles: reader(192.168.1.182), writer(192.168.1.180)
db2(192.168.1.252) master/ONLINE. Roles: reader(192.168.1.181)
显示信息
[root@jboss1 ~]# mmm_control checks
db2 ping [last change: 2010/11/04 22:19:42] OK
db2 mysql [last change: 2010/11/04 22:48:03] OK
db2 rep_threads [last change: 2010/11/04 22:19:42] OK
db2 rep_backlog [last change: 2010/11/04 22:19:42] OK: Backlog is null
db1 ping [last change: 2010/11/04 22:19:42] OK
db1 mysql [last change: 2010/11/04 22:19:42] OK
db1 rep_threads [last change: 2010/11/04 22:48:53] OK
db1 rep_backlog [last change: 2010/11/04 22:19:42] OK: Backlog is null
检测是否正常
[root@jboss1 ~]# mmm_control set_online db1
OK: This host is already ONLINE. Skipping command.
[root@jboss1 ~]# mmm_control set_online db2
OK: This host is already ONLINE. Skipping command.
设置服务器上线
[root@jboss1 ~]# mmm_control help
Valid commands are:
help - show this message
ping - ping monitor
show - show status
checks [<host>|all [<check>|all]] - show checks status
set_online <host> - set host <host> online
set_offline <host> - set host <host> offline
mode - print current mode.
set_active - switch into active mode.
set_manual - switch into manual mode.
set_passive - switch into passive mode.
move_role [--force] <role> <host> - move exclusive role <role> to host <host>
(Only use --force if you know what you are doing!)
set_ip <ip> <host> - set role with ip <ip> to host <host>
其他一引起帮助信息
(5)测试切换
DB1上的信息
[root@jboss1 ~]# mmm_control show
db1(192.168.1.251) master/ONLINE. Roles: reader(192.168.1.182), writer(192.168.1.180)
db2(192.168.1.252) master/ONLINE. Roles: reader(192.168.1.181)
[root@jboss1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:9d:93:dd brd ff:ff:ff:ff:ff:ff
inet 192.168.1.251/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.182/32 scope global eth0
inet 192.168.1.180/32 scope global eth0
inet6 fe80::20c:29ff:fe9d:93dd/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
DB2上的信息
[root@jboss2 mysql-mmm]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:6e:42:93 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.252/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.181/32 scope global eth0
inet6 fe80::20c:29ff:fe6e:4293/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
现在我们来停掉DB1上的mysql,,它会把所有的VIP都切换的DB2上,,操作如下
DB1上的信息
[root@jboss1 ~]# service mysql stop
Shutting down MySQL.. [ OK ]
[root@jboss1 ~]# mmm_control show
db1(192.168.1.251) master/HARD_OFFLINE. Roles:
db2(192.168.1.252) master/ONLINE. Roles: reader(192.168.1.181), reader(192.168.1.182), writer(192.168.1.180)
[root@jboss1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:9d:93:dd brd ff:ff:ff:ff:ff:ff
inet 192.168.1.251/24 brd 192.168.1.255 scope global eth0
inet6 fe80::20c:29ff:fe9d:93dd/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
再看DB2上的信息
[root@jboss2 mysql-mmm]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:6e:42:93 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.252/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.181/32 scope global eth0
inet 192.168.1.182/32 scope global eth0
inet 192.168.1.180/32 scope global eth0
inet6 fe80::20c:29ff:fe6e:4293/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
到些本文就完成了,在mysql-mmmm的基础上,再加一个mysql-proxy就可以实现读写分离了!
PS:如要是CENTOS的用户,可以更省事,直接用YUM升级就OK
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
yum install mysql-mmm* -y
©著作权归作者所有:来自51CTO博客作者akin520的原创作品,如需转载,请注明出处,否则将追究法律责任
mysql数据库休闲技术
共同学习,写下你的评论
评论加载中...
作者其他优质文章