CentOS 7.4 安装 MySQL 5.7 及常见问题
确定系统版本
命令:cat /etc/centos-release
输入:CentOS Linux release 7.4.1708 (Core)
安装方式
- yum 安装
- 编译安装(以后有时间介绍,不同的系统不同的版本,编译安装会有各种不同的问题)
yum 安装方式介绍
CentOS 7 默认安装了mariadb数据库,但是我们要使用MySQL
- 进入本机的源文件目录
cd /usr/local/src/
- 下载MySQL的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
- 安装完成
总下载量: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-03-29 01:28:50 CST; 16s ago
- 查找数据库root用户的 临时密码:
grep "password" /var/log/mysqld.log
2020-03-28T17:28:47.341327Z 1 [Note] A temporary password is generated for root@localhost: :8#I;as5F/yw
- 使用临时密码进入数据库:
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 'Limm123!!';
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> flush privileges;
Query OK, 0 rows affected (0.01 sec)
- Done !!!
MySQL 操作命令
- 启动:
systemctl start mysqld.service
- 重启:
systemctl restart mysqld.service
- 停止:
systemctl stop mysqld.service
- 状态:
systemctl status mysqld.service
- 设置MySQL开机启动:
systemctl enable mysqld.service
- 停止MySQL开机启动:
systemctl disable mysqld.service
常见问题
-
MySQL开启外网访问权限后还是无法访问
如果是阿里云服务器,找到对应实例的安全组 > 配置规则 > 添加安全组规则:如下图
-
No more mirrors to try.
安装过程如果出现如下报错,重新执行yum就好
mysql-community-common-5.7.29- FAILED
http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.29-1.el7.x86_64.rpm:
[Errno 14] curl#6 - "Could not resolve host: repo.mysql.com; Unknown error"
正在尝试其它镜像。
Error downloading packages:
mysql-community-common-5.7.29-1.el7.x86_64: [Errno 256] No more mirrors to try.
- 问题:MySQL server has gone away
在执行大文件的时候可能会报如上错误, 是因为MySQL限制了server接受的数据包的大小
查看当前的大小配置:
show VARIABLES like '%max_allowed_packet%';
- 第一种方式:
- 修改my.cnf(查找文件:
mysql --help | grep my.cnf
) - 添加:
max_allowed_packet = 100M
- 修改my.cnf(查找文件:
- 第二种方式(临时解决方案, 数据库重启之后会失效):
- 首先命令行进入MySQL数据库
- 执行:
set global max_allowed_packet=100*1024*1024;
- 第一种方式: