一、安装准备
关闭防火墙
# systemctl stop firewalld.service
# systemctl disable firewalld.service
![](https://img-blog.csdnimg.cn/img_convert/8c3312e0534b229c33a16b3bf96f9d74.png)
2、 关闭SELinux 编辑 /etc/selinux/config
# SELINUX=disabled
![](https://img-blog.csdnimg.cn/img_convert/42f8a4e46588c13653cb68adc7f2f3ce.png)
3、创建MySQL用户和组
# 创建mysql的HOME目录
# mkdir -p /home/mysql
![](https://img-blog.csdnimg.cn/img_convert/28bcc560d2f24af56c9ddab1f00340fa.png)
# 创建MySQL组
# groupadd mysql
![](https://img-blog.csdnimg.cn/img_convert/0616a524531f137593a9383eda6b4fe7.png)
# 创建MySQL用户,指定组及默认路径
# useradd -r -d /home/mysql -g mysql mysql
![](https://img-blog.csdnimg.cn/img_convert/9539815d9d590f6575e02aa775ba81ce.png)
#将mysql默认路径的用户名和组改成mysql
# chown -R mysql:mysql /home/mysql
![](https://img-blog.csdnimg.cn/img_convert/abd61b48ac95eb28a7b2c2250be12d75.png)
二、安装MySQL
1、下载MySQL8
# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.30-linux-glibc2.12-i686.tar.xz
![](https://img-blog.csdnimg.cn/img_convert/7469c6598797ccaef242a1169430a020.png)
![](https://img-blog.csdnimg.cn/img_convert/ca3642cef258ca7fe2f4467e3da611b4.png)
2、解压MySQL
# tar -xvf mysql-8.0.30-linux-glibc2.12-i686.tar.xz
![](https://img-blog.csdnimg.cn/img_convert/d80e8c161026ed7581685d3645887ada.png)
3、修改MySQL解压后的名称
# mv mysql-8.0.30-linux-glibc2.12-i686 mysql
![](https://img-blog.csdnimg.cn/img_convert/360b4203f15bb9bcecb4013b365dc2ac.png)
4、将mysql目录移动至 /usr/local目录下,设置目录 /usr/local/mysql 所有者
# mv mysql /usr/local/
# chown -R mysql:mysql /usr/local/mysql
![](https://img-blog.csdnimg.cn/img_convert/646506c5921cd0d531c5b5179d2bf4c6.png)
5、查看MySQL目录结构
# tree -d -L 1 mysql
![](https://img-blog.csdnimg.cn/img_convert/3f23442a137e4a12ace4693252c6afdc.png)
6、配置环境变量,/etc/profile 为mysql环境变量,文件最后增加
# vi /etc/profile
export PATH=$PATh:/usr/local/mysql/bin
![](https://img-blog.csdnimg.cn/img_convert/80dd923c03af13169e162593b4cad6c9.png)
7、重新生成环境变量
# source /etc/profile
![](https://img-blog.csdnimg.cn/img_convert/bdf5251aa76e5af918299bd45f020822.png)
8、创建MySQL数据目录
# 创建数据目录
# mkdir /usr/local/mysql/data
![](https://img-blog.csdnimg.cn/img_convert/be0fa59e8b3eed44e095a8cd237b57f0.png)
# 将数据目录的用户和组改成mysql
# chown mysql:mysql /usr/local/mysql/data
![](https://img-blog.csdnimg.cn/img_convert/ba2d7a145e22014d421a7b16e3bb9bce.png)
# 更改数据目录权限
# chmod 750 /usr/local/mysql/data
![](https://img-blog.csdnimg.cn/img_convert/49bdca01153a4115d07ecc86adf8e5dc.png)
9、新建MySQL配置文件 /etc/my.cnf 添加如下内容
vi /etc/my.cnf
[mysqld]
server-id=1
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/data/error.log
socket=/tmp/mysql.sock
pid-file=/usr/local/mysql/data/mysql.pid
character-set-server=utf8
lower_case_table_names=1
innodb_log_file_size=1G
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password
[client]
port=3306
default-character-set=utf8
10、初始化MySQL数据库
mysqld --initialize --user mysql
# mysqld --initialize --user mysql
注意:如果这里缺少依赖包,注意查看提示 直接yum -y instal 依赖包名称
错误:
mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
解决:
![](https://img-blog.csdnimg.cn/img_convert/8fa4544d965c70b99d36e68941440a8e.png)
错误:
mysqld: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory
解决:
![](https://img-blog.csdnimg.cn/img_convert/1bbf4e9e1e8d98dcbee3a05be849580d.png)
11、查看初始化日志 mySQl的root用户密码
# cat /usr/local/mysql/data/error.log
输出如下信息:
[root@node-a bin]# cat /usr/local/mysql/data/error.log
2023-01-05T08:54:50.808389Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
2023-01-05T08:54:50.808434Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.30) initializing of server in progress as process 14863
2023-01-05T08:54:50.810583Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2023-01-05T08:54:50.818656Z 0 [Warning] [MY-013907] [InnoDB] Deprecated configuration parameters innodb_log_file_size and/or innodb_log_files_in_group have been used to compute innodb_redo_log_capacity=2147483648. Please use innodb_redo_log_capacity instead.
2023-01-05T08:54:50.822397Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-01-05T08:54:56.007346Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-01-05T08:55:22.908682Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ;<kuZuSiB6Av
看到如下信息,表示初始化成功
[InnoDB] InnoDB initialization has started.
[InnoDB] InnoDB initialization has ended.
默认密码为:;<kuZuSiB6Av
![](https://img-blog.csdnimg.cn/img_convert/418dd5675c4e8f217ffc2a601d5b0674.png)
三、启动与关闭MySQL
1、启动MySQL数据库
[root@node-a mysql]# support-files/mysql.server start
Starting MySQL.............. SUCCESS!
![](https://img-blog.csdnimg.cn/img_convert/0f2bf345b8cafa1c9164b85ba822517a.png)
2、查看MySQL数据库状态
[root@node-a mysql]# support-files/mysql.server status
SUCCESS! MySQL running (15177)
![](https://img-blog.csdnimg.cn/img_convert/7da82b565762d066a15c0e3117c7023f.png)
3、配置MySQL数据库的开机自启服务
# 复制mysql.server 到 /etc/init.d目录下
[root@node-a mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 使用chkconfig 添加 mysql服务到开机启动的列表中
[root@node-a mysql]# chkconfig --add mysqld
![](https://img-blog.csdnimg.cn/img_convert/452fb5e3904d487014c05a2818a8bb4c.png)
4、启动并查看MySQL状态
![](https://img-blog.csdnimg.cn/img_convert/9558a2b81ec567b6eeb6c39914f58d63.png)
5、查看系统配置的开机自启列表
![](https://img-blog.csdnimg.cn/img_convert/1be07de4e1f9a7ff8a134a5f9220df76.png)
6、关闭MySQL
[root@node-a mysql]# cd /usr/local/mysql/
[root@node-a mysql]# support-files/mysql.server stop
![](https://img-blog.csdnimg.cn/img_convert/da4eb400375bfb27dd85908627612cca.png)
![](https://img-blog.csdnimg.cn/img_convert/d1cbb82957c700c2ecb8149b136f85c3.png)
四、登录MySQL
本地链接
# mysql -u root -p
2、修改密码
mysql> alter user 'root'@'localhost' identified by 'Welcome_1';
Query OK, 0 rows affected (0.04 sec)
![](https://img-blog.csdnimg.cn/img_convert/50d5d2202d9ff2ad0e574a37d32b3cb3.png)
3、修改root用户允许远程登录
mysql> create user 'root'@'%' identified by 'Welcome_1';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to 'root'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
![](https://img-blog.csdnimg.cn/img_convert/acb05f0af6d6a76599e3d3129fd3185e.png)
4、远程链接
# 创建用户 user001 密码 Welcome_1
create user 'user001'@'%' identified by 'Welcome_1';
![](https://img-blog.csdnimg.cn/img_convert/14740857ac6a5b6d6daeffe45b13b976.png)
5、给用户授权
# 给用户授权
grant all on mysql.* to 'user001'@'%';
![](https://img-blog.csdnimg.cn/img_convert/2a84cae56b1b4a167c095c274b872fb0.png)
6、查看系统user表
#使用root用户查看系统的 user表
use mysql
select host,user from user;
![](https://img-blog.csdnimg.cn/img_convert/9c270f57d45d65a6fb974eb8ef62a30f.png)
7、查看系统db表
# 使用root用户查看系统的db表
use mysql;
select host,user,db from db where user='user001';
![](https://img-blog.csdnimg.cn/img_convert/e3312dc68c4a041bb3813f578fef9202.png)