导读 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; 

全做完了。

原文来自:https://www.howtoing.com/how-to-set-up-master-slave-replication-in-mysql

本文地址:https://www.linuxprobe.com/master-slave-mysql.html编辑:吴向平,审核员:逄增宝

本文原创地址:https://www.linuxprobe.com/master-slave-mysql.html编辑:xiangping wu,审核员:暂无