centos7 安装mysql 5.7
1、安装mysql
-
从官网下载对应版本,本次下载的是5.7.38版本
-
下载地址:https://downloads.mysql.com/archives/community/
-
将压缩包拷贝至服务器中并解压
# 解压
tar -xvf mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar
[root@shell mysql]# ll
total 541528
-rw-r--r-- 1 7155 31415 28991900 Mar 23 2022 mysql-community-client-5.7.38-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 318868 Mar 23 2022 mysql-community-common-5.7.38-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 4363096 Mar 23 2022 mysql-community-devel-5.7.38-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 47993516 Mar 23 2022 mysql-community-embedded-5.7.38-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 23315792 Mar 23 2022 mysql-community-embedded-compat-5.7.38-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 132675656 Mar 23 2022 mysql-community-embedded-devel-5.7.38-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 2704332 Mar 23 2022 mysql-community-libs-5.7.38-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 1264876 Mar 23 2022 mysql-community-libs-compat-5.7.38-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 186231736 Mar 23 2022 mysql-community-server-5.7.38-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 126641600 Mar 23 2022 mysql-community-test-5.7.38-1.el7.x86_64.rpm
[root@shell mysql]#
- 主要安装
rpm -ivh mysql-community-common-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.38-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.38-1.el7.x86_64.rpm
# 安装 mysql-community-server 时会报错,提示缺少 libaio.so.1
# 可以在rpm 安装命令后加 --nodeps强制安装,但是建议先将缺少的包安装,否则后面无法启动mysqld
yum install libaio.so.1
# 再执行
rpm -ivh mysql-community-server-5.7.38-1.el7.x86_64.rpm
2、初始化mysql
# 启动mysql
systemctl start mysqld.service
# 设置开机自启
systemctl enable mysqld.service
# 查看mysql状态
systemctl status mysqld.service
# 若启动报错,可通过 journactl -xe 查看具体报错日志
journactl -xe
# 查看初始密码(要记住这个密码,之后修改密码会用到)
cat /var/log/mysqld.log | grep password
3、遇到的报错
- mysql初始化 报错 [ERROR] --initialize specified but the data directory has files in it. Aborting.
# 原因:是最开始没有清理干净mysql
journalctl -xe
……
[ERROR] --initialize specified but the data directory has files in i...borting.
# 解决方法
[root@shell mysql]# cat /etc/my.cnf | grep datadir
datadir=/var/lib/mysql
[root@shell mysql]# rm -fr /var/lib/mysql
[root@shell mysql]# systemctl start mysqld
[root@shell mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-03-29 10:56:05 CST; 23s ago
Docs: man:mysqld(8)