Linux安装mysql8.0

时间:2024-01-24 07:29:48

1、解压安装包:

(1) 创建目录:/usr/local/mysql

(2) tar -xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz


2、配置依赖

使用yumremove mysql卸载mysql即其相关依赖 yum remove mysql

检查是否有MariaDB并卸载

rpm -qa | grep mariadb

有的话就删除rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64

# 安装mysql需要的依赖

yum install libaio



3、授权:

(1) chmod -R 777 /usr/local/mysql/


4、.创建mysql组合用户

(1) 创建用户组:

(2) 创建用户并添加到组:useradd -r -g mysql -s /bin/false mysql

(3) 将mysql目录所有权给创建的mysql用户:chown -R mysql:mysql ./


5、修改Mysql配置文件

(1) mysql配置文件在 /etc/my.cnf 中: vi /etc/my.cnf


6、初始化mysql

(1) cd /usr/local/mysql/mysql-8.0.20/bin

(2) 再执行./mysqld --initialize --console进行安装初始化

(3) 可能有报错:

2024-01-23T06:32:54.956657Z 0 [System] [MY-013169] [Server] /usr/local/mysql/mysql-8.0.20/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 1649112

2024-01-23T06:32:54.956681Z 0 [ERROR] [MY-010338] [Server] Can't find error-message file '/usr/local/mysql/mysql8/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.

2024-01-23T06:32:54.958677Z 0 [ERROR] [MY-010187] [Server] Could not open file '/usr/local/mysql/mysql8_log/mysql.log' for error logging: No such file or directory

2024-01-23T06:32:54.958717Z 0 [ERROR] [MY-013455] [Server] The newly created data directory /usr/local/mysql/mysql8_data/ by --initialize is unusable. You can remove it.

2024-01-23T06:32:54.958724Z 0 [ERROR] [MY-010119] [Server] Aborting

2024-01-23T06:32:54.958821Z 0 [System] [MY-010910] [Server] /usr/local/mysql/mysql-8.0.20/bin/mysqld: Shutdown complete (mysqld 8.0.20)  MySQL Community Server - GPL.


先把对应的目录创建,然后没目录授权:

bash`sudo mkdir -p /usr/local/mysql/mysql8_data_log/

sudo chown -R mysql:mysql /usr/local/mysql/mysql8_data_log/`

bash`sudo chown -R mysql:mysql /usr/local/mysql/mysql8_data/`


如果还不成功,可能是没有给目录授权



7、初始密码查找

(1) 运行后可能没有日志输出,就无法看到初始密码,就去/usr/local/mysql/mysql8_log的mysql.log

(2) 如图:

Linux安装mysql8.0_安装mysql

8、将MySQL添加到系统进程并设置自启动

(1) 添加到系统进程

cp /usr/local/mysql/mysql8/support-files/mysql.server /etc/init.d/mysqld

(2) 设置自动启动

chmod +x /etc/init.d/mysqld

systemctl enable mysqld  


可能最后一句会报错:

mysqld.service is not a native service, redirecting to systemd-sysv-install.

Executing: /usr/lib/systemd/systemd-sysv-install enable mysqld


可以先用下面的“常用命令”先启动


9、修改root用户登录密码并设置远程访问

使用 /mysql /usr/bin 命令将mysql命令全局化(环境变量)



10、常用命令:

(1) 启动mysql服务

systemctl start mysqld.service


(2) 停止mysql服务

systemctl stop mysqld.service


(3) 重启mysql服务

systemctl restart mysqld.service


(4) 查看mysql服务当前状态

systemctl status mysqld.service


(5) 设置mysql服务开机自启动

systemctl enable mysqld.service


(6) 停止mysql服务开机自启动

systemctl disable mysqld.service


(7) 错误原因排查

Mysql启动后,通过“systemctl status mysqld.service”查询,报下面的错误:

Linux安装mysql8.0_安装mysql_02


原因是:

Linux安装mysql8.0_安装mysql_03

Linux安装mysql8.0_安装mysql_04

这里46、47行,basedir和datadir是空的,要修改为与my.cnf一致。

basedir=/usr/local/mysql/mysql8

datadir=/usr/local/mysql/mysql8_data


要不然他就会找到

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

Linux安装mysql8.0_mysql_05

从上面可以看出:我们最好的安装地址是

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data



从上面可以看出,我们还需要把my.cnf放到安装目录下去


以上问题都解决完之后,重启,查看日志,已经没有问题了:

Linux安装mysql8.0_mysql_06



11、修改root用户登录密码

(1) 使用默认密码登录: ./mysql-uroot -p  然后输入刚才初始化时生成的初始化密码

(2) 修改为自己的密码:alter user user() identified by "123456";


12、设置允许远程访问

(1) # 切换到mysql数据库

(2) use mysql;# 更新为允许使用root远程访问

(3) update user set user.host = '%' where user.User = 'root';# 刷新生效改动

(4) flush privileges;


13、如果是阿里云服务器,请打开3306端口