1. 检查系统版本
[root@sit-stf-ap01 local]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
系统是 CentOS 7.8 版本,RedHat Linux
2. 下载 MySQL 安装包
下载地址:
https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
下载的是免安装包,也可去 MySQL 官网下载需要安装的包。
3. 安装
- 上传安装包到 Linux 服务器上;
- 解压安装包
tar xvjf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
- 将解压后的文件重命名为 mysql-8.0
- 在该文件夹下创建 data 文件夹用于存储文件
[root@sit-stf-ap01 mysql-8.0]# mkdir data
[root@sit-stf-ap01 mysql-8.0]# ll
total 412
drwxr-xr-x 2 mysql mysql 4096 Mar 27 2020 bin
drwx------ 7 mysql mysql 4096 Oct 20 17:23 data
drwxr-xr-x 2 mysql mysql 55 Mar 27 2020 docs
drwxr-xr-x 3 mysql mysql 282 Mar 27 2020 include
drwxr-xr-x 6 mysql mysql 201 Mar 27 2020 lib
-rw-r--r-- 1 mysql mysql 404604 Mar 26 2020 LICENSE
drwxr-xr-x 4 mysql mysql 30 Mar 27 2020 man
-rw-r--r-- 1 mysql mysql 687 Mar 26 2020 README
drwxr-xr-x 28 mysql mysql 4096 Mar 27 2020 share
drwxr-xr-x 2 mysql mysql 77 Mar 27 2020 support-files
- 创建用户组和用户
[root@sit-stf-ap01 mysql-8.0]# groupadd mysql
[root@sit-stf-ap01 mysql-8.0]# useradd -g mysql mysql
[root@sit-stf-ap01 mysql-8.0]#
- 给用户授权
[root@sit-stf-ap01 mysql-8.0]# pwd
/usr/local/mysql-8.0
[root@sit-stf-ap01 mysql-8.0]# chown -R mysql.mysql /usr/local/mysql-8.0
[root@sit-stf-ap01 mysql-8.0]#
- 切换到 bin 目录,初始化基础信息,得到初始密码
[root@sit-stf-ap01 mysql-8.0]# cd bin
[root@sit-stf-ap01 bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql-8.0 --datadir=/usr/local/mysql-8.0/data/ --initialize
- 编辑 my.cnf 文件,
vi /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql-8.0/
datadir=/usr/local/mysql-8.0/data/
socket=/tmp/mysql.sock
character-set-server=UTF8MB4
- 添加 mysqld 服务到系统,要切换到 mysql 安装目录下执行命令才会生效
[root@sit-stf-ap01 bin]# cd ../
[root@sit-stf-ap01 mysql-8.0]# cp -a ./support-files/mysql.server /etc/init.d/mysql
- 授权及添加服务
[root@sit-stf-ap01 mysql-8.0]# chmod +x /etc/init.d/mysql
[root@sit-stf-ap01 mysql-8.0]# chkconfig --add mysql
- 启动服务
service mysql start
- 查看服务状态
service mysql status
- 将 mysql 命令添加到服务
ln -s /usr/local/mysql-8.0/bin/mysql /usr/bin
4. 登录 mysql
- 用初始密码登录 mysql
[root@sit-stf-ap01 mysql-8.0]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 365
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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>
- 修改 root 密码
其中123456是新的密码,你可以替换成你自己设置的密码。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
- 执行
flush privileges;
使密码生效 - 选择 mysql 数据库,修改远程连接并生效,退出
update user set host='%' where user='root';
flush privileges;
exit;
-
使用 Navicat 远程测试连接
踩坑记录
1. 执行 service mysql start
报错
[root@sit-yzjpt-ap01 mysql-8.0]# service mysql start
Starting MySQL.2022-06-15T03:42:12.547076Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
ERROR! The server quit without updating PID file (/usr/local/mysql-8.0/data//sit-yzjpt-ap01.pid).
解决方案:权限问题,创建相应的文件即可
[root@sit-yzjpt-ap01 mysql-8.0]# mkdir /var/log/mariadb
[root@sit-yzjpt-ap01 mysql-8.0]# touch /var/log/mariadb/mariadb.log
[root@sit-yzjpt-ap01 mysql-8.0]# chown -R mysql:mysql /var/log/mariadb/
再次执行启动命令,成功:
[root@sit-yzjpt-ap01 mysql-8.0]# service mysql start
Starting MySQL. SUCCESS!