-
先去mysql官网:https://downloads.mysql.com/archives/community/ 选择对应的版本下载,然后上传到Linux机器上
或者直接在linux上
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.4.0-1.el8.x86_64.rpm-bundle.tar
下载资源 -
使用
tar -xvf mysql-8.4.0-1.el8.x86_64.rpm-bundle.tar
解压缩
-
安装依赖
MySQL会依赖到一些第三方软件包,按照以下顺序进行安装:
1.mysal-community-common
2.mysql-community-client-plugins
3.mysql-community-libs
4.mysql-community-icu-data-files
5.mysql-community-client
6.mysql-community-server
安装命令:
rpm -ivh mysql-community-common-8.4.0-1.el8.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.4.0-1.el8.x86_64.rpm
rpm -ivh mysql-community-libs-8.4.0-1.el8.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.4.0-1.el8.x86_64.rpm
rpm -ivh mysql-community-client-8.4.0-1.el8.x86_64.rpm
rpm -ivh mysql-community-server-8.4.0-1.el8.x86_64.rpm
依赖安装完成后可以使用rpm -qa | grep mysql
检查:
或者直接使用yum安装:
yum install mysql
- 初始化mysql:
mysqld --initialize --console
,输入mysqld后如果出现以下错误:Could not open file ‘/var/log/mysqld.log’ for error logging: Permission denied解决办法:
- 检查/var/log/mysqld.log 文件是否存在,不存在则新建一个:
touch /var/log/mysqld.log
- 确认MySQL服务器用户对该文件具有写权限:
sudo chown mysql:mysql /var/log/mysqld.log
sudo chmod 644 /var/log/mysqld.log
5.登录MySQL:
初始化后,cat /var/log/mysqld.log |grep localhost
搜索日志查看MySQL临时密码6.登录MySQL:
mysql -uroot -p'QM4rIhbmeS:U'
又有报错:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (111)
解决方案:
[root@localhost mysql8] sudo systemctl start mysqld #开启服务失败
Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.
[root@localhost mysql8] sudo systemctl status mysqld #查看服务状态
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2024-07-26 14:32:17 CST; 6s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 8289 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=1/FAILURE)
Process: 8261 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 8289 (code=exited, status=1/FAILURE)
Status: "Server shutdown complete (with return value = 1)"
Error: 13 (Permission denied)
localhost.localdomain systemd[1]: Starting MySQL Server...
localhost.localdomain systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE
localhost.localdomain systemd[1]: mysqld.service: Failed with result 'exit-code'.
localhost.localdomain systemd[1]: Failed to start MySQL Server.
cat /var/log/mysqld.log #查看日志
看到最主要的错误还是权限不足:mysqld: File ‘./binlog.index’ not found (OS errno 13 - Permission denied)
给予权限:
sudo chown -R mysql:mysql /var/lib/mysql/
sudo chmod -R 755 /var/lib/mysql/
重新开启服务:可以看到登录成功!:
- 登录进数据库后,修改一个好记的密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';