问题描述:现在有一套主主复制的mysql数据库,数据文件目录超过97%,严重不足,需要马上进行处理,我们知道mysql数据库数据文件目录不能动态的去改变,不像oracle那样可以直接更改数据文件的目录,下面展示一种比较好的扩大mysql数据文件目录的方法:
这套库是利用keepalived ,来提供一个vip,然后业务连接这个vip(这里的vip为192.***118),如下查看vip的位置,它现在在S244上,说i明目前的S244,
[root@S244 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 mq state UP qlen 1000
link/ether 00:26:2d:0d:50:42 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.244/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.118/24 brd 192.168.0.255 scope global secondary eth0:1
inet6 fe80::226:2dff:fe0d:5042/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:26:2d:0d:50:43 brd ff:ff:ff:ff:ff:ff
inet6 fe80::226:2dff:fe0d:5043/64 scope link
valid_lft forever preferred_lft forever
4: eth2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:26:2d:0d:50:41 brd ff:ff:ff:ff:ff:ff
inet 192.168.8.244/24 brd 192.168.8.255 scope global eth2
[root@S243 keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 mq state UP qlen 1000
link/ether 00:26:2d:0d:50:42 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.244/24 brd 192.168.0.255 scope global eth0
inet6 fe80::226:2dff:fe0d:5042/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:26:2d:0d:50:43 brd ff:ff:ff:ff:ff:ff
inet6 fe80::226:2dff:fe0d:5043/64 scope link
valid_lft forever preferred_lft forever
4: eth2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:26:2d:0d:50:41 brd ff:ff:ff:ff:ff:ff
inet 192.168.8.244/24 brd 192.168.8.255 scope global eth2
[root@S244 keepalived]#
那么好,下面展示具体操作流程:
我打算移动一个库的目录到另一个空间充足的路径下。
在操作数据库之前,应该先和系统工程沟通好,先停掉相关的服务。
一)首先操作从库;
1,关闭主从复制
mysql> stop slave;
2,关闭mysql服务,注意有的时候是:service mysqld stop
[root@S243 ~]# service mysql stop
3,我们知道一个库对应一个子目录,经过对比,我决定把mailer 库移动到另一个目录 /mysql2
1)[root@S243 ~]#cd /mysql/datadir
2)[root@S243 datadir]# cp -r mailer /mysql2/mailer ----把整个目录 mailer 复制到/mysql2,并且起名字也为mailer.
3)确定复制成功后,删除原来在/mysql的mailer目录
[root@S243 datadir]# rm -rf mailer
4)修改 /mysql2/下的mailer的属组,
[root@S243 mysql2]# chown -R mysql:mysql mailer
5)建立软连接,到/mysql/datadir目录
[root@S243 mysql2]# ln -s /mysql2/mailer /mysql/datadir/mailer
4,启动数据库.
[root@S243 ~]# service mysql start
5,开启主从复制,并检查相关进程的状态,均为yes为正常。
mysql> start slave;
mysql> show slave status/G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.244
Master_User: info_syncer
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.001358
Read_Master_Log_Pos: 643287297
Relay_Log_File: S243-relay-bin.001134
Relay_Log_Pos: 350
Relay_Master_Log_File: mysql-bin.001358
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
。。。。。。
到此从库的操作结束。。。。
针对主库的操作和从库的操作一样,需要注意的我们这台主主复制的mysql.通过keepalived设置出了一个vip 118 ,写的业务是通过连接vip登录数据库的,我们可以保证写的业务不受影响,因为vip所在的那台服务器就是主mysql,当这台mysql死掉后,vip就自动转移到另一台服务器上了,依旧保持正常的写入的业务,
然后我关注的是vip转移的触发条件:我们都知道keepalived 监控着的服务死掉,会触发vip迁移,还有就是keepalived 的服务停掉也会触发vip漂移,
1).关闭keepalived 会触发vip飘到另一台服务器,
[root@S244 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 mq state UP qlen 1000
link/ether 00:26:2d:0d:50:42 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.244/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.118/24 brd 192.168.0.255 scope global secondary eth0:1
inet6 fe80::226:2dff:fe0d:5042/64 scope link
valid_lft forever preferred_lft forever
[root@S244 ~]# service keepalived stop
再次查看果然没有了 118那个vip
[root@S244 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 mq state UP qlen 1000
link/ether 00:26:2d:0d:50:42 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.244/24 brd 192.168.0.255 scope global eth0
inet6 fe80::226:2dff:fe0d:5042/64 scope link
valid_lft forever preferred_lft forever
可是当再次[root@S244 ~]# service keepalived start之后,vip118又再次飘回到这里了。原来keepalived 配置文件里有个参数 priority 是优先级,如下发现是s244这台机器的优先级更高 priority 100高于 priority 50,并且还有个参数nopreempt代表优先级别高的不要抢占vip, 默认是preempt抢占vip。
[root@S243 keepalived]# cat keepalived.conf
global_defs {
notification_email {
}
}
vrrp_instance VI_1 {
# state MASTER
state BACKUP
interface eth0
virtual_router_id 51
# priority 100
priority 50
mcast_src_ip 192.168.0.243
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# 202.85.218.197 dev eth1 label eth1:1
192.168.0.118/24 broadcast 192.168.0.255 dev eth0 label eth0:1
}
virtual_server 192.168.0.118 3306 {
delay_loop 2 #每个2秒检查一次real_server状态
lb_algo wrr #LVS算法
lb_kind DR #LVS模式
persistence_timeout 60 #会话保持时间
protocol TCP
real_server 192.168.0.243 3306 {
weight 3
notify_down /var/lib/mysql/mysql_kpa.sh #检测到服务down后执行的脚本
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
[root@S244 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
}
}
vrrp_instance VI_1 {
# state BACKUP
state MASTER
interface eth0
virtual_router_id 51
# priority 50
priority 100
mcast_src_ip 192.168.0.244
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# 202.85.218.197 dev eth1 label eth1:1
192.168.0.118/24 broadcast 192.168.0.255 dev eth0 label eth0:1
}
virtual_server 192.168.0.118 3306 {
delay_loop 2 #每个2秒检查一次real_server状态
lb_algo wrr #LVS算法
lb_kind DR #LVS模式
persistence_timeout 60 #会话保持时间
protocol TCP
real_server 192.168.0.244 3306 {
weight 3
notify_down /var/lib/mysql/mysql_kpa.sh #检测到服务down后执行的脚本
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
总结:mysql数据库的空间空间可扩展性还是比oracle要差点,所以针对mysql数据库前期的准备和预估工作很重要,你需要更准确的预估出未来一段时间的空间增幅,以便于预留出更加准确的空间,因为mysql空间扩展是需要停服务的,并且存在一定的风险,