CentOS 7.8 安装 MySQL 5.7 教程
系统版本
命令:cat /etc/centos-release
输入:CentOS Linux release 7.8.2003 (Core)
安装方式
- yum 安装
- 编译安装(以后有时间介绍,不同的系统不同的版本,编译安装会有各种不同的问题)
yum 安装方式介绍
CentOS 7 默认安装了mariadb数据库,但是我们要使用MySQL
- 进入本机的源文件目录
cd /usr/local/src/
- 下载MySQL 5.7的repo源
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
- 安装MySQL源
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
- 使用yum安装mysql-server(这个过程可能要30分钟左右)
yum -y install mysql-server
- 安装完成
- 说明:如果是MySQL 8.0版本的安装源是:wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
- 参考:https://dev.mysql.com/downloads/repo/yum/
总下载量:208 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/7): libaio-0.3.109-13.el7.x86_64.rpm | 24 kB 00:00:00
(2/7): mysql-community-common-5.7.29-1.el7.x86_64.rpm | 311 kB 00:00:04
(3/7): mysql-community-libs-5.7.29-1.el7.x86_64.rpm | 2.5 MB 00:01:39
(4/7): mysql-community-libs-compat-5.7.29-1.el7.x86_64.rpm | 1.3 MB 00:00:44
(5/7): postfix-2.10.1-7.el7.x86_64.rpm | 2.4 MB 00:00:00
(6/7): mysql-community-client-5.7.29-1.el7.x86_64.rpm | 26 MB 00:09:19
(7/7): mysql-community-server-5.7.29-1.el7.x86_64.rpm | 175 MB 00:28:37
已安装:
mysql-community-libs.x86_64 0:5.7.29-1.el7
mysql-community-libs-compat.x86_64 0:5.7.29-1.el7
mysql-community-server.x86_64 0:5.7.29-1.el7
作为依赖被安装:
libaio.x86_64 0:0.3.109-13.el7
mysql-community-client.x86_64 0:5.7.29-1.el7
mysql-community-common.x86_64 0:5.7.29-1.el7
作为依赖被升级:
postfix.x86_64 2:2.10.1-7.el7
替代:
mariadb-libs.x86_64 1:5.5.56-2.el7
完毕!
数据库配置
- 启动MySQL:
systemctl start mysqld.service
- 查看MySQL状态:
systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2020-07-17 11:06:22 CST; 14s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 10007 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 9957 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 10010 (mysqld)
CGroup: /system.slice/mysqld.service
└─10010 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysql...
7月 17 11:06:19 flyone systemd[1]: Starting MySQL Server...
7月 17 11:06:22 flyone systemd[1]: Started MySQL Server.
- 查找数据库root用户的 临时密码:
grep "password" /var/log/mysqld.log
2020-07-17T03:06:20.464191Z 1 [Note] A temporary password is generated for root@localhost: Q8ry3siVj/ue
- 使用临时密码进入数据库:
mysql -uroot -p
- 如果不修改密码,会有如下报错
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
- 修改密码,如果密码太简单,会有如下报错
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
- 调整密码后:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Fly@123456';
Query OK, 0 rows affected (0.00 sec)
- 配置访问权限
mysql> update mysql.user set host='%' where host='localhost';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
- 或者
mysql> grant all on *.* to root@'%';
Query OK, 0 rows affected (0.00 sec)
- 刷新权限(重要 !!!)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
- Done !!!