频道栏目
首页 > 资讯 > MySQL > 正文

Mysql配置数据库主从复制

17-08-03        来源:[db:作者]  
收藏   我要投稿

mysql实现主从复制的过程
使用的环境为rhel7
主服务器ip 为172.25.254.41
从服务器ip 为172.25.254.42
首先配置好yum源,然后安装好mariadb-server,设置好root密码等等
进入/etc下配置好my.cnf文件
在master端配置如下

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
log-bin=mysql-bin   #开启二进制日志(必须)
server-id=41        #服务器ID号,必须是唯一

在slave端配置为

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
log-bin=mysql-bin   #开启二进制日志(不是必须,但是为了可以主从切换所以还是写上去了)
server-id=42        #服务器ID号,必须是唯一

然后在两台主机上,重启服务

systemctl restart mariadb

在master上的操作
进入mysql

grant replication slave on *.* to 'slave'@'172.25.254.42' identified by 'slave';

对slave登陆时创建一个登陆用户并授权,最好不要用root用户。
show master status;
然后查询master的状态

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |     1235 |              |                  |
+------------------+----------+--------------+------------------+

注意:在这一步之后不要对数据库进行操作,不然File和Position都会出现变化,导致连接不上

在slave进行操作
进入mysql

 change master to master_host='172.25.254.41',master_user='slave',master_password='slave',master_log_file='mysql-bin.000002',master_log_pos=1235; 

与master建立连接,并且将登陆用户信息加入,最后两项就是写在master中显示的File和Position
start slave;
show slave status \G; #查看从服务器的状态

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event  #从服务器的I/O状态
                  Master_Host: 172.25.254.41            #主服务器的ip
                  Master_User: slave                #登陆的用户
                  Master_Port: 3306             #开启的端口
                Connect_Retry: 60               #重试等待时间
              Master_Log_File: mysql-bin.000002         #日志文件
          Read_Master_Log_Pos: 1235             #位置
               Relay_Log_File: mariadb-relay-bin.000002  
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes              #必须为yes
            Slave_SQL_Running: Yes              #必须为yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1235
              Relay_Log_Space: 825
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 41       #主服务的server id

测试
在master上

create database xiaobai;
use xiaobai;
create table status( name varchar(5),gender char(2) );

在slave上

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| xiaobai            |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use xiaobai;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [xiaobai]> show tables;
+-------------------+
| Tables_in_xiaobai |
+-------------------+
| status            |
+-------------------+
1 row in set (0.00 sec)

最后结果为
这里写图片描述

这里写图片描述

相关TAG标签
上一篇:linux中系统延时及定时任务
下一篇:Hadoop2.x.0分布式HA安装及部署
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站