Centos7安装MySQL5.7(yum)

时间:2022-11-11 21:07:03

本人尝试过使用源码安装方式,那叫一个头疼,各种问题,于是采用yum方式安装,没想到如此简单:

此服务器是刚买的,所以以前没有安装过mysql,如果以前安装过mysql的,好像要卸载干净再安装(其实我也不懂~)。

开始吧:

1、配置yum源

# 下载mysql源安装包(默认5.7最新版本)
[root@pd ~]# wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm # 安装mysql源
[root@pd ~]# rpm -ivh mysql57-community-release-el7-8.noarch.rpm

检测yum源是否安装成功

[root@pd ~]# yum repolist enabled | grep "mysql.*-community.*"

Centos7安装MySQL5.7(yum)

如上图所示即安装成功

2、安装MySQL

[root@pd ~]# yum -y install mysql-server

等一会就安装好了

Centos7安装MySQL5.7(yum)

默认配置文件路径: 

/etc/my.cnf                              # 配置文件
/usr/lib/systemd/system/mysqld.service   # 服务启动脚本

3、配置 my.cnf

[root@pd ~]# vim /etc/my.cnf
Centos7安装MySQL5.7(yum)Centos7安装MySQL5.7(yum)
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server_id = 1
expire_logs_days = 3

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
my.cnf

4、启动mysql服务

[root@pd ~]# service mysqld restart

5、查看密码(因为安装好后,第一次登录密码为随机密码)

[root@pd ~]# grep "password" /var/log/mysqld.log

Centos7安装MySQL5.7(yum)

输入 mysql -u root -p 回车,再输入上面的随机密码,回车,即可登录MySQL。

6、重置密码

注意:

  • ip处填localhost,不要填%
  • 密码必须包含数字、字母、符号(为了安全)
mysql> alter user "root"@"localhost" identified by "新密码";

Centos7安装MySQL5.7(yum)

记得要刷新权限

mysql> flush privileges;

输入 exit 退出 MySQL,再次登录就可以用设置好的密码了。

7、添加远程登录用户

默认只允许 root 帐户在本地登录,如果要在其它机器上连接MySQL,必须修改 root 允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,可以添加一个新的帐户。

mysql> grant all privileges on *.* to "pd"@"%" identified by "密码" with grant option;

Centos7安装MySQL5.7(yum)

在 Navicat 中就可以使用这个新建的用户连接我们服务器的 MySQL啦。

Centos7安装MySQL5.7(yum)