淘宝数以万计的用户重要数据是如何实现不丢失的?---MySQL主从复制+MySQL主主复制

1.MYSQL主从复制

1.1用途:

Mysql主从复制的目的是实现数据冗余备份,从而保障服务器压力不断增加时一旦出现主库宕机可以快速将web应用数据库配置切换到slave数据库,以确保web应用较高的可用率。

1.2工作原理:

  1. Slave上执行slave start,Slave IO线程会通过在Master创建的授权用户连接上至Master,并请求master从指定的文件和位置之后发送bin-log日志内容;
  2. Master接收到来自slave IO线程的请求后,master IO线程根据slave发送的指定bin-log日志position点之后的内容,然后返回给slave的IO线程。
  3. 返回的信息中除了bin-log日志内容外,还有master最新的binlog文件名以及在binlog中的下一个指定更新position点;
  4. Slave IO线程接收到信息后,将接收到的日志内容依次添加到Slave端的relay-log文件的最末端,并将读取到的Master端的 bin-log的文件名和position点记录到master.info文件中,以便在下一次读取的时候能告知master从响应的bin-log文件名及最后一个position点开始发起请求;
  5. Slave Sql线程检测到relay-log中内容有更新,会立刻解析relay-log的内容成在Master真实执行时候的那些可执行的SQL语句,将解析的SQL语句并在Slave里执行,执行成功后,Master库与Slave库保持数据一致。

1.3实施注意事项:

1.需要准备两台以上服务器,可以配置一主多从,多主多从;

2.必须确保slave库的position点与master库的同步开始点位一致,文件名一致

1.4主从结构:

1.5实施过程:

1.准备两台服务器(centos7.4),都安装好MySQL数据库

主库:

IP:192.168.1.112 版本:MySQL8.0.31

从库:

IP: 192.168.1.123 版本:MySQL8.0.31

wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm   ###下载镜像包
rpm -ivh mysql80-community-release-el7-1.noarch.rpm    ###获取安装包
sed -i 's#gpgcheck=1#gpgcheck=0#g' /etc/yum.repos.d/mysql-community.repo   ###避免版本问题密钥报错
yum install mysql-community-server -y				###yum安装数据库


systemctl enable mysqld				###设置开机自启
systemctl start mysqld					###启动MySQL
grep 'password' /var/log/mysqld.log 				###获取初始密码
2022-12-05T09:59:08.779856Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ++V5oq:aurX>
[root@zzp112 ~]# mysql -uroot -p'++V5oq:aurX>'																	####登录MySQL
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.31

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> alter user 'root'@'localhost' identified by 'P@ssw0rd';									###设置新密码
Query OK, 0 rows affected (0.01 sec)

mysql> set global validate_password.policy=0;								###设置密码强度
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password.length=1;							###设置密码长度
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';				###设置密码
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;						###刷新

2.配置主库 /etc/my.cnf配置文件[mysqld]部分

[root@zzp112 ~]# vim /etc/my.cnf

配置内容:添加以下

[mysqld]
server-id = 1
log-bin = mysql-bin

3.Master数据库服务器命令行中 创建tongbu用户及密码并设置权限,执行如下命令,查看bin-log文件及position点

[root@zzp112 ~]# mysql -uroot -p123456				###登录
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> create user 'tongbu'@'%' identified with mysql_native_password by 'P@ssw0rd';    ####创建同步用户
Query OK, 0 rows affected (0.04 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'tongbu'@'%';						####设置tongbu用户权限
Query OK, 0 rows affected (0.01 sec)

mysql> show  master  status;								####查看起始点位
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |     1305 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> 

4.slave数据库,配置/etc/my.cnf,则需修改server-id,MASTER与Slave端server-id不能一样,Slave端也无需开启bin-log功能

添加内容:

[mysqld]
server-id = 2

5.Slave指定Master IP、用户名、密码、bin-log文件名( mysql-bin.000002)及position(1305)

[root@zzp123 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> change master to 
  master_host='192.168.10.110',master_user='tongbu',master_password='P@ssw0rd',master_log_file='mysql--bin.000001',master_log_pos=664;
Query OK, 0 rows affected, 8 warnings (0.04 sec)             ###指定Master IP、用户名、密码、bin-log文件名( mysql-bin.000002)及position(1305)

6.在slave启动slave start,并执行show slave status\G查看Mysql主从状态:

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)

> show slave status\G							###查看主从状态
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.1.112
                  Master_User: tongbu
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 1305
               Relay_Log_File: zzp123-relay-bin.000002
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes													####I/O线程已启动
            Slave_SQL_Running: Yes															####SQL线程已启动
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 

7.在主库创建数据库查看从库是否同步:

2.MySQL主主复制

1.在原来主从基础上修改slave库 /etc/my.cnf文件

修改内容:

[mysqld]
server-id = 2
log-bin = mysql-bin1

2.Master123数据库服务器命令行中 创建tongbu用户及密码并设置权限,执行如下命令,查看bin-log文件及position点

[root@zzp123 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> create user 'tongbu2'@'%' identified with mysql_native_password by 'P@ssw0rd';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'tongbu'@'%';
ERROR 1410 (42000): You are not allowed to create a user with GRANT
mysql> GRANT REPLICATION SLAVE ON *.* TO 'tongbu2'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 |     1512 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

3.Slave112指定Master IP、用户名、密码、bin-log文件名( binlog.000001 )及position(1512)

mysql> change master to 
    -> master_host='192.168.1.123',master_user='tongbu2',master_password='P@ssw0rd',master_log_file='binloog.000001
Query OK, 0 rows affected, 8 warnings (0.03 sec)

mysql> slave start;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'slave start' at line 1
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.1.123
                  Master_User: tongbu2
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000001
          Read_Master_Log_Pos: 1512
               Relay_Log_File: zzp112-relay-bin.000002
                Relay_Log_Pos: 323
        Relay_Master_Log_File: binlog.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 

4.测试:123库创建数据库,112库可以查询到

112库创建表插入数据,123库也可以查询到

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: npm -g --save是npm命令的一种组合使用方式。 首先,npm是Node Package Manager的缩写,是Node.js的包管理器,用于安装、卸载和管理Node.js模块。通过npm,我们可以方便地查找、下载和安装数以万计的第三方模块,使得开发和发布Node.js应用变得更加高效和便捷。 在npm命令中,-g是一个选项,代表全局安装。通常情况下,使用npm安装模块时,默认是在当前项目的根目录下进行安装,只对当前项目有效。而使用-g选项,可以将模块安装到全局环境中,使得安装的模块可以在任何项目中被使用。全局安装后的模块会被安装在系统特定的目录下,可以通过指定环境变量来访问它们。 而--save选项,则是保存模块信息到package.json文件中。package.json是一个用于描述当前项目的配置文件,其中包含了项目的名称、版本、作者、依赖等信息。使用--save选项,可以将安装的模块信息自动添加到package.json文件中的dependencies字段中,方便后续管理和维护。当我们需要重新安装整个项目时,只需要执行npm install命令,npm会根据package.json文件中dependencies字段的信息自动安装所需的模块,从而实现项目的快速搭建和迁移。 综上所述,npm -g --save是一个常用的npm命令组合,表示在全局环境下安装模块,并将模块信息保存到package.json文件中,以便于项目的管理和维护。这种方式对于需要在多个项目中共享和复用特定模块的情况非常有用。 ### 回答2: npm -g --save是npm命令中的一种参数组合。它可以一起使用来全局安装并保存依赖。 首先,npm是一个用于管理JavaScript软件包的包管理器。它是Node.js的默认包管理器,用于安装、升级和删除JavaScript依赖项。 而-g参数是指定全局安装软件包,意味着安装的软件包将在系统的全局范围内可用,而不仅仅是在特定的项目中。这对于安装一些通用的工具,比如构建工具或命令行工具非常有用。 另外,--save参数是将安装的软件包的依赖信息保存到项目的package.json文件中。package.json是一个用于描述项目和其依赖项的配置文件,其中包含项目的名称、版本、作者等信息。通过保存依赖信息,有助于确保在将项目从一个环境移动到另一个环境时,依赖项能够被正确地安装和使用。 综上所述,npm -g --save命令用于全局安装软件包,并将依赖信息保存到项目的package.json文件中,以便在不同环境中正确安装和使用依赖项。 ### 回答3: npm -g --save 是一条命令,用于在全局环境中安装一个包并将其添加到项目的依赖中。 npm 是 Node.js 包管理器,用于安装、管理和卸载 Node.js 模块。它允许开发者轻松地分享和重用代码,提高开发效率。 -g 是 npm 命令的一个选项,表示全局安装。全局安装是指将包安装在全局环境中,而不是项目的本地 node_modules 目录中。这意味着安装的包可以在任何项目中使用,而不需要重复安装。 --save 是 npm 命令的另一个选项,表示将包添加到项目的依赖中。这样,在项目的 package.json 文件中,会自动添加该包的版本信息到 dependencies 或 devDependencies 字段中。当其他开发者使用 npm install 命令安装项目时,可以自动安装项目所需的所有依赖。 综上所述,npm -g --save 命令的作用是在全局环境中安装一个包,并将其添加到项目的依赖中,使得其他开发者可以方便地使用该包,并确保项目在不同环境中的一致性和可再现性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醉里看星辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值