设为首页 收藏本站
查看: 805|回复: 0

[经验分享] mysql主主互备模式配置!!!

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-3-14 08:39:51 | 显示全部楼层 |阅读模式
项目背景:
实现mysql的主主实时复制,保证我们的数据安全!


实验环境:
vmware workstation 11
mysql主服务器:ip:192.168.0.53 主机名:DB1
mysql从服务器:ip:192.168.0.26 主机名:DB2
主服务器和从服务器安装的的软件(一样)
mysql-5.1.73-5.el6_6.x86_64
mysql-devel-5.1.73-5.el6_6.x86_64
mysql-libs-5.1.73-5.el6_6.x86_64
mysql-server-5.1.73-5.el6_6.x86_64
mysql主服务器:iptables关掉  setenforce 0
mysql从服务器:iptables关掉  setenforce 0
SecureCRT (ssh远程连接软件)


实验流程:
一、软件下载(主服务器和从服务器分别执行下面的命名安装)
yum install -y mysql mysql-server mysql-devel

二、主机名修改(主服务器和从服务器都修改)
vim /etc/sysconfig/network

三、关掉防火墙(主服务器和从服务器都关闭)
service iptables stop ;chkconfig iptables off

四、修改/etc/hosts文件(主服务器和从服务器都修改)
[iyunv@DB1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.0.53  DB1
192.168.0.26  DB2

[iyunv@DB2 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.0.53  DB1
192.168.0.26  DB2

五、修改mysql配置文件(/etc/my.cnf)
主服务器配置文件:
[iyunv@DB1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

server-id=1
log-bin=mysql-bin
relay-log =mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

从服务器配置:
[iyunv@DB2 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0


server-id=2
log-bin=mysql-bin
relay-log =mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


六、手动同步数据库
打包mysql来实现,具体你可以百度一下。


七、创建复制用户并授权
登录以后执行:
主服务器:
grant replication slave on *.* to  'repl_user'@'192.168.0.26' identified by 'repl_passwd';

从服务器:
grant replication slave on *.* to  'repl_user'@'192.168.0.53' identified by 'repl_passwd';

八、执行 show master status;
主服务器执行
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |     1001 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

mysql>
从服务器执行
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      417 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

mysql>
记住它的信息

九、在DB2 mysql数据库把DB1设置为主服务器,然后执行:start slave
mysql> change master to \
    -> master_host ='192.168.0.53',
    -> master_user='repl_user',
    -> master_password='repl_passwd',
    -> master_log_file='mysql-bin.0000004'
    -> msater_log_pos='1001'


十、在DB1 mysql数据库把DB2设置为主服务器,然后执行:start slave
mysql> change master to \
    -> master_host ='192.168.0.26',
    -> master_user='repl_user',
    -> master_password='repl_passwd',
    -> master_log_file='mysql-bin.0000004'
    -> msater_log_pos='417'

十一、安装keepalived软件

十二、修改keepalived配置文件
DB1:
[iyunv@DB1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 80
    priority 100
    advert_int 2
    nopreempt

    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.0.10/24
    }
}

DB2:
[iyunv@DB2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 80
    priority 90
    advert_int 2


    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.0.10/24
    }
}

十三、启动keepalived(DB1和DB2都启动)

十四、远程mysql登录
[iyunv@localhost ~]# mysql -u root -p -h 192.168.0.10
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.73-log Source distribution

Copyright (c) 2000, 2013, 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> show databes;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databes' at line 1
mysql> show database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fuchao1            |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> show  variables like "hostname%";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| hostname      | DB1   |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show  variables like "%server_id%";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+
1 row in set (0.00 sec)


可以看到我们用vip登录的,现在主是DB1

十五、数据复制测试
我在DB1上创建一个数据库,看看DB2上会不会复制过去

QQ截图20160314083935.png
QQ截图20160314083941.png

当然你进行删除的话 也会同步!!!! 你可以自己试一下!


谢谢大家,希望对大家有帮助。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-190443-1-1.html 上篇帖子: MySQL mha搭建出错,求教故障原因及结局方法。报错信息请看正文 下篇帖子: my.ini文件注释 mysql
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表