二进制方式安装MySQL并备份数据库

一、openEuler二进制方式安装MySQL 8.0.28版本

1.1 获取软件包

bash 复制代码
[root@openEuler3 ~]# wget -c https://mirrors.aliyun.com/mysql/MySQL-8.0/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz

1.2 解压软件包

bash 复制代码
[root@openEuler3 ~]#  dnf install -y tar xz
[root@openEuler3 ~]# tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@openEuler3 ~]# ln -sv /usr/local/mysql-8.0.28-linux-glibc2.12-x86_64/ /usr/local/mysql
'/usr/local/mysql' -> '/usr/local/mysql-8.0.28-linux-glibc2.12-x86_64/'

1.3 创建用户和用户组

bash 复制代码
[root@openEuler3 ~]# groupadd -g 27 -r mysql
[root@openEuler3 ~]# useradd -u 27 -g 27 -c 'MySQL Server' -r -s /sbin/nologin mysql

1.4 创建目录并修改权限

bash 复制代码
[root@openEuler3 ~]# cd /usr/local/mysql
[root@openEuler3 mysql]# mkdir mysql-files
[root@openEuler3 mysql]# chown mysql:mysql mysql-files
[root@openEuler3 mysql]# chmod 750 mysql-files

1.5 安装

bash 复制代码
[root@openEuler3 mysql]#  bin/mysqld --initialize --user=mysql
2024-02-21T03:03:21.878690Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.28-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 5067
2024-02-21T03:03:21.887090Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-02-21T03:03:22.366051Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-02-21T03:03:23.332212Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: wy11ZhmyaU-7
[root@openEuler3 mysql]# bin/mysql_ssl_rsa_setup
[root@openEuler3 mysql]# bin/mysqld_safe --user=mysql &
[1] 5115
[root@openEuler3 mysql]# Logging to '/usr/local/mysql/data/openEuler3.err'.
[root@openEuler3 mysql]# 2024-02-21T03:03:42.947144Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

1.6 配置环境变量

bash 复制代码
[root@openEuler3 mysql]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin/
[root@openEuler3 mysql]# source /etc/profile.d/mysql.sh
[root@openEuler3 mysql]# dnf install -y ncurses-compat-libs

1.7 测试并修改密码

bash 复制代码
[root@openEuler3 mysql]# mysql -uroot -p'wy11ZhmyaU-7'
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.28

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 '*********';
Query OK, 0 rows affected (0.01 sec)

1.8 提供服务脚本

bash 复制代码
#停止服务
[root@openEuler3 mysql]# mysqladmin -uroot -p'*********' shutdown
[root@openEuler3 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@openEuler3 mysql]# chkconfig --add mysqld
[root@openEuler3 ~]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
socket=/tmp/mysql.sock

log-error=/usr/local/mysql/data/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid

1.9 测试

bash 复制代码
[root@openEuler3 etc]# systemctl start mysqld
[root@openEuler3 etc]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
     Loaded: loaded (/etc/rc.d/init.d/mysqld; generated)
     Active: active (running) since Wed 2024-02-21 11:16:43 CST; 5s ago
       Docs: man:systemd-sysv-generator(8)
    Process: 5582 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
      Tasks: 39 (limit: 21389)
     Memory: 356.0M
     CGroup: /system.slice/mysqld.service
             ├─ 5595 /bin/sh /usr/local/mysql//bin/mysqld_safe --datadir=/usr/local/mysql/data/ --pid-file=/usr/local/mysql/data/mysqld.pid
             └─ 5753 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql//l

Feb 21 11:16:42 openEuler3 systemd[1]: Starting LSB: start and stop MySQL...
Feb 21 11:16:42 openEuler3 mysqld[5582]: Starting MySQL.
Feb 21 11:16:42 openEuler3 mysqld[5595]: Logging to '/usr/local/mysql/data/mysqld.log'.
Feb 21 11:16:43 openEuler3 mysqld[5582]:  SUCCESS!
Feb 21 11:16:43 openEuler3 systemd[1]: Started LSB: start and stop MySQL.
lines 1-16/16 (END)
[root@openEuler3 etc]# mysql -uroot -p'*********'
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.28 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>

二、创建数据库并备份数据库

2.1 创建数据库和数据表

bash 复制代码
mysql> create database school;
Query OK, 1 row affected (0.01 sec)

mysql> use school
Database changed
mysql> CREATE  TABLE  student (
    -> id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY,
    -> name  VARCHAR(20)  NOT NULL,
    -> sex  VARCHAR(4),
    -> birth  YEAR,
    -> department  VARCHAR(20),
    -> address  VARCHAR(50)
    -> );
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> CREATE  TABLE  score (
    -> id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY  AUTO_INCREMENT,
    -> stu_id  INT(10)  NOT NULL,
    -> c_name  VARCHAR(20),
    -> grade  INT(10)
    -> );
Query OK, 0 rows affected, 3 warnings (0.01 sec)

2.2 插入数据

bash 复制代码
mysql> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       |
| 903 | 张三      | 女   |  1990 | 中文系       | 湖南省永州市       |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)

mysql> select * from score;
+----+--------+-----------+-------+
| id | stu_id | c_name    | grade |
+----+--------+-----------+-------+
|  1 |    901 | 计算机    |    98 |
|  2 |    901 | 英语      |    80 |
|  3 |    902 | 计算机    |    65 |
|  4 |    902 | 中文      |    88 |
|  5 |    903 | 中文      |    95 |
|  6 |    904 | 计算机    |    70 |
|  7 |    904 | 英语      |    92 |
|  8 |    905 | 英语      |    94 |
|  9 |    906 | 计算机    |    90 |
| 10 |    906 | 英语      |    85 |
+----+--------+-----------+-------+
10 rows in set (0.00 sec)

2.3 备份数据库school到/backup目录

①代码

bash 复制代码
[root@openEuler3 ~]# mkdir /backup/
[root@openEuler3 ~]# cd /backup/
[root@openEuler3 backup]# mysqldump -uroot -p'*********' --opt -B school > school.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@openEuler3 backup]# more school.sql

②结果

2.4 备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库

①代码

bash 复制代码
[root@openEuler3 backup]# mysqldump --add-drop-table -uroot -p'*********' -d school > /backup/drop.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@openEuler3 backup]# more drop.sql

②结果

2.5 直接将MySQL数据库压缩备份

bash 复制代码
[root@openEuler3 backup]# mysqldump -uroot -p'*********' school | gzip > /backup/gzip.sql.gz
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@openEuler3 backup]# ll
total 12
-rw-r--r-- 1 root root 2355 Feb 21 11:39 drop.sql
-rw-r--r-- 1 root root 1173 Feb 21 11:44 gzip.sql.gz
-rw-r--r-- 1 root root 3558 Feb 21 11:33 school.sql
相关推荐
8Qi817 分钟前
Redis哨兵模式(Sentinel)深度解析
java·数据库·redis·分布式·缓存·sentinel
数据库小组20 分钟前
从业务库到实时分析库,NineData 构建 MySQL 到 SelectDB 同步链路
数据库·mysql·数据库管理工具·数据同步·ninedata·数据库迁移·selectdb
CDN36027 分钟前
CDN HTTPS 证书配置失败?SSL 部署与域名绑定常见问题
数据库·https·ssl
Chengbei1133 分钟前
一次比较简单的360加固APP脱壳渗透
网络·数据库·web安全·网络安全·系统安全·网络攻击模型·安全架构
寒秋花开曾相惜34 分钟前
(学习笔记)3.9 异质的数据结构(3.9.1 结构)
c语言·网络·数据结构·数据库·笔记·学习
mcooiedo44 分钟前
mybatisPlus打印sql配置
数据库·sql
wudl55661 小时前
MySQL 8.0.42 Docker 开发部署手册
数据库·mysql·docker
xhuiting1 小时前
MySQL专题总结(四)—— 高可用
java·数据库·mysql
kjmkq1 小时前
目工业级宽温SSD哪个品牌不掉盘最稳定?宽温环境下的稳定性性技术解析
数据库·存储
Predestination王瀞潞1 小时前
Java EE3-我独自整合(第二章:Spring IoC 入门案例)
数据库·spring·java-ee