43

在MySQL中设置主从复制入门实例

 5 years ago
source link: https://www.linuxprobe.com/master-slave-mysql.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
导读 MySQL复制是一个允许您通过自动从主数据库复制到从数据库来轻松维护MySQL数据的多个副本的过程。 这可能有助于许多原因,包括为数据提供备份,一种在不使用主数据库的情况下分析数据的方法,或者只是作为向外扩展的一种手段。

mysql复制示例:一个master将向单个slave发送信息。为了使进程工作,您将需要两个IP地址:主服务器之一和从属设备之一。

本教程将使用以下IP地址:

12.34.56.789-主数据库

12.23.34.456-从数据库

本文假设您具有sudo权限的用户并且已安装MySQL。 如果你没有mysql,你可以用这个命令安装:

sudo apt-get install mysql-server mysql-client
第一步 - 配置主数据库

打开主服务器上的mysql配置文件。

sudo nano /etc/mysql/my.cnf

一旦进入该文件,我们需要进行一些更改。

第一步是找到如下所示的部分,将服务器绑定到本地主机:

bind-address            = 127.0.0.1
将标准IP地址替换为服务器的IP地址。

bind-address            = 12.34.56.789 
下一个配置更改是指位于[mysqld]部分中的server-id。 您可以为此点选择任何数字(可能更容易从1开始),但该数字必须是唯一的,并且不能与复制组中的任何其他服务器标识匹配。 我要去打电话这个1。

确保此行已取消注释。

server-id               = 1
移动到log_bin行。 这是保存复制的真实细节的地方。 从属程序将复制在日志中注册的所有更改。 对于这一步,我们只需要取消注释引用log_bin的行:

log_bin                 = /var/log/mysql/mysql-bin.log
最后,我们需要指定将在从服务器上复制的数据库。 您可以通过为所有您需要的数据库重复此行,包括多个数据库。

binlog_do_db            = newdatabase
完成所有更改后,继续保存并退出配置文件。

刷新MySQL。
sudo service mysql restart

接下来的步骤将在MySQL shell中进行,本身。

打开MySQL shell。

mysql -u root -p

我们需要给从属权限。 您可以使用此行命名您的从属并设置其密码。 命令应采用以下格式:

GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

下一部分是有点bit。。 为了完成任务,你需要在除了你已经使用了几步倒行一打开一个新窗口或标签 。

在当前标签页切换到“newdatabase”。

USE newdatabase;

接下来,锁定数据库以防止任何新的更改:

FLUSH TABLES WITH READ LOCK;

然后输入:

SHOW MASTER STATUS;

你会看到一个表应该看起来像这样:

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      107 | newdatabase  |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

这是从数据库将开始复制的位置。 记录这些数字,他们将在以后有用。

如果在同一个窗口中进行任何新的更改,数据库将自动解锁。 因此,您应该打开新的选项卡或窗口,然后继续下一步。

继续数据库仍然锁定,在新窗口中使用mysqldump导出数据库(确保您在bash shell中而不是在MySQL中键入此命令)。

mysqldump -u root -p --opt newdatabase > newdatabase.sql

现在,返回到您的原始窗口,解锁数据库(使它们可写入)。 通过退出shell完成。

UNLOCK TABLES;
QUIT;

现在你已经完成了master数据库的配置。

第二步 - 配置从数据库

配置主数据库之后。 你可以把它放在一边,我们现在将开始配置从数据库。

登录到从服务器,打开MySQL shell并创建要从主服务器复制的新数据库(然后退出):

CREATE DATABASE newdatabase;
EXIT;
导入先前从主数据库导出的数据库。

mysql -u root -p newdatabase 

Now we need to configure the slave configuration in the same way as we did the master:

sudo nano /etc/mysql/my.cnf

We have to make sure that we have a few things set up in this configuration. The first is the server-id. This number, as mentioned before needs to be unique. Since it is set on the default (still 1), be sure to change it’s something different.

server-id               = 2

Following that, make sure that your have the following three criteria appropriately filled out:

relay-log               = /var/log/mysql/mysql-relay-bin.log
log_bin                 = /var/log/mysql/mysql-bin.log
binlog_do_db            = newdatabase

You will need to add in the relay-log line: it is not there by default.
Once you have made all of the necessary changes, save and exit out of the slave configuration file.

Restart MySQL once again:

sudo service mysql restart

The next step is to enable the replication from within the MySQL shell.

Open up the the MySQL shell once again and type in the following details, replacing the values to match your information:

CHANGE MASTER TO MASTER_HOST='12.34.56.789',MASTER_USER='slave_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=  107;

This command accomplishes several things at the same time:

It designates the current server as the slave of our master server. 
It provides the server the correct login credentials
Last of all, it lets the slave server know where to start replicating from; the master log file and log position come from the numbers we wrote down previously.


With that—you have configured a master and slave server. 

Activate the slave server:

START SLAVE;

You be able to see the details of the slave replication by typing in this command. The \G rearranges the text to make it more readable.

SHOW SLAVE STATUS\G 

If there is an issue in connecting, you can try starting slave with a command to skip over it:

SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; SLAVE START; 

All done. 

See More

MySQL replication has a lot different options, and this was just a brief overview.

If you have any further questions about the specific capabilities of MySQL, feel free to post your questions in our Q&A Forum and we’ll be happy to answer them. 

By Etel Sverdlov

现在我们需要以与我们做主机相同的方式配置从机配置:

sudo nano /etc/mysql/my.cnf

我们必须确保我们在这个配置中设置了一些东西。 第一个是服务器标识。 这个数字,如前所述需要是唯一的。 因为它被设置为默认(仍然是1),一定要改变它的东西不同。

server-id               = 2
之后,请确保您已正确填写以下三个条件:

relay-log               = /var/log/mysql/mysql-relay-bin.log
log_bin                 = /var/log/mysql/mysql-bin.log
binlog_do_db            = newdatabase

您将需要在中继日志行中添加:默认情况下不存在。 一旦完成所有必要的更改,保存并退出从配置文件。

再次重新启动MySQL:

sudo service mysql restart

下一步是在MySQL shell中启用复制。

再次打开MySQL shell,输入以下详细信息,替换值以匹配您的信息:

CHANGE MASTER TO MASTER_HOST='12.34.56.789',MASTER_USER='slave_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=  107;

此命令同时完成几个事情:

它将当前服务器指定为我们的主服务器的从属。

它为服务器提供正确的登录凭据

最后,它让从服务器知道从哪里开始复制; 主日志文件和日志位置来自我们之前写下的数字。

这样,您已经配置了主服务器和从服务器。

激活从服务器:

START SLAVE;

通过键入此命令,您可以看到从复制的详细信息。 \ G重新排列文本,使其更易读。

SHOW SLAVE STATUS\G

如果在连接中存在问题,可以尝试使用命令启动从器件以跳过它:

SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; SLAVE START; 

全做完了。


Recommend

  • 95
    • www.dboracle.com 5 years ago
    • Cache

    MySQL-5.7.18 搭建主从复制

    最近需要做一套MySQL主从复制的升级,因为之前没搭过MySQL主从复制,这次正好现在虚拟机上搭一下。先介绍一下我的环境。 两套Linux主机,都是CentOS 6.9操作系统。Master的IP是192.168.56.161,Slave的IP是192.168.56.2。Master...

  • 85
    • www.dboracle.com 5 years ago
    • Cache

    MySQL-5.7.18 主从复制升级至5.7.21

    前面写了一篇文章,搭建了MySQL5.7主从复制。今天要把这套主从环境从5.7.18升级到5.7.21。 一.上传安装包并解压 首先我们把安装介质传到salve服务器上,我们要先对slave服务器进行升级。 -rw-r--r--....

  • 9
    • database.51cto.com 3 years ago
    • Cache

    深入了解MySQL主从复制的原理

      本文转载自微信公众号「SH的全栈笔记」,作者SH。转载本文请联系SH的全栈笔记公众号。SH 0. 主从复制 首先主从复...

  • 6

    您现在的位置:首页 --> MySQL --> MySQL 5.6 测试之 Replication(主从复制) MySQL 5.6 测试之 Repl...

  • 5
    • www.wencst.com 3 years ago
    • Cache

    部署MySQL主从复制与读写分离

    一、实验坏境 1.一台CentOS 7作为客户端测试,对应的地址为:192.168.80.120 2.一台CentOS 7作为Amoeba前端代理服务器,对应的地址为:192.168.80.110 3.一台CentOS 7作为mysql主服务器,对应的地址为:192.168.80.100

  • 7

    在实际的生产环境中,如果对MySQL数据库的读和写都在一台数据库服务中操作,无论在安全性、高可用性,还是高并发性等各个方面都是完全不能满足实际需求的,一般来说都是通过主从复制(Master-Slave)的方式来同步数据,再通过读写分离来提升数据库的并发负载能力...

  • 3
    • blog.csdn.net 2 years ago
    • Cache

    Mysql主从复制和读写分离

    Mysql主从复制原理 MySQL 的主从复制和 MySQL 的读写分离两者有着紧密联系,首先要部署主从复制,只有主从复制完成了,才能在此基础上进行数据的读写分离。 Mysql支持的复制类型 基于语句的复制(STATEM...

  • 5

    [TOC] 今天的文章来晚了,主要是我一觉起来变黄码了,关键是我还不知道,早上 8.20 到了公司楼下,保安要看健康码,当我自信满满的打开粤省事却傻眼了,折腾一早上,绿码总算回来了,真是生活处处有惊喜。。。 书接上回,闲话不表。

  • 7
    • oceandlnu.github.io 2 years ago
    • Cache

    MySQL 主从复制(Master-Slave)

    MySQL 主从复制(Master-Slave) 发表于 ...

  • 4
    • maxqiu.com 1 year ago
    • Cache

    MySQL主从复制

    MySQL主从复制 2022/05/16  MySQL 如何提升并发能力一般应用对数据库而言都是 “读多写少” ,对数据库...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK