您的位置:首页 > 运维架构 > Linux

Linux下MySQL数据库主从同步配置

2018-03-12 15:25 585 查看
操作系统:CentOSMySQL数据库版本:mysql-5.5MySQL主服务器:192.168.21.128MySQL从服务器:192.168.21.129准备篇:说明:在两台MySQL服务器192.168.21.128和192.168.21.129上分别进行如下操作备注:作为主从服务器的MySQL版本建议使用同一版本!或者必须保证主服务器的MySQL版本要高于从服务器的MySQL版本!一、配置好IP、DNS 、网关,确保使用远程连接工具能够连接服务器二、配置防火墙,开启3306端口vi /etc/sysconfig/iptables  #编辑-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT  #允许3306端口特别提示:如果这两条规则添加到防火墙配置的最后一行,导致防火墙启动失败,正确的应该是添加到默认的22端口这条规则的下面,添加好之后防火墙规则如下所示:########################################################## Firewall configuration written by system-config-securitylevel# Manual customization of this file is not recommended.*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]:RH-Firewall-1-INPUT - [0:0]-A INPUT -j RH-Firewall-1-INPUT-A FORWARD -j RH-Firewall-1-INPUT-A RH-Firewall-1-INPUT -i lo -j ACCEPT-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT-A RH-Firewall-1-INPUT -p 50 -j ACCEPT-A RH-Firewall-1-INPUT -p 51 -j ACCEPT-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibitedCOMMIT#########################################################/etc/init.d/iptables restart #最后重启防火墙使配置生效三、关闭SELINUXvi /etc/selinux/config#SELINUX=enforcing #注释掉#SELINUXTYPE=targeted #注释掉SELINUX=disabled #增加:wq  #保存退出setenforce 0 #立即生效

配置篇

一、配置MySQL主服务器(192.168.21.128)mysql -u root -p  #进入MySQL控制台create database osyunweidb;   #建立数据库osyunweidbinsert into mysql.user(Host,User,Password) values('localhost','osyunweiuser',password('123456'));   #创建用户osyunweiuser,密码123456grant all on osyunweidb.* to 'osyunweiuser'@'192.168.21.130' identified by '123456' with grant option;  #授权用户osyunweiuser从192.168.21.130完全访问数据库,注意:这里的192.168.21.130是要连接数据库Web服务器IPinsert into mysql.user(Host,User,Password) values('localhost','osyunweidbbak',password('123456'));   #建立MySQL主从数据库同步用户osyunweidbbak密码123456flush privileges;   #刷新系统授权表grant replication slave  on *.* to 'osyunweidbbak'@'192.168.21.129' identified by '123456' with grant option;  #授权用户osyunweidbbak只能从192.168.21.129这个IP访问主服务器192.168.21.128上面的数据库,并且只具有数据库备份的权限二、把MySQL主服务器192.168.21.128中的数据库osyunweidb导入到MySQL从服务器192.168.21.129中1、导出数据库osyunweidbmysqldump -u root -p --default-character-set=utf8 --opt -Q -R --skip-lock-tables osyunweidb > /home/osyunweidbbak.sql    #在MySQL主服务器进行操作,导出数据库osyunweidb到/home/osyunweidbbak.sql备注:在导出之前可以先进入MySQL控制台执行下面命令flush tables with read lock;    #数据库只读锁定命令,防止导出数据库的时候有数据写入unlock tables;   #解除锁定scp /home/osyunweidbbak.sql root@192.168.21.129:/home   #把home目录下的osyunweidbbak.sql 数据库文件上传到MySQL从服务器的home目录下面2、导入数据库到MySQL从服务器mysql  -u root -p  #进入从服务器MySQL控制台create database osyunweidb;   #创建数据库use osyunweidb    #进入数据库source  /home/osyunweidbbak.sql  #导入备份文件到数据库mysql -u osyunweidbbak -h 192.168.21.128 -p  #测试在从服务器上登录到主服务器三、配置MySQL主服务器(192.168.21.128)的my.cnf文件vi /etc/my.cnf   #编辑配置文件,在[mysqld]部分添加下面内容server-id=1   #设置服务器id,为1表示主服务器,注意:如果原来的配置文件中已经有这一行,就不用再添加了。log-bin=mysql-bin  #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。binlog-do-db=osyunweidb  #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行binlog-ignore-db=mysql   #不同步mysql系统数据库:wq!   #保存退出service mysqld  restart  #重启MySQLmysql -u root -p   #进入mysql控制台show variables like 'server_id';  #查看server-id的值是否为1mysql> show variables like 'server_id';+---------------+-------+| Variable_name | Value |+---------------+-------+| server_id     | 1     |+---------------+-------+1 row in set (0.00 sec)show master status;  #查看主服务器,出现以下类似信息mysql> show master status;+------------------+----------+--------------+------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000011 |      107 | osyunweidb   | mysql            |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)注意:这里记住File的值:mysql-bin.000011和Position的值:107,后面会用到。四、配置MySQL从服务器(192.168.21.129)的my.cnf文件vi /etc/my.cnf   #编辑配置文件,在[mysqld]部分添加下面内容server-id=2   #设置服务器id,修改其值为2,表示为从数据库log-bin=mysql-bin  #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。replicate-do-db=osyunweidb   #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行replicate-ignore-db=mysql   #不同步mysql系统数据库read_only  #设置数据库只读:wq!    #保存退出service mysqld restart   #重启MySQLmysql  -u root -p  #进入MySQL控制台show variables like 'server_id';  #查看server-id的值,必须为上面设置的2,否则请返回修改配置文件mysql> show variables like 'server_id';+---------------+-------+| Variable_name | Value |+---------------+-------+| server_id     | 2     |+---------------+-------+1 row in set (0.01 sec)slave stop;   #停止slave同步进程change master to master_host='192.168.21.128',master_user='osyunweidbbak',master_password='123456',master_log_file='mysql-bin.000011' ,master_log_pos=107;    #执行同步语句slave start;    #开启slave同步进程SHOW SLAVE STATUS\G   #查看slave同步信息,出现以下内容mysql> SHOW SLAVE STATUS\G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.21.128Master_User: osyunweidbbakMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000011Read_Master_Log_Pos: 107Relay_Log_File: mysqlslave-relay-bin.000004Relay_Log_Pos: 253Relay_Master_Log_File: mysql-bin.000011Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: osyunweidbReplicate_Ignore_DB: mysqlReplicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 107Relay_Log_Space: 560Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 11 row in set (0.00 sec)mysql>注意查看:Slave_IO_Running: YesSlave_SQL_Running: Yes以上这两个参数的值为Yes,即说明配置成功!测试篇测试MySQL主从服务器是否正常运行1、进入MySQL主服务器(192.168.21.128)mysql -u root -p  #进入MySQL控制台use osyunweidb   #进入数据库CREATE TABLE test ( id int not null primary key,name char(20) );   #创建test表2、进入MySQL从服务器mysql -u root -p  #进入MySQL控制台use osyunweidb   #进入数据库show  tables;  #查看osyunweidb表结构,会看到有一个新建的表test,表示数据库同步成功mysql> show  tables;+----------------------+| Tables_in_osyunweidb |+----------------------+| test                 |+----------------------+1 row in set (0.00 sec)至此,Linux下MySQL数据库主从同步配置完成!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: