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

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

1.1 获取软件包

[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 解压软件包

[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 创建用户和用户组

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

1.4 创建目录并修改权限

[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 安装

[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 配置环境变量

[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 测试并修改密码

[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 提供服务脚本

#停止服务
[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 测试

[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 创建数据库和数据表

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 插入数据

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目录

①代码

[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数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库

①代码

[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数据库压缩备份 

[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
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值