CentOS 8 上MySQL 8.0 安装部署与配置教程

一、前言

1、主要内容

  • MySQL 8.0安装(yum)
  • MySQL 8.0 基础配置
  • MySQL shell管理常用语法示例(用户、权限等)
  • MySQL字符编码配置

2、环境信息与适用范围

  • 环境信息
软件 版本
CentOS 8.0 Release
MySQL 8.0.21
  • 适用范围
软件 版本
CentOS CentOS 8.0
MySQL 8.0.21+

二、安装

1、添加包

#创建文件夹
mkdir downloads
chmod 777 downloads

#导入RPM源
cd /home/downloads
wget https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
sudo rpm -ivh mysql80-community-release-el8-1.noarch.rpm

2、安装

#安装
sudo yum install -y mysql-server

#启动服务
sudo systemctl start mysqld

#查看版本信息
mysql -V

#mysql  Ver 8.0.21 for Linux on x86_64 (Source distribution)

3、root账号密码修改

#1、无密码进入MySQL shell
mysql -u root

#2、修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mypwd123!';

4、开放端口

#开放端口
firewall-cmd --add-port=3306/tcp --permanent

#重新加载防火墙设置
firewall-cmd --reload

三、MySQL安全设置

1、MySQL 8 安全设置介绍

MySQL 8 新增了安全设置向导,这对于在服务器部署MySQL来说,简化了安全设置的操作,非常棒。

安全设置大致分为以下几个步骤/选项

  1. 密码强度验证插件
  2. 修改root账号密码
  3. 移除匿名用户
  4. 禁用root账户远程登录
  5. 移除测试数据库(test)
  6. 重新加载授权表

以上几个步骤/选项根据自己需要来即可。

2、MySQL 8 安全设置示例

  • 进入安全设置
mysql_secure_installation
  • 设置示例
Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: N
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y

New password: 

Re-enter new password: 
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : 

 ... skipping.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done!

四、MySQL shell管理语法示例

1、数据库相关语法示例

#首先先赋予root权限
update user set host='%' where user='test';
flush privileges;

#创建数据库
mysql> CREATE DATABASE mydb;

#查看所有数据库
mysql> SHOW DATABASES;

#使用数据并创建表
mysql> USE mydb;
mysql> CREATE TABLE test(id int,body varchar(100));

#查看表
mysql> SHOW TABLES;

2、用户与访问授权语法示例

#新建本地用户
mysql> CREATE USER 'test'@'localhost' IDENTIFIED BY '123456';

#新建远程用户
mysql> CREATE USER 'test'@'%' IDENTIFIED BY '123456';

#赋予指定账户指定数据库远程访问权限
mysql> GRANT ALL PRIVILEGES ON mydb.* TO 'test'@'%';

#赋予指定账户对所有数据库远程访问权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'test'@'%';

#赋予指定账户对所有数据库本地访问权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost';

#刷新权限
mysql> FLUSH PRIVILEGES;

3、授权相关语法示例

#1、查看权限
SHOW GRANTS FOR 'test'@'%';

#2、赋予权限
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%';

#3、收回权限
REVOKE ALL PRIVILEGES ON *.* FROM 'test'@'%';

#4、刷新权限
FLUSH PRIVILEGES;

#5、删除用户
DROP USER 'test'@'localhost';

五、修改字符编码

1、 查找配置文件位置

[root@centos7 download]# whereis my.cnf
my: /etc/my.cnf

2、 修改配置文件

#修改配置文件
vi /etc/my.cnf

#修改1:增加client配置(文件开头)
[client]
default-character-set=utf8mb4

#修改2:增加mysqld配置(文件结尾)
#charset
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

3、 重启mysql服务

#重启后配置即可生效
systemctl restart mysqld

4、添加自动备份

1.查看磁盘空间找一个空间位置大的地方

df -h  

2.创建文件夹

cd /home/
mkdir backup
chmod 777 backup

3.创建自动化脚本(其中有两种方式,注意选择)

vim backup.sh 或vi backup.sh
db_user="username"
db_password="password"
db_name="database_name"
# the directory for story your backup file.you shall change this dir
backup_dir="/home/backup"
# date format for backup file (dd-mm-yyyy)
time="$(date +"%Y%m%d%H%M%S")"
# 备份指定数据库
mysqldump -u$db_user -p$db_password $db_name | gzip > $backup_dir/$db_name"_"$time.sql.gz

#备份所有数据库
#!/bin/bash
mysqldump -u$db_user -p$db_password --all-databases > $backup_dir/sqldata_$time.sql
#!/bin/bash
mysqldump -u$db_user -p$db_password --all-databases | gzip >  $backup_dir/$db_name"_"$time.sql.gz

4.添加执行权限

chmod u+x backup.sh 或chmod +x backup.sh

5.测试脚本

./backup.sh

6.创建定时任务

crontab -e
# 每天的凌晨三点执行一次脚本
0 3 * * * /backup.sh

7.定期删除备份文件(+15:15天之前的)


find /home/backup/ -name db_name"*.sql.gz" -type f -mtime +15 -exec rm -rf {} \; > /dev/null 2>&1

你可能感兴趣的:(Linux,环境部署,centos,mysql)