MySQL的安装和备份

一、openEuler 二进制方式安装MySQL 8.0.x

1、获取软件包

[root@Localhost ~]# wget -c https://mirrors.aliyun.com/mysql/MySQL-8.0/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz

2、创建用户和组

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

3、解压软件包

[root@Localhost ~]# tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/

4、创建指向安装目录的软连接

[root@Localhost ~]# 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/'

5、初始化MySQL服务器

[root@Localhost ~]# cd /usr/local/mysql
[root@Localhost mysql]# mkdir mysql-files
[root@Localhost mysql]# chown mysql:mysql mysql-files
[root@Localhost mysql]# chmod 750 mysql-files
[root@Localhost mysql]# bin/mysqld --initialize --user=mysql
[root@Localhost mysql]# bin/mysql_ssl_rsa_setup
[root@Localhost mysql]# bin/mysqld_safe --user=mysql

6、配置环境变量

[root@Localhost mysql]# vim /etc/profile.d/mysql.sh
[root@Localhost mysql]# more /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin/

[root@Localhost mysql]# source /etc/profile.d/mysql.sh
[root@Localhost mysql]# yum install ncurses-compat-libs

7、登录并修改密码

[root@Localhost mysql]# mysql
mysql> alter user root@localhost identified by 'MySQL@123';
Query OK, 0 rows affected (0.01 sec)

8、提供服务脚本

[root@Localhost mysql]# mysqladmin -uroot -p'MySQL@123' shutdown
[root@Localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@Localhost mysql]# chkconfig --add mysqld

9、提供配置文件

[root@Localhost mysql]# scp /etc/my.cnf 192.168.190.144:/etc/
[root@Localhost mysql]# scp -r /etc/my.cnf.d/ 192.168.190.144:/etc/

[root@Localhost mysql]# vim /etc/my.cnf
#在/etc/my.cnf中添加以下内容
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、备份数据库school到/backup目录

[root@Localhost backup]# mysqldump --opt -B school > school.sql
[root@Localhost backup]# ll
total 0
-rw-r--r--. 1 root root 0 Feb 19 18:10 school.sql

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

[root@Localhost backup]# mysqldump -uroot -p --add-drop-table school > /backup/school.sql

3、直接将MySQL数据库压缩备份

[root@Localhost backup]# mysqldump -uroot -p school | gzip > school_2.sql.gz
Enter password:
[root@Localhost backup]# ll
total 8
-rw-r--r--. 1 root root  941 Feb 19 18:45 school_2.sql.gz
-rw-r--r--. 1 root root 2226 Feb 19 18:34 school.sql

你可能感兴趣的:(mysql,数据库)