作业需求
1、openEuler 二进制方式安装MySQL 8.0.x。
二、备份数据库
3.备份数据库school到/backup目录
4.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
5.直接将MySQL数据库压缩备份
第一题
1、openEuler 二进制方式安装MySQL 8.0.x。
#首先先进行二进制包的下载
wget -c https://mirrors.aliyun.com/mysql/MySQL-8.0/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
#建立用户和所属的组
[root@localhost ~]# groupadd -g 27 -r mysql
[root@localhost ~]# useradd -u 27 -g 27 -c 'MySQL Se/sbin/nologin mysql
#将二进制安装包进行解压缩,并且创建一个软连接文件
[root@localhost ~]# tar xf mysql-8.0.28-linux-glibc2ar.xz -C /usr/local/
[root@localhost ~]# ln -sv /usr/local/mysql-8.0.28-l12-x86_64/ /usr/local/mysql
'/usr/local/mysql' -> '/usr/local/mysql-8.0.28-linux86_64/'
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# mkdir mysql-files
#给mysql赋予权限
[root@localhost mysql]# chown mysql:mysql mysql-file
[root@localhost mysql]# chmod 750 mysql-files
#将mysql进行初始化
[root@localhost mysql]# bin/mysqld --initialize --user=mysql
[root@localhost mysql]# bin/mysql_ssl_rsa_setup
#生成密钥对,mysql进行连接
[root@localhost mysql]# bin/mysqld_safe --user=mysql & #mysql命令行启动指令
#指定为安全启动,登录用户为mysql在后台启动
进行玩初始化之后我们可以直接看到mysql生成的随机登录密码
#配置环境变量
[root@localhost mysql]# vi /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin/
#尝试登录
[root@localhost mysql]# mysql -u root -p 'aopVqGH2YG<>'
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
#报错并缺少依赖
官方手册指导
[root@localhost mysql]# yum install ncurses-compat-libs
#再次登录
[root@localhost mysql]# mysql -u root -p
Enter password:
#等录成功后修改密码
mysql> alter user root@localhost identified by 'Mysql@123';
Query OK, 0 rows affected (0.02 sec)
使用命令登录的不方便我们可以参考官方文档所给来进行操作
#首先先结束mysql进程(可以使用kill或者mysqladmin)
[root@localhost mysql]# mysqladmin -uroot -p shutdown
Enter password:
2024-01-23T07:01:01.605185Z mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.localdomain.pid ended
[1]+ Done bin/mysqld_safe --user=mysql
#提供服务脚本
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld
#提供配置文件(由于二进制安装没有my.cnf和my.cnf.d文件)
[root@node1 etc]# scp /etc/my.cnf 192.168.75.42:/etc/
[root@node1 etc]# scp -r /etc/my.cnf.d/ 192.168.75.42:/etc/
[root@localhost mysql]# vi /etc/my.cnfbasedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
socket=/tmp/mysql.socklog-error=/usr/local/mysql/data/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid
第二题
备份数据库
mysql> create database school;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.01 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.02 sec)
mysql> INSERT INTO student VALUES( 901,'张老大', ' 男',1985,'计算机系', '北京市海淀区');
®机系', '湖南省衡阳市');Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO student VALUES( 902,'张老二', ' 男',1986,'中文系', '北京市昌平区');
Query OK, 1 row affected (0.01 sec)mysql> INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
Query OK, 1 row affected (0.01 sec)mysql> INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO score VALUES(NULL,901, '计算机',98);
RT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO score VALUES(NULL,901, '英语', 80);
Query OK, 1 row affected (0.01 sec)mysql> INSERT INTO score VALUES(NULL,902, '计算机',65);
Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO score VALUES(NULL,902, '中文',88);
Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO score VALUES(NULL,903, '中文',95);
Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO score VALUES(NULL,904, '计算机',70);
Query OK, 1 row affected (0.01 sec)mysql> INSERT INTO score VALUES(NULL,904, '英语',92);
Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO score VALUES(NULL,905, '英语',94);
Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO score VALUES(NULL,906, '计算机',90);
Query OK, 1 row affected (0.01 sec)mysql> INSERT INTO score VALUES(NULL,906, '英语',85);
Query OK, 1 row affected (0.00 sec)
2.备份数据库school到/backup目录
#创建backup目录
[root@localhost ~]# mkdir /backup
[root@localhost ~]# cd /backup
#由题目可知需要将数据库school备份在/backup
[root@localhost backup]# mysqldump --opt -B -uroot -p school > school.sql
可以查看到school.sql脚本文件说明备份成功
最后时间为备份时间,也可作为我们完成备份的标志
3.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
[root@localhost backup]# mysqldump -u root -p -B school --set-gtid-purged=OFF > /backup/school_`date +%F`.sql
4.直接将MySQL数据库压缩备份
[root@localhost backup]# mysqldump -u root -p -B -A --set-gtid-purged=OFF | gzip > /backup/totle_`date +%F`.sql.gz
因为是整个数据库所以加上-A并且在后面修改后缀