MySQL 运维篇

回顾基本语句:

数据定义语言(DDL)

这类语言用于定义和修改数据库的结构,包括创建、删除和修改数据库、 表、视图和索引等对象。

主要的语句关键字包括 CREATE 、 DROP 、 ALTER 、 RENAME 、 TRUNCATE 等。

create database 数据库 ;

create table 表 (列1 数据类型 [ 约束条件], 列2 数据类型 【约束条件】……) add CONTRAINT 增加一些主键或者外键的约束

drop table/ drop database;

truncate 表;

rename 旧表名 TO 新表名 ;

alter 表 add 列 / drop 列 /

create view (虚表) / drop view

create user / drop user

数据操作语言(DML)

用于添加、删除、更新和查询数据库中的记录。这些语句是关系型数据库的 核心,用于操作表中的数据。主要的语句关键字包括 INSERT 、 DELETE 、 UPDATE 、 SELECT 等。

select 表的数据查询 from 表 WHERE 过滤条件 group by 列 order by ;

insert into 表(列1……)values (一行数据【和列是对应输入的】), (第二行数据)……;

update 表 SET 列 = 值 where 过滤条件 (定位修改的数据行);

delete from 表 where 过滤条件;

数据控制语言(DCL)

用于定义和管理数据库、表、字段或用户的访问权限和安全级别。主要的语 句关键字包括 GRANT 、 REVOKE 、 COMMIT 、 ROLLBACK 等。

GRANT // 数据库用户的授权 对应权限的内容见 MySQL CRASH COURSE p257 表 28.1

revoke // 数据库用户权限的回收【撤销】

commit / rollback // 对于事务的提交和回滚 

数据查询语言(DQL)

这是专门用于从数据库中检索数据/ f

select cur();

select version();

联合查询等

join

UNION

一、安装

1. 源码编译安装

a.获取源码     boost(包含这个组件的源码包)

b. cmake ---> 生成makefile

c. make

d. make install

e. 进行一些基础的优化

2. 二进制 (软件包中包括已经编译完成的二进制程序以及需要的文件和配置文件 )

a. 直接获取安装需要的所有软件包的bundle 包 ,然后进行安装 【mysql 安装】

b. 直接配置mysql 官方的仓库 通过yum仓库安装

1. 添加yum 仓库
[root@bogon ~]# wget https://repo.mysql.com//mysql80-communityrelease-el8-9.noarch.rpm
--2024-04-27
 20:31:45-- https://repo.mysql.com//mysql80-communityrelease-el8-9.noarch.rpm
Resolving
 repo.mysql.com (repo.mysql.com)... 23.210.109.97, 
2a02:26f0:d8:980::1d68, 2a02:26f0:d8:98f::1d68
Connecting to repo.mysql.com (repo.mysql.com)|23.210.109.97|:443... 
connected.
HTTP request sent, awaiting response... 200 OK
Length: 17792 (17K) [application/x-redhat-package-manager]
Saving to: ‘mysql80-community-release-el8-9.noarch.rpm’
mysql80-community-release-el8-9 100%
[=====================================================>] 17.38K -
-.-KB/s   in 0.006s  
2024-04-27 20:31:45 (3.02 MB/s) - ‘mysql80-community-release-el8-
9.noarch.rpm’ saved [17792/17792]
[root@bogon ~]# ls 
aaa             Desktop   Downloads Music                       
                original-ks.cfg Public     Videos
anaconda-ks.cfg Documents keys       mysql80-community-releaseel8-9.noarch.rpm
Pictures         Templates
[root@bogon ~]# rpm -ivh mysql80-community-release-el8-9.noarch.rpm 
[root@bogon ~]# ls /etc/yum.repos.d/
aa.repo bb.repo mysql-community-debuginfo.repo mysqlcommunity.repo
mysql-community-source.repo redhat.repo
# 可选 , 如果已经在系统中进行了相关数据的安装(mariadb 以及 mysql)
# rpm -qa | egrep 'mysql|mariadb'
# 如果有提示任何包的安装信息,需要先卸载对应的包,然后再禁用下面的模块
yum module -y disable mysql # 禁用原本启用的mysql 模块 ,后续安装
时,可以使用第一步所设置的仓库进行安装 


2. 安装server 包
[root@bogon ~]# yum install mysql-community-server


3. 启动服务
完成数据的初始化
创建基本的数据库和表
完成mysql 超级用户 root@localhost的创建以及密码的生成(限制root用户 只能从
mysql 运行的节点登录)
[root@mysql8 ~]# systemctl start mysqld

# 抓取用户密码 
[root@mysql8 ~]# grep password /var/log/mysqld.log 
2024-04-28T00:27:27.590106Z 6 [Note] [MY-010454] [Server] A 
temporary password is generated for root@localhost: f%EqxVUy9d5u

# 修改密码
[root@mysql8 ~]# mysql -u root -p'f%EqxVUy9d5u' -h localhost 
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.36
Copyright (c) 2000, 2024, 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 'Redhat12~';
Query OK, 0 rows affected (0.01 sec)

3. 补充MySQL用户管理

默认情况下,只要root@localhost,也就是说 只能通过127.0.0.1建立连接并连接到数据库, 一旦在连接数据库时,不是使用127.0.0.1 就会拒绝请求

[root@mysql8 ~]# mysql -u root -p'Redhat12~' -h localhost 
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 15
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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.
[root@mysql8 ~]# mysql -u root -p'Redhat12~' -h 192.168.110.50
mysql: [Warning] Using a password on the command line interface can 
be insecure.
ERROR 1130 (HY000): Host '192.168.110.50' is not allowed to connect 
to this MySQL server
-h 192.168.110.50   // 指定数据库服务器的IP地址

需要先创建对应客户端IP地址的用户,才可以访问

mysql-1: 192.168.110.50

bogon: 192.168.110.131

#从localhost登录,授权可以从192.168.110.50登录
[root@mysql8 ~]# mysql -u root -p'Redhat12~' -h localhost 
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 15
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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. 
mysql> create user root@'192.168.110.50' identified by 'Redhat12~';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
再次尝试
[root@mysql8 ~]# mysql -u root -p'Redhat12~' -h 192.168.110.50
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 13
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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> use mysql;    //其实是一个全新为授权任何库或者表,或者语句,因此除了登录什么也不能干
                     // 通过select * from mysql.user 发现新建的名为root 
                     // 主机名为192.168.110.50 的用户任何权限都没有
ERROR 1044 (42000): Access denied for user 'root'@'192.168.110.50' to 
database 'mysql'
mysql> exit
Bye
因为只授权了 可以从192.168.110.50登录,在192.168.110.131上是无法登录的
[root@bogon ~]# mysql -u root -p'Redhat12~' -h 192.168.110.50
mysql: [Warning] Using a password on the command line interface can be 
insecure.
ERROR 1130 (HY000): Host '192.168.110.131' is not allowed to connect to this 
MySQL server
# 引申:如果需要从 192.168.110.131 登录数据库 就需要再授权一个用户可以从192.168.110.131登录

lamp  
grant all webapp.* to appuser@'192.168.110.%' identified by 'mima';

4. 升级降级

a. 确定安装方式

b. 数据迁移

二、配置

 调整数据库管理系统的密码策略

MySQL :: MySQL 8.0 参考手册 :: 8.4.3.2 密码验证选项和变量icon-default.png?t=N7T8https://dev.mysql.com/doc/refman/8.0/en/validate-password-options-variables.html

熟悉mysql配置文件结构

主配置文件 :/etc/my.cnf

额外的附加配置文件路径: /etc/my.cnf.d/*.cnf

数据目录:

auto.cnf   //持久生效的所有变量以及对应的值

binlog.index binlog.000002 binlog.000003 binlog.000001  //日志

client-key.pem         server-key.pem         ca-key.pem         ca.pem         private_key.pem         cert.pem         server-cert.pem         public_key.pem          // 加密通信需要使用的秘钥和证书

mysql performance_schema sys         //这三个是目录,所有的mysql下的库都会创 建属于自己的目录来保存库中表的数据

                                                            // 使用 innodb 存储引擎的表文件会被存储成一个文件

                                                            //使用 myisam 存储引擎的表文件,会存储成多个文件

ibdata1         '#ib_16384_0.dblwr'         ibtmp1        mysql.ibd         '#ib_16384_1.dblwr'

'#innodb_redo'         undo_001

client- ib_buffer_pool         '#innodb_temp'         undo_002          // innodb存储引擎运 行期间产生的额外数据(索引、事务等) 临时性的运行文件

MySQL 日志

update set =
二进制启用后,后导致数据库运行效率降低。可以通过二进制日志实现数据备份和数据同步的操作。
二进制日志不能很好的应对突然发生的服务中断(停止),只有已经执行完毕的语句和事务才会记录,
张中断时,正在进行的数据变化不会被记录到二进制日志。
-- log - bin 启用二进制日志。
-- skip - log - bin -- disable - log - bin 禁用二进制日志。
二进制日志的更新:(更新编号)
1 、每一次数据库守护进程重新启动
2 、 执行 flush 二进制日志
3 、设定了二进制日志的最大大小

三、备份与还原

数据库的数据备份就是为了进行数据还原。保证数据完整性和一致性。

备份:​​​​​​​

物理备份

1. 物理备份一般是针对数据库的数据目录进行的备份(可以直接使用文件操作命令实现)
2. 物理备份速度取决于磁盘 IO 速度,效率很高
3. 输出的备份日志更具有可读性
4. 物理备份可能包含不需要的日志文件以及配置
5. 对于数据备份的颗粒度,需要取决于不同的存储引擎
6. 物理备份的时候,一般需要设置专门的读写锁,或者停止 mysql 服务,来避免在备份过程中
出现任何新数据的写入,避免备份遗漏数据。

逻辑备份(mysqldump

1. 通过向数据库服务查询相关数据来产生数据备份
2. 逻辑备份的效率远低于物理备份
3. 逻辑备份的数据量可能远大于物理备份,兼容性相对较低(逻辑备份不能在版本差异较大的数据库之间实现数据的恢复)
4. 逻辑备份完全规避日志和配置文件
5. 逻辑备份期间,数据库需要处于运行状态
6. 逻辑备份使用 mysqldump SELELCT OUTFILE 文件路径
7. 进行数据恢复,通过 mysql 或者 LOAD DATA mysqlimport 实现

在线备份和离线备份 (热备份 和 冷备份)

mysql 服务处于运行状态下,进行的备份叫做在线备份(热备份),反之 冷备份和离线备份 一般对于一直处于业务处理场景下的数据库进行热备份 对于业务处理量较小或者是指主业务库的数据 副本节点进行冷备份。

本地备份和远程备份

在数据库的节点上进行的备份,是本地备份。从远程节点进行备份是远程备份
cp / tar // 本地备份(物理备份)
scp / sftp / rsync // 可以针对数据库进行远程备份程序 (物理备份)

快照备份

数据库本身并不提供这个功能。逻辑卷和 xfs Veritas ZFS 文件系统都提供快照的功能

全量备份 增量备份

每一次备份都是对于数据库全部文件的备份,称之为全量备份
每一次备份会和上一次备份比较,仅备份发生变化的部分,称之为增量备份

数据表的数据完整性

1. innodb 数据库一般不存在数据碎坏,丢失的问题
2. MyISAM 数据表比较可能因为缺少表结构文件、表索引、表数据文件等导致数据损坏,通过MyISAM检查工具检查表数据的完整性

mysqldump 备份数据库

使用场景:

1. 为了防止数据丢失,对整个数据库进行全量备份
2. 在进行主从架构搭建的时候,向从库导入数据的一种手段
3. 作为实验数据的源数据:
         为整个数据库制作一个副本,而不需要直接修改源数据库的数据
         为了检验数据库升级之后的数据迁移能够顺利进行

逻辑备份:

1. 备份 mysqldump
2. 恢复数据
1. 系统  shell mysql -u root -p [db] < dump.sql
2. MYSQL shell source 备份文件路径
备份:
[root@mysql-1 ~]# mysqldump -u root -predhat --databases test > backup.sql
// 有创建库的语句,所以只能恢复到原本的库中
[root@mysql-1 ~]# mysqldump -u root -predhat test > backup01.sql
[root@mysql-1 ~]# mysql -u root -predhat
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.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> create table a (id int, name char(8));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into a values(1, 'aaa'), (2, 'bbb'), (3, 'ccc');
Query OK, 3 rows affected (0.08 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from a;
+------+------+
| id   | name |
+------+------+
| 1    | aaa  |
| 2    | bbb  |
| 3    | ccc  |
+------+------+
3 rows in set (0.00 sec)
mysql> exit
Bye

[root@mysql-1 ~]# mysqldump -u root -predhat --databases test > backup.sql
// 有创建库的语句,所以只能恢复到原本的库中
mysqldump: [Warning] Using a password on the command line interface can be
insecure.
[root@mysql-1 ~]# cat backup.sql
-- MySQL dump 10.13 Distrib 8.0.36, for Linux (x86_64)
--
-- Host: localhost Database: test
-- ------------------------------------------------------
-- Server version 8.0.36
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,
FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO'
*/;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `test`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFAULT CHARACTER
SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N'
*/;
USE `test`;
--
-- Table structure for table `a`
--
DROP TABLE IF EXISTS `a`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `a` (
`id` int DEFAULT NULL,
`name` char(8) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `a`
--
LOCK TABLES `a` WRITE;
/*!40000 ALTER TABLE `a` DISABLE KEYS */;
INSERT INTO `a` VALUES (1,'aaa'),(2,'bbb'),(3,'ccc');
/*!40000 ALTER TABLE `a` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2024-04-28 23:47:16

[root@mysql-1 ~]# mysqldump -u root -predhat test > backup01.sql
mysqldump: [Warning] Using a password on the command line interface can be
insecure.
// 没有创建库的语句,所以可能恢复的任意指定的数据库中
[root@mysql-1 ~]# cat backup01.sql
-- MySQL dump 10.13 Distrib 8.0.36, for Linux (x86_64)
--
-- Host: localhost Database: test
-- ------------------------------------------------------
-- Server version 8.0.36
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,
FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO'
*/;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `a`
--
DROP TABLE IF EXISTS `a`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `a` (
`id` int DEFAULT NULL,
`name` char(8) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `a`
--
LOCK TABLES `a` WRITE;
 恢复:
[root@mysql-1 ~]# mysql -u root -predhat < backup.sql
mysql> source /root/backup.sql
​mysql> source /root/backup01.sql //这个备份中没有创建库和切换库的语句,所以恢复数据失败,需要先切换到库中(这里可以尝试恢复到其他库中)
[root@mysql-1 ~]# mysql -u root -predhat
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 11
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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> drop database test;
Query OK, 1 row affected (0.01 sec)
mysql> exit
Bye
[root@mysql-1 ~]# mysql -u root -predhat < backup.sql
mysql: [Warning] Using a password on the command line interface can be
insecure.
[root@mysql-1 ~]# mysql -u root -predhat
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 14
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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.
# 另一种恢复数据的方法
[root@mysql-1 ~]# mysql -u root -predhat
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 15
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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> drop database test;
Query OK, 1 row affected (0.01 sec)
mysql> source /root/backup.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
--tab: 
# 使用--tab 在备份时,分离表结构和表数据,在恢复时,也可以分开恢复
[root@mysql-1 ~]# mysqldump -u root -p --tab=/var/lib/mysql-files test
Enter password:
[root@mysql-1 ~]# ls /var/lib/mysql-files/
a.sql a.txt
# 恢复表
[root@mysql-1 ~]# mysql -u root -predhat test < /var/lib/mysql-files/a.sql
mysql: [Warning] Using a password on the command line interface can be
insecure.
# 需要test库是存在的
# 恢复数据
[root@mysql-1 ~]# mysqlimport -u root -predhat test /var/lib/mysql-files/a.txt
mysqlimport: [Warning] Using a password on the command line interface can
be insecure.
test.a: Records: 3 Deleted: 0 Skipped: 0 Warnings: 0

物理备份:

1. 备份:使用文件备份的命令 直接创建压缩包备份或者 cp 等命令创建数据文件副本
2. 恢复: 把备份的文件还原到原本的数据目录中即可,文件的权限和 selinux 标签是需要注意

[root@mysql-1 ~]# mkdir /db_backup; cp -r /var/lib/mysql/test/ /db_backup/
[root@mysql-1 ~]# ls /var/lib/mysql/test/
a.ibd
[root@mysql-1 ~]# ls /db_backup/test/
a.ibd
[root@mysql-1 ~]# rm -rf /var/lib/mysql/test/
[root@mysql-1 ~]# mysql -u root -predhat
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 27
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a              |
+----------------+
1 row in set (0.00 sec)
mysql> select * from a;
+------+------+
| id   | name |
+------+------+
| 1    | aaa  |
| 2    | bbb  |
| 3    | ccc  |
+------+------+
3 rows in set (0.01 sec)
mysql> exit
Bye
[root@mysql-1 ~]# systemctl restart mysqld
[root@mysql-1 ~]# ls /var/lib/mysql/test
ls: cannot access '/var/lib/mysql/test': No such file or directory
[root@mysql-1 ~]# mysql -u root -predhat
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.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.01 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from a;
ERROR 1812 (HY000): Tablespace is missing for table `test`.`a`.
mysql> exit
Bye
[root@mysql-1 ~]# systemctl stop mysqld
[root@mysql-1 ~]# mv /db_backup/test/ /var/lib/mysql/
[root@mysql-1 ~]# ls /var/lib/mysql/test
a.ibd
[root@mysql-1 ~]# restorecon -Rvv /var/lib/mysql/test/
Relabeled /var/lib/mysql/test from unconfined_u:object_r:default_t:s0 to
unconfined_u:object_r:mysqld_db_t:s0
Relabeled /var/lib/mysql/test/a.ibd from
unconfined_u:object_r:default_t:s0 to unconfined_u:object_r:mysqld_db_t:s0
[root@mysql-1 ~]# chown -R mysql.mysql /var/lib/mysql/test/
[root@mysql-1 ~]# systemctl restart mysqld
[root@mysql-1 ~]# mysql -u root -predhat
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.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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> select * from test.a;
+------+------+
| id   | name |
+------+------+
| 1    | aaa  |
| 2    | bbb  |
| 3    | ccc  |
+------+------+
3 rows in set (0.01 sec)
mysql> exit
Bye
# 使用tar 不需要关注文件的权限和标签
[root@mysql-1 ~]# cd /var/lib/mysql
[root@mysql-1 mysql]# tar -czf testdb.tar.gz test/
[root@mysql-1 mysql]# tar -tf testdb.tar.gz
test/
test/a.ibd
[root@mysql-1 mysql]# systemctl stop mysql
Failed to stop mysql.service: Unit mysql.service not loaded.
[root@mysql-1 mysql]# systemctl stop mysqld
[root@mysql-1 mysql]# pwd
/var/lib/mysql
[root@mysql-1 mysql]# rm -rf testq
[root@mysql-1 mysql]# rm -rf test
[root@mysql-1 mysql]# tar -xzf testdb.tar.gz .
tar: .: Not found in archive
tar: Exiting with failure status due to previous errors
[root@mysql-1 mysql]#
[root@mysql-1 mysql]# tar -xzf testdb.tar.gz -C .
[root@mysql-1 mysql]# ls test
a.ibd
[root@mysql-1 mysql]# ll -d test
drwxr-x---. 2 mysql mysql 19 Apr 29 00:09 test
[root@mysql-1 mysql]# ll -dZ test
drwxr-x---. 2 mysql mysql unconfined_u:object_r:mysqld_db_t:s0 19 Apr 29
00:09 test
[root@mysql-1 mysql]# rm -rf test
[root@mysql-1 mysql]# ll test
ls: cannot access 'test': No such file or directory
[root@mysql-1 mysql]# !tar
tar -xzf testdb.tar.gz -C .
[root@mysql-1 mysql]# ll -dZ test
drwxr-x---. 2 mysql mysql unconfined_u:object_r:mysqld_db_t:s0 19 Apr 29
00:09 test
[root@mysql-1 mysql]# ll -Z test
total 112
-rw-r-----. 1 mysql mysql unconfined_u:object_r:mysqld_db_t:s0 114688 Apr
29 00:09 a.ibd
[root@mysql-1 mysql]# systemctl start mysqld
[root@mysql-1 mysql]# mysql -u root -predhat
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.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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> select * from test.a;
+------+------+
| id   | name |
+------+------+
| 1    | aaa  |
| 2    | bbb  |
| 3    | ccc  |
+------+------+
3 rows in set (0.00 sec)
mysql> exit
Bye
增量备份一般是通过二进制日志来实现的

主从同步

"假设A是主B是从,A把干的事情写小本本1(二进制日志)上,然后一个叫I/O的家伙(I/O线程)把A的小本本1抄下来,抄到小本本2(中继日志)上,最后B按照小本本2上的内容去照着干"                                                                                         

1. 保证主节点开始二进制日志,从节点配置中继日志
2. 从节点的开启一个 I/O 线程读取主节点二进制日志的内容
3. 从节点读取主节点的二进制日志之后,会将去读的内容写入从节点的中继日志
4. 从节点开启 SQL 线程,读取中继日志的内容,并执行,来保证从节点的数据和主节点的数据保持一 致
缺点:
1. 从节点只能从指定的偏移量开始同步数据,也就是说偏移量之前的数据无法通过主从同步去完善,所以就需要通过逻辑备份等方式来导入到从节点。
2. 二进制日志因为只记录数据变化状态,而不是记录真实有哪些数据存在

主从同步的过程:

1. 分别修改主从节点的配置文件,启用对应类型的日志,需要配置局域网内数据库服务的编号,以区别不同的节点 需要重启服务
2. 在主节点授权,并导出二进制日志偏移量之前的数据,以方便后续数据的写入
3. 在从节点,连接主节点的二进制日志,开启从节点的相关线程
4. 检查从节点的主从同步状态
5. 导入提前备份的主节点数据
6. 在主节点新建数据,测试数据的同步的效果 ,在从节点写入的数据是不会同步到主节点的!!!!!
主节点:192.168.110.50 - mysql-1
从节点: 192.168.110.60 - mysql-2
主节点:
server-id 区别局域网内的mysql服务器
log-bin 开启二进制日志。并制定二进制日志的文件名
[root@mysql-1 ~]# cat /etc/my.cnf //如果my.cnf中没有[mysqld],那么将下面的内
容写入 /etc/my.cnf.d./mysql-server.cnf
文件末尾新增如下内容,确保几行在[mysqld]的配置块中:
log_bin=source-bin
server_id=1
validate_password.policy = low
validate_password.length = 4
validate_password.mixed_case_count = 0
validate_password.special_char_count = 0
validate_password.number_count = 0
[root@mysql-1 ~]# rm -f /var/lib/mysql/source-bin.*
[root@mysql-1 ~]# systemctl restart mysqld
[root@mysql-1 ~]# firewall-cmd --add-service=mysql
success
[root@mysql-1 ~]# firewall-cmd --add-service=mysql --permanent
success
从节点:
server-id
relay-log: 开启中继日志,并指定中继日志的文件名
[root@mysql-2 ~]# cat /etc/my.cnf //如果my.cnf中没有[mysqld],那么将下面的内容
写入 /etc/my.cnf.d./mysql-server.cnf
#文件末尾新增,确保几行在[mysqld]的配置块中
server_id = 2
relay_log = relipca-relay
relay_log_index = relipca-relay-index
[root@mysql-2 ~]# rm -f /var/lib/mysql/reli*
[root@mysql-2 ~]# ls /var/lib/mysql/rel*
ls: cannot access '/var/lib/mysql/rel*': No such file or directory
[root@mysql-2 ~]# systemctl restart mysqld

主节点创建同步用户

mysql> CREATE USER 'replica-user'@'192.168.110.%' IDENTIFIED BY
'redhat';
Query OK, 0 rows affected (0.01 sec)
mysql> select user from mysql.user where user = 'replica-user';
+--------------+
| user |
+--------------+
| replica-user |
+--------------+
1 row in set (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replicauser'@'192.168.110.%';
Query OK, 0 rows affected (0.00 sec)
# 增加一个读锁,避免在成功建立主从复制架构前产生任何的数据读写
mysql> FLUSH TABLES WITH READ LOCK;
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position   | Binlog_Do_DB            | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| source-bin.000001 | 730      |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

主节点需要在开始主从同步之前,将部分数据预先导入从数据库,避免后续SQL写入失败。

1. 使用任意一种方式,实现数据迁移到从节点即可。
mysqldump 导出主节点有但是从节点没有的数据
mysql < dump.sql 从节点导入主节点备份的数据

从节点同步主节点二进制日志

主节点:
mysql> UNLOCK TABLES;
从:
mysql> stop slave;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.110.50',
SOURCE_USER='replica-user', SOURCE_PASSWORD='redhat',
-> SOURCE_LOG_FILE='source-bin.000001', SOURCE_LOG_POS=730,
GET_MASTER_PUBLIC_KEY=1;
Query OK, 0 rows affected, 3 warnings (0.01 sec)
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.110.50
                  Master_User: replica-user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: source-bin.000001
          Read_Master_Log_Pos: 730
               Relay_Log_File: relipca-relay.000002
                Relay_Log_Pos: 327
        Relay_Master_Log_File: source-bin.000001
             Slave_IO_Running: Yes //这两行的值必须是YES
            Slave_SQL_Running: Yes //这两行的值必须是YES

验证主从同步

主节点创建库
从节点检查 同步成功
主:
mysql> create database testa;
Query OK, 1 row affected (0.01 sec)
从:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testa              |
+--------------------+
5 rows in set (0.01 sec)

加入新的从节点

两种方法:
1. 在新的从节点上,配置 change 语句即可
2. 从原本的从节点复制数据,修改配置文件并重启
         在原本的从节点上打包数据目录,方便传输
                原本从节点关闭数据库服务
                tar,命令打包
[root@mysql-2 ~]# systemctl stop mysqld
[root@mysql-2 ~]# cd /var/lib/mysql
[root@mysql-2 mysql]# tar -czf /root/salve1.tar.gz .
[root@mysql-2 ~]# scp slave1.tar.gz root@192.168.110.131:/root
root@192.168.110.131's password:
slave1.tar.gz
100% 2112KB 110.5MB/s
00:00
2. 将压缩包复制到新的从节点上
       scp 传输压缩包
       启动数据库服务
[root@mysql-2 ~]# scp salve1.tar.gz root@192.168.110.131:/root
root@192.168.110.131's password:
salve1.tar.gz
100% 2108KB 116.2MB/s
00:00
[root@mysql-2 ~]# systemctl start mysqld
3. 确保从节点上新的数据的安全上下文以及权限正确
        mysql 用户需要有完整的读写执行权限
        直接使用 restorecon 恢复数据目录的安全上下文为默认的标签
[root@bogon ~]# rm -rf /var/lib/mysql
[root@bogon ~]# mkdir /var/lib/mysql
[root@bogon ~]# tar -xzf salve1.tar.gz -C /var/lib/mysql/
[root@bogon ~]# chown mysql.mysql /var/lib/mysql
[root@bogon ~]# restorecon -R /var/lib/mysql
4. 在新的从节点上
         删除 /var/lib/mysql/auto.cnf
         修改配置文件
                server_id // 值和目前主从复制架构下的任何数据库的server_id 的值不重复
                relay_log相关的配置和原本的从节点保持一致
                skip_slave_start 在启动数据库时,跳过从节点复制线程的启动
[root@bogon ~]# rm -f /var/lib/mysql/auto.cnf
[root@bogon ~]# vim /etc/my.cnf
# 添加如下内容
server_id=3
skip-slave-start=1
relay_log = relipca-relay
relay_log_index = relipca-relay-index
5. 新的从节点上,启动数据库服务
        检查主从配置和原本从节点配置是否一致 show slave status host /user /log/pos)
        执行start slave 启动从线程。
​​​​​​​
[root@bogon ~]# systemctl start mysqld
[root@bogon ~]# mysql -uroot -p'Redhat12~'

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/589138.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【MySQL | 第十一篇】一条SQL语句在MySQL的执行过程

文章目录 11.一条SQL语句在MySQL的执行过程11.1MySQL整体架构11.2SQL语句在Server层执行流程11.3拓展&#xff1a;InnoDB存储引擎的更新操作11.3.1问题&#xff1a;为什么写了redolog日志还要写binlog日志&#xff1f;11.3.2问题&#xff1a;为什么要两阶段提交&#xff1f;11.…

《QT实用小工具·四十七》可交互的创意动态按钮

1、概述 源码放在文章末尾 该项目实现了可交互的创意动态按钮&#xff0c;包含如下功能&#xff1a; 所有颜色自定义 鼠标悬浮渐变 两种点击效果&#xff1a;鼠标点击渐变 / 水波纹动画&#xff08;可多层波纹叠加&#xff09; 额外鼠标移入/移出/按下/弹起的实时/延迟共8种事…

springboot 自动配置源码解读

什么是自动装配 当我们程序依赖第三方功能组件时&#xff0c;不需要手动将这些组件类加载到IOC容器中。例如 当程序需要用到redis时&#xff0c;在pom.xml文件中引入依赖&#xff0c;然后使用依赖注入的方式直接从IOC容器中拿到相应RedisTemplate实例。 SpringBootApplication …

【已解决】json文件太大无法打开怎么办+ijson报错

下载了一个json文档&#xff0c;尝试了全部的编辑器都打不开。。。 搜了搜或许可以使用ijson GitHub - ICRAR/ijson: Iterative JSON parser with Pythonic interfaces "Ijson is an iterative JSON parser with standard Python iterator interfaces." 示例代码&…

【C++ —— 多态】

C —— 多态 多态的概念多态的定义和实现多态的构成条件虚函数虚函数的重写虚函数重写的两个例外协变&#xff1a;析构函数的重写 C11 override和final重载、覆盖(重写)、隐藏(重定义)的对比 抽象类概念接口继承和实现继承 多态的继承虚函数表多态的原理动态绑定和静态绑定 单继…

VTK 的可视化方法:Glyph

VTK 的可视化方法&#xff1a;Glyph VTK 的可视化方法&#xff1a;Glyph标量、向量、张量将多边形数据的采集点法向量标记成锥形符号参考 VTK 的可视化方法&#xff1a;Glyph 模型的法向量数据是向量数据&#xff0c;因此法向量不能像前面讲到的通过颜色映射来显示。但是可以通…

25 JavaScript学习:var let const

JavaScript全局变量 JavaScript中全局变量存在多种情况和定义方式&#xff0c;下面详细解释并提供相应的举例&#xff1a; 使用var关键字声明的全局变量&#xff1a; var globalVar "我是全局变量";未使用var关键字声明的变量会成为全局变量&#xff08;不推荐使用&…

【前端】-【防止接口重复请求】

文章目录 需求实现方案方案一方案二方案三 需求 对整个的项目都做一下接口防止重复请求的处理 实现方案 方案一 思路&#xff1a;通过使用axios拦截器&#xff0c;在请求拦截器中开启全屏Loading&#xff0c;然后在响应拦截器中将Loading关闭。 代码&#xff1a; 问题&…

(刷题记录2)随机链表的复制

[刷题记录2]随机链表的复制 题目信息&#xff1a;题目思路(环境来自力扣OJ的C语言)&#xff1a;复杂度&#xff1a;代码和解释&#xff1a;1.遍历一遍原链表的同时&#xff0c;在每个原节点后面插入一个相同的新节点&#xff0c;共插入 n 个新节点。2.利用两者联系&#xff0c;…

神奇的Vue3 - 组件探索

神奇的Vue3 第一章 神奇的Vue3—基础篇 第二章 神奇的Vue3—Pinia 文章目录 神奇的Vue3了解组件一、注册组件1. 全局注册​2. 局部注册3. 组件命名 二、属性详解1. Props&#xff08;1&#xff09;基础使用方法&#xff08;2&#xff09;数据流向&#xff1a;单项绑定原则&…

ThreeJS:Mesh网格与三维变换

Mesh网格 ThreeJS中&#xff0c;Mesh表示基于以三角形为多边形网格(polygon mesh)的物体的类&#xff0c;同时也作为其它类的基类。 通过Mesh网格&#xff0c;我们可以组合Geometry几何体与Material材质属性&#xff0c;在3D世界中&#xff0c;定义一个物体。例如&#xff1a;之…

vue2(4)之scoped解决样式冲突/组件通信/非父子通信/ref和$refs/异步更新/.sync/事件总线/provide和inject

vue2 一、学习目标1.组件的三大组成部分&#xff08;结构/样式/逻辑&#xff09;2.组件通信3.综合案例&#xff1a;小黑记事本&#xff08;组件版&#xff09;4.进阶语法 二、scoped解决样式冲突**1.默认情况**&#xff1a;2.代码演示3.scoped原理4.总结 三、data必须是一个函数…

Copilot Venture Studio創始合伙人楊林苑確認出席“邊緣智能2024 - AI開發者峰會”

隨著AI技術的迅猛發展&#xff0c;全球正逐步進入邊緣計算智能化與分布式AI深度融合的新時代&#xff0c;共同書寫著分布式智能創新應用的壯麗篇章。邊緣智能&#xff0c;作為融合邊緣計算和智能技術的新興領域&#xff0c;正逐漸成為推動AI發展的關鍵力量。借助分布式和去中心…

由于找不到mfc140u.dll,无法继续执行的多种解决方法

在我们日常与计算机的密切互动中&#xff0c;或许不少用户都曾遇到过这样一个棘手的问题&#xff1a;系统突然弹出一个提示窗口&#xff0c;告知我们“找不到mfc140u.dll文件”。这个文件是Microsoft Foundation Class&#xff08;MFC&#xff09;库的一部分&#xff0c;用于支…

提升编码技能:学习如何使用 C# 和 Fizzler 获取特价机票

引言 五一假期作为中国的传统节日&#xff0c;也是旅游热门的时段之一&#xff0c;特价机票往往成为人们关注的焦点。在这个数字化时代&#xff0c;利用爬虫技术获取特价机票信息已成为一种常见的策略。通过结合C#和Fizzler库&#xff0c;我们可以更加高效地实现这一目标&…

20240502在WIN10下给X99平台上的M6000显卡安装驱动程序

20240502在WIN10下给X99平台上的M6000显卡安装驱动程序 2024/5/2 9:32 人工智能计算领域的领导者 | NVIDIA https://www.nvidia.cn/ C:\NVIDIA\DisplayDriver\552.22\Win11_Win10-DCH_64\International IMPORTANT NOTICE -- READ CAREFULLY: -------------------------------…

pmp培训机构哪个比较好,求推荐-

寻找一个自己认为比较好的PMP培训机构千万不要盲目&#xff0c;先在网上看看大家都推荐什么&#xff0c;看一下各个机构的老学员反馈&#xff0c;这些对我们的选择有非常大的帮助&#xff0c;最起码有了一些风评上的参考&#xff0c;现状就是目前线上机构的竞争比较大&#xff…

c语言从入门到函数速成(1)

温馨提醒&#xff1a;本篇文章适合人群&#xff1a;刚学c又感觉那个地方不怎么懂的同学以及以及学了一些因为自身原因停学一段时间后又继续学c的同学 好&#xff0c;正片开始。 主函数 学c时最先学的是我们c语言程序的主体函数&#xff0c;c的主函数有两种写法&#xff0c;这…

【JavaEE】Thread的方法和属性

文章目录 1、Thread的常见构造方法2、Thread的几个常见属性2.1 ID2.2 名称2.3 状态2.4 优先级2.5 是否后台线程2.6 是否存活2.7 是否被中断 3.补充说明3.1 Thread.sleep()的作用3.2 Thread.sleep()的异常处理方式 1、Thread的常见构造方法 方法说明Thread()创建线程对象Thread…

动态规划-子序列问题1

文章目录 1. 最长递增子序列&#xff08;300&#xff09;2. 摆动序列&#xff08;376&#xff09;3. 最长递增子序列的个数&#xff08;673&#xff09;4. 最长数对链&#xff08;646&#xff09; 1. 最长递增子序列&#xff08;300&#xff09; 题目描述&#xff1a; 状态表…
最新文章