1.上传软件包
# 下载地址:https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
上传安装包至/apps/scripts目录下
2.解压及更名
[root@db scripts]# tar -xf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz -C /apps/
[root@db scripts]# cd ..
[root@db apps]# mv mysql-8.0.32-linux-glibc2.12-x86_64 mysql
[root@db apps]# cd mysql
4. 创建用户、组
# 创建用户
[root@db mysql]# groupadd mysql -g 666
[root@db mysql]# useradd mysql -g 666 -u 666 -r -M -s /sbin/nologin
# 初始化数据库
[root@db mysql]# bin/mysqld --initialize --user=mysql --basedir=/apps/mysql --datadir=/apps/mysql/data --lower_case_table_names=1
# --lower_case_table_names=1在初始化时候,添加该参数是因为在my.cnf中存在该参数,不加会报错
# A temporary password is generated for root@localhost: 3X(G>W0kdqBm
# 3X(G>W0kdqBm为自动生成的root密码,需要记录
10. 安装包准备
# 注册MySQL启动服务
[root@db mysql]# vim /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Start main service
ExecStart=/apps/mysql/support-files/mysql.server start
# Sets open_files_limit
LimitNOFILE = 65535
Restart=on-failure
RestartPreventExitStatus=1
# Set environment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false
# 需修改/apps/mysql/support-files/mysql.server文件中basedir和datadir,其他内容无需修改
[root@db mysql]# vim /apps/mysql/support-files/mysql.server
basedir=/apps/mysql
datadir=/apps/mysql/data
# 重载服务
[root@db mysql]# systemctl daemon-reload
# 启动mysql服务
[root@db mysql]# systemctl start mysqld
11. 测试连接
# 测试连接
[root@db mysql]# bin/mysql -h127.0.0.1 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
# 默认密码修改
mysql> alter user root@localhost identified by 'Abc123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
12. 修改环境变量
# 添加环境变量
[root@db mysql]# vim /etc/profile
# 在最后添加以下两行
export MYSQL_HOME=/apps/mysql
export PATH=$PATH:$MYSQL_HOME/bin
# 重新加载配置
[root@db mysql]# source /etc/profile
# 切换目录测试环境变量是否添加成功
[root@db mysql]# cd /
[root@db /]# mysql -h127.0.0.1 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.32 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
13. 相关操作
# 创建数据库
[root@db /]# mysql -h127.0.0.1 -uroot -p
mysql> CREATE DATABASE sql_test DEFAULT CHARACTER SET UTF8MB4;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sql_test |
| sys |
+--------------------+
5 rows in set (0.00 sec)
# 允许其他主机连接该数据库
mysql> use mysql;
mysql> UPDATE user SET Host = '%' WHERE User = 'root';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
# 导入数据
[root@db scripts]# mysql -h127.0.0.1 -uroot -pAbc123 sql_test < ./sql_test.sql
# 防火墙开放3306端口
[root@db scripts]# firewall-cmd --zone=public --list-ports
[root@db scripts]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
[root@db scripts]# firewall-cmd --reload