Install MySQL on CentOS

下载MySQL yum包

https://dev.mysql.com/downloads/repo/yum/

1
wget http://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm

安装软件源

1
rpm -Uvh mysql80-community-release-el7-1.noarch.rpm

安装MySQL服务器

1
yum install mysql-community-server

MySQL服务相关命令

1
2
3
4
systemctl start mysqld
systemctl stop mysqld
systemctl restart mysqld
systemctl enable mysqld # 开机自启

修改root密码

刚安装的 MySQL root 密码可以在 /var/log/mysqld.log 中找到, 使用找到的密码登录数据库

1
mysql -uroot -p

然后输入刚才找到的密码即可进入数据库, 使用下面的数据库语句修改 root 用户的密码

1
ALTER USER 'root'@'localhost' IDENTIFIED BY 'A-COMPLEX-PASSWORD';

添加用户

1
CREATE USER 'hdvsyu'@'localhost' IDENTIFIED BY 'A-COMPLEX-PASSWORD';

创建数据库

1
CREATE DATABASE hdvsyu CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

将某个数据库的权限赋予某个用户

1
GRANT ALL PRIVILEGES ON hdvsyu.* TO 'hdvsyu'@'localhost';

刷新权限

1
FLUSH PRIVILEGES;

从数据库文件中恢复数据

1
source backup.sql