---
title: "Centos7 安装mysql"
author: "代码家的乐趣"
date: "2019-01-09T14:33:36Z"
tags: []
categories: ["企业架构"]
slug: "centos7-mysql"
draft: false
lastmod: "2019-01-09T14:33:22Z"
id: 2436341
url: "https://daimajia.iteye.com/blog/2436341"
---

在CentOS中默认安装有MariaDB，这个是MySQL的分支，但为了需要，还是要在系统中安装MySQL，而且安装完成之后可以直接覆盖掉MariaDB。
1 下载并安装MySQL官方的 Yum Repository


|# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm|

使用上面的命令就直接下载了安装用的Yum Repository，大概25KB的样子，然后就可以直接yum安装了。

|# yum -y install mysql57-community-release-el7-10.noarch.rpm|

之后就开始安装MySQL服务器。

|# yum -y install mysql-community-server|


centos7 上查看mysql 命令：

systemctl enable mysqld.service  #设置开机启动

systemctl start mysqld.service  #启动mysql

systemctl stop mysqld.service  #停止mysql

systemctl restart mysqld.service  #重启mysql

systemctl status mysqld.service 查看mysql 状态


此时MySQL已经开始正常运行，不过要想进入MySQL还得先找出此时root用户的密码，通过如下命令可以在日志文件中找出密码：
grep "password" /var/log/mysqld.log 

root@localhost:初始密码


# mysql -uroot -p
输入初始密码 回车

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';去更改新密码

<span style="color:red">配置vim /etc/my.cnf
加上 端口 port=3306
#skip-grant-tables
安全模式启动  如果注释掉就在非安全模式下启动，太危险了， 如果忘记密码，可以去注释掉，然后重启，直接进入mysql 或者用mysql 连接 进入user 表去修改密码
<span style="color:orange">忘记密码修改密码操作：
MySQL5.7 以后 password 字段为：authentication_string 

update mysql.user set authentication_string=password('root') where user='root' ;
</span>

用户创建、授权以及删除
创建用户
CREATE USER yy IDENTIFIED BY '123';

yy表示你要建立的用户名，后面的123表示密码 
上面建立的用户可以在任何地方登陆。 
如果要限制在固定地址登陆，比如localhost 登陆：
CREATE USER yy@localhost IDENTIFIED BY '123';


|mysql> GRANT ALL PRIVILEGES ON *.* TO user;



grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";

格式：grant select on 数据库.* to 用户名@登录主机 identified by “密码”


修改密码
mysql> grant   all   privileges   on   pureftpd.*   to   koko@localhost   identified   by   'mimi';  
查看用户信息：
mysql> select host,user from mysql.user; 
 </span>
上面操作也可以root账户连接mysql工具 在user表里直接创建也行。

最后卸载掉  yum -y remove mysql57-community-release-el7-10.noarch
最后注释掉my.cnf 里面的 skip-grant-tables 重启mysql
systemctl restart mysqld.service,用新建的用户连接测试
参考文章：<https://www.cnblogs.com/bigbrotherer/p/7241845.html>
参考文章：<https://blog.csdn.net/qq_27575627/article/details/50172673>