美文网首页Linux运维开发
MySQL主从复制(一主一从)

MySQL主从复制(一主一从)

作者: InnocenceYWQ | 来源:发表于2018-07-19 22:45 被阅读20次

一. 为什么要使用MySQL主从复制

  • 当单台MYSQL服务器无法满足当前网站流量时的优化方案。需要搭建mysql集群技术。
  • 但在实际的生产环境中,由单台Mysql作为独立的数据库是完全不能满足实际需求的,无论是在安全性,高可用性以及高并发等各个方面。
  • 从安全性角度来说,只有一台数据库是万万不够的,如果这台数据库出现故障,将造成不可挽回的损失。这个时候我们需要两台甚至多台数据库来为我们提供数据库服务。
  • 从高可用的角度来说,主从复试模式具有高性能,可以由多台slave,实现读写分离
  • 从高并发的角度来说,多台主从服务器可以实现负载均衡,把高并发的请求分发到不同的服务器,实现读写分离,保证了数据的安全。

  • 复试可以实现将数据从一台数据库服务器(master)复制到一或多台数据库服务器(slave)
  • 默认情况下属于异步复制,无需维持长连接
  • 通过配置,可以复制所有的库或者几个库,甚至库中的一些表
    是MySQL内建的,本身自带的

二. 安装MySQL服务器

CentOS 7.0中,已经使用MariaDB替代了MySQL数据库,功能和MySQL差不多,这里我们使用MariaDB服务器进行MySQL主从复制。

安装所需软件包:
[root@centos7 ~]#yum -y install httpd mariadb-server mariadb php php-mysql

启动相关服务:
[root@centos7 ~]# systemctl start  httpd
[root@centos7 ~]# systemctl start  mariadb 

测试数据库连接: 
[root@centos7 ~]# mysql -u root –p123456 –h 10.10.10.68

为root用户设置密码
[root@centos7 ~]# mysqladmin -u root password "123456"

启动服务后查看默认的数据库:

[root@xuegod64 ~]# systemctl start  mariadb 
[root@xuegod64 ~]# mysql -u root 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)


通过密码登录:

[root@xuegod64 ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

三.主从复制

环境准备.png
[root@xuegod64 html]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database HA;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use HA;
Database changed
MariaDB [HA]> create table T1(
    -> id int ,
    -> name varchar(20));
Query OK, 0 rows affected (0.03 sec)

MariaDB [HA]> show tables;
+--------------+
| Tables_in_HA |
+--------------+
| T1           |
+--------------+
1 row in set (0.00 sec)

配置my.cnf:

log-bin=mysql-bin-master  #启用二进制日志  
server-id=1   
binlog-do-db=HA #可以被从服务器复制的库, 二进制需要同步的数据库名
binlog-ignore-db=mysql  

重启服务
查看二进制日志:

ls /usr/local/mysql/data/

保持同步

复制前要保证同步的数据库一致
mysqldump  -uroot -p123456 HA >HA.sql  #可以导出数据库

导出数据

将导出的数据库传给从服务器
方法:scp HA.sql  10.10.10.64:/root

登录slave服务器,执行以下命令:

mysql>stop slave;    #停止slave
mysql> change master to master_host='10.10.10.63',master_user='slave',master_password='123456';
mysql> start slave;    #启动slave

查看Slave_IO_Running ,Slave_SQL_Running为yes即可。

相关文章

  • 23-MYSQL数据库(三)

    MySQL复制 一主一从 一主多从 主从复制原理 MySQL垂直分区 MySQL水平分片(Sharding) 对应...

  • mysql主从代理

    mysql主从复制+读写分离 环境: mysql主:193.168.1.1 mysql从:193.168.1.2 ...

  • MySQL主从复制 - 入门

    目录 主MySQL安装MySQL配置MySQL账号和状态 从MySQL安装MySQL配置MySQL主从复制 测试主...

  • Mysql主从复制

    MySQL 主从复制概念 MySQL 主从复制是指数据可以从一个MySQL数据库服务器主节点复制到一个或多个从节点...

  • MySQL配置主从同步

    一、数据库主从复制的基本思想 mysql主从复制是指将mysql主服务器中的数据复制到一台或者多台mysql从服务...

  • MySQL学习日记(十)-主从同步

    主从形式 mysql主从复制 灵活 一主一从 主主复制 一主多从---扩展系统读取的性能,因为读是在从库读取的; ...

  • Mysql主从复制(一主一从)

    Mysql主从复制总结: 前文链接:CentOS7 下安装 Mysql8

  • MySQL主从复制(一主一从)

    一. 为什么要使用MySQL主从复制 当单台MYSQL服务器无法满足当前网站流量时的优化方案。需要搭建mysql集...

  • mysql主从复制-一主一从

    准备: 两台window7 虚拟机 一台ip为 10.3.0.194 master 一台为10.3.0.201...

  • Mycat进阶实战

    Mysql主从复制 1、主从复制的含义在 MySQL 多服务器的架构中,至少要有一个主节点(master),跟主节...

网友评论

    本文标题:MySQL主从复制(一主一从)

    本文链接:https://www.haomeiwen.com/subject/zymtmftx.html