---
title: "CentOS7下以RPM方式安装mysql-5.7.20"
author: "jilin"
date: "2017-12-06T12:52:07Z"
tags: []
categories: ["数据库"]
slug: "centos7-rpm-mysql-5-7-20"
draft: false
lastmod: "2017-12-06T12:52:07Z"
id: 2403908
url: "https://jilin.iteye.com/blog/2403908"
---

1、准备4个文件

mysql-community-common-5.7.20-1.el7.x86\_64.rpm

mysql-community-libs-5.7.20-1.el7.x86\_64.rpm

mysql-community-client-5.7.20-1.el7.x86\_64.rpm

mysql-community-server-5.7.20-1.el7.x86\_64.rpm

2、因为centos7默认安装了MariaDB，需要先卸载，很关键

&gt; rpm -qa|grep mariadb

mariadb-libs-5.5.52-1.el7.x86\_64

mariadb-5.5.52-1.el7.x86\_64

&gt; rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86\_64

&gt; rpm -e --nodeps mariadb-5.5.52-1.el7.x86\_64

&gt; rpm -qa|grep mariadb #查看是否卸载成功

 

3、用root用户按1.的顺序安装，安装的时候会自动创建mysql用户

 

4、初始化mysql

&gt; mysqld --initialize --user=mysql

 

5、查看/etc/my.cnf文件（用于定位初始化时log日志，日志中记录了初始化的密码）

&gt; vi /etc/my.cnf

........

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

........

 

查看初始化密码

&gt; vi /var/log/mysqld.log

......

A temporary password is generated for root@localhost: UloEtAbnt2\*5

.......

(密码：UloEtAbnt2\*5)

 

6、启动MySQL

&gt; systemctl start mysqld

 

7、登陆mysql用户

&gt; mysql -u root -p

输入密码 UloEtAbnt2\*5（P.S. 见日志）

 

更改密码为mysql：

mysql&gt; set password=password('mysql');

 

8、修改用户可以远程连接

#先查看有哪些用户

mysql&gt; select host,user from user;  
+-----------+---------------+  
| host | user |  
+-----------+---------------+  
| % | root |  
| localhost | mysql.session |  
| localhost | mysql.sys |  
+-----------+---------------+

host为localhost代表只能本地访问。要root用户可以远程访问，可以把host改为%

&gt; update user set host='%' where user='root';

&gt; flush privileges; #刷新后才生效

增加用户：

&gt; grant all privileges on \*.* to 'admin'@'%' identified by 'admin123' with grant option; #增加admin用户,密码为admin123，允许可以远程访问