CentOS7 通过YUM安装MySQL5.7的步骤

1.进入到要存放安装包的位置

cd /home/lnmp

2.查看系统中是否已安装 MySQL 服务,以下提供两种方式:

1
2
rpm -qa | grep mysql
yum list installed | grep mysql

3.如果已安装则删除 MySQL 及其依赖的包:

yum -y remove mysql-libs.x86_64

4.下载 mysql57-community-release-el7-8.noarch.rpm 的 YUM 源:

wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm

5.安装 mysql57-community-release-el7-8.noarch.rpm:

rpm -ivh mysql57-community-release-el7-8.noarch.rpm

安装完后,得到如下两个包(执行命令查看:find / -name "mysql-com*"):

1
2
mysql-community.repo
mysql-community-source.repo

6.安装 MySQL,出现提示的话,一路 Y 到底

先安装公钥以免报错:

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

yum install mysql-server

安装后查看版本号:

mysql --version
netstat -nltp

安装过程中如果报公钥未安装错误则见最后面解决方法。安装完毕后,运行mysql,然后在 /var/log/mysqld.log 文件中会自动生成一个随机的密码,我们需要先取得这个随机密码,以用于登录 MySQL 服务端:

1
2
service mysqld start
grep "password" /var/log/mysqld.log

将会返回如下内容,末尾字符串就是密码,把它复制下来:

1
A temporary password is generated for root@localhost: hilX0U!9i3_6

7.登录到 MySQL 服务端并更新用户 root 的密码:

注意:由于 MySQL5.7 采用了密码强度验证插件 validate_password,故此我们需要设置一个有一定强度的密码;

1
2
mysql -u root -p
hilX0U!9i3_6

然后更改密码

1
2
优先这个set password=password("youpassword");

或者alter user 'root'@'localhost' identified by 'youpassword';
或者update user set authentication_string=password("新密码") where user="root";
flush privileges;

设置用户 root 可以在任意 IP 下被访问:

1
grant all privileges on *.* to root@"%" identified by "new password";

设置用户 root 可以在本地被访问:

1
grant all privileges on *.* to root@"localhost" identified by "new password";

刷新权限使之生效:

flush privileges;

OK,输入 exit 后用新密码再次登录看看吧!

注意:如果用远程工具还是连接不上,试试用 iptables -F 命令来清除防火墙中链中的规则

8.MySQL控制命令:启动、停止、重启、查看状态

1
2
3
4
5
6
7
8
9
service mysqld start
service mysqld stop
service mysqld restart
service mysqld status
 
systemctl start mysqld
service mysqld stop
service mysqld restart
systemctl status mysqld

9.设置 MySQL 的字符集为 UTF-8:

打开 /etc 目录下的 my.cnf 文件(此文件是 MySQL 的主配置文件):

vim /etc/my.cnf

在 [mysqld] 前添加如下代码:

[client] default-character-set=utf8

在 [mysqld] 后添加如下代码:

character_set_server=utf8

再登录mysql,看看字符集,6个utf8就算OK

show variables like '%character%';

10.查看指定的数据库中指定数据表的字符集,如查看 mysql 数据库中 servers 表的字符集:

show table status from mysql like '%servers%';

查看指定数据库中指定表的全部列的字符集,如查看 mysql 数据库中 servers 表的全部的列的字符集:

show full columns from servers;

11. 忘记密码时,可用如下方法重置:

1
2
3
service mysqld stop
mysqld_safe --user=root --skip-grant-tables --skip-networking &
mysql -u root

进入MySQL后

1
2
3
use mysql;
update user set password=password("new_password") where user="root"; //旧版本
update user set authentication_string=password("新密码") where user="root"; //mysql5.7或以上
flush privileges;

12.一些文件的存放目录

配置文件

vim /etc/my.cnf

存放数据库文件的目录

cd /var/lib/mysql

日志记录文件

vim /var/log/ mysqld.log

服务启动脚本

/usr/lib/systemd/system/mysqld.service

socket文件

/var/run/mysqld/mysqld.pid

13.MySQL 采用的 TCP/IP 协议传输数据,默认端口号为 3306,我们可以通过如下命令查看:

netstat -anp

解决公钥未安装错误方法:

解决办法:


rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

再次安装,成功


yum install mysql-server

二. 设置root 登录密码为空


启动mysql服务端mysqld后,因为没有配置root及密码,启动mysql客户端会出错


systemctl restart mysqld

mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

设置免密登录


vi /etc/my.cnf

#添加跳过密码验证

skip-grant-tables

重启mysql服务


systemctl restart mysqld

设置root登录密码为空


mysql -uroot

mysql> use mysql

mysql> update user set authentication_string='' where user='root';

mysql> quit

关闭免密登录


vi /etc/my.cnf

#注释掉跳过密码验证

#skip-grant-tables

重启mysql服务


systemctl restart mysqld

登录mysql客户端不需要密码, 出现提示输入密码时直接回车:


mysql -uroot -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 6

Server version: 5.7.37

 

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql>