centos 下安装 mysql5.7

目前(2019)年,MySQL 的最新版本为 MySQL 8.0 ,然而很多项目中依然使用的是 MySQL 5.7,个人开发环境中为了和线上项目数据库保持一致,也需要安装相同版本的 MySQL,即安装 MySQL 5.7
更新于: 2022-05-17 22:39:12

步骤如下

  • 添加执行用户名和组
  • yumrpm包下载 
  • 本地安装 mysql yum
  • 检查 mysql 源是否安装成功
  • 安装 MySQL 服务
  • 创建数据目录
  • 修改配置文件如下
# 添加执行用户名和组
groupadd mysql
useradd -g mysql mysql

# yum 源 rpm包下载
yum -y install wget
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
# 本地安装 mysql yum源
yum localinstall mysql57-community-release-el7-11.noarch.rpm
# 检查 mysql 源是否安装成功
yum repolist enabled | grep "mysql.*-community.*"
# 安装 MySQL 服务
yum install -y mysql-community-server --nogpgcheck
# 创建数据目录
mkdir /data/mysql
chown -R mysql:mysql /data/mysql

配置

vim /etc/my.cnf 

[mysqld]
character-set-server=utf8
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[root@localhost ~]#

服务管理

systemctl start mysqld
systemctl restart mysqld

查看登录密码

2022-05-17T05:57:45.261448Z 1 [Note] A temporary password is generated for root@localhost: u,&kGYSUl9!i

用来登录,必须马上改密码,否则无法下一步操作!

grep 'temporary password' /var/log/mysqld.log

登录修改密码

mysql -uroot -p
mysql> alter user 'root'@'localhost' identified by 'xxxy';
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec);

# 密码策略有时候会导致修改密码失败
SHOW VARIABLES LIKE 'validate_password%';
SET GLOBAL validate_password_policy=LOW;

修改root用户远程登录

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A


Database changed
mysql> UPDATE user SET Host='%' WHERE User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0


mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


mysql> exit

查看字符集

mysql -uroot -p

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

参考