码迷,mamicode.com
首页 > 数据库 > 详细

mysql主从机制的部署与应用

时间:2019-05-25 09:42:10      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:增加   机制   ORC   star   database   restart   时间同步服务器   status   server-id   

部署mysql主从复制

Mysql master ip: 192.168.30.25   一主两从

Mysql slave ip: 192.168.30.24

Mysql slave ip:192.168.30.23

 

关闭防火墙

[root@master ~]# setenforce 0

[root@master ~]# iptables -F

[root@master ~]# systemctl stop firewalld

 

mysql master 上的配置NTP时间同步服务器

[root@master ~]# yum -y install ntp

[root@master ~]# vim /etc/ntp.conf

server 127.127.1.0

fudge 127.127.1.0 startum 8

 

启动NTP服务

[root@master ~]# systemctl enable ntpd

[root@master ~]# systemctl start ntpd

 

2slave节点上配置与master进行时间同步

[root@slave1 ~]# yum -y install ntpdate

[root@slave1 ~]# /usr/sbin/ntpdate 192.168.30.25

 

所有机器安装mariadb

[root@master ~]# yum -y install mariadb mariadb-server mariadb-devel

配置mysql master 服务器

1,etc/my.cnf中修改或者增加如下内容

[mysqld]

Server-id=1

Log-bin=mysql-binlog

Log-slave-ipdates=ture

重启mysql服务器

[root@master ~]# systemctl start mariadb

 

2,创建replication用户

Replication slave 用于复制从属服务器,(从主服务器中读取二进制日志事件权限

[root@master ~]# mysql -u root

MariaDB [(none)]> grant replication slave on *.* to ‘myslave‘@‘192.168.30.%‘identified by ‘123456‘;

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> show master status;

+---------------------+----------+--------------+------------------+

| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+---------------------+----------+--------------+------------------+

| mysql-binlog.000003 |      475 |              |                  |

+---------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

MariaDB [(none)]> exit

 

3,获得Master DB 的相关信息

 

Slave 连接时使用,记录下fileposition的值

 

4,备份matser原有数据此处省略

5,从库连接主库进行测试,如果连接成功说明主库配置成功

[root@slave1 ~]# mysql -u myslave -p123456 -h 192.168.30.25

[root@slave2 ~]# mysql -u myslave -p123456 -h 192.168.30.25

在从服务器都修改Mysql 配置文件

Vim /etc/my.cnf

Server-id = 2

Relay-log=relay-log-bin

Rela:y-log-index=slave-relay-bin.index

配置多个从服务器时依次设置server-id

修改完重启数据库

[root@slave1 ~]# systemctl restart mariadb

 

6,slave 服务器授权,启动从库,进行主从库同步

[root@slave1 ~]# mysql -u root

MariaDB [(none)]> stop slave;

MariaDB [(none)]> change master to

    -> master_host=‘192.168.30.25‘,

    -> master_user=‘myslave‘,

    -> master_password=‘123456‘,

    -> master_log_file=‘mysql-binlog.000003‘,

    -> master_log_pos=475;

Query OK, 0 rows affected (0.01 sec)

 

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> show slave status\G

master查看授权成功

[root@master ~]# netstat -anpt |grep mysql

tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      4162mysqld         

tcp        0      0 192.168.30.25:3306      192.168.30.23:58866     ESTABLISHED 4162mysqld         

tcp        0      0 192.168.30.25:3306      192.168.30.24:59174     ESTABLISHED 4162mysqld

 

进行测试是否成功

主库当前存在的库

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

4 rows in set (0.01 sec)

 

从库当前的库

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

 

主库添加创建库和表

MariaDB [(none)]> create database sampdb;

Query OK, 1 row affected (0.00 sec)

 

MariaDB [(none)]> use sampdb;

Database changed

MariaDB [sampdb]> create table new(name char(20),phone char(20));

Query OK, 0 rows affected (0.08 sec)

 

MariaDB [sampdb]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sampdb             |

| test               |

+--------------------+

5 rows in set (0.00 sec)

 

从库验证

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sampdb             |

| test               |

+--------------------+

 

mysql主从机制的部署与应用

标签:增加   机制   ORC   star   database   restart   时间同步服务器   status   server-id   

原文地址:https://www.cnblogs.com/zc1741845455/p/10921251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!