1.在MySQL官网下载mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
2.使用rz命令或者xftp工具上传mysql安装包到linux目录
[root@VM-1-9-centos ~]# cd /mnt/datadisk0/package
[root@VM-1-9-centos package]# pwd
/mnt/datadisk0/package
[root@VM-1-9-centos package]# rz
如果出现: -bash: rz: 未找到命令 ,则是未安装lrzsz ,安装lrzsz即可
[root@VM-1-9-centos ~]# yum -y install lrzsz
[root@VM-1-9-centos package]# rz
[root@VM-1-9-centos package]# ll
total 853488
-rw-r--r-- 1 root root 873967604 Apr 19 22:34 mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
3.解压mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz到/usr/local/目录下
[root@VM-1-9-centos package]# tar -xvf mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
4.重命名为mysql
[root@VM-1-9-centos package]# cd /usr/local/
[root@VM-1-9-centos local]# mv mysql-8.0.23-linux-glibc2.12-x86_64/ mysql
[root@VM-1-9-centos local]# ll
total 46
drwxr-xr-x. 2 root root 4096 Apr 11 2018 bin
drwxr-xr-x. 2 root root 4096 Apr 11 2018 etc
drwxr-xr-x. 2 root root 4096 Apr 11 2018 games
drwxr-xr-x. 2 root root 4096 Apr 11 2018 include
drwxr-xr-x. 2 root root 4096 Apr 11 2018 lib
drwxr-xr-x. 2 root root 4096 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 4096 Apr 11 2018 libexec
drwxr-xr-x 9 root root 4096 Apr 19 22:40 mysql
drwxr-xr-x 13 root root 4096 Apr 12 21:14 qcloud
drwxr-xr-x. 2 root root 4096 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 4096 Mar 7 2019 share
drwxr-xr-x. 2 root root 4096 Apr 11 2018 src
5.创建用户和用户组,并对mysql目录进行授权
[root@VM-1-9-centos local]# groupadd mysql
[root@VM-1-9-centos local]# useradd -r -g mysql mysql
[root@VM-1-9-centos local]# chown -R mysql:mysql mysql/
[root@VM-1-9-centos local]# ll
total 46
drwxr-xr-x. 2 root root 4096 Apr 11 2018 bin
drwxr-xr-x. 2 root root 4096 Apr 11 2018 etc
drwxr-xr-x. 2 root root 4096 Apr 11 2018 games
drwxr-xr-x. 2 root root 4096 Apr 11 2018 include
drwxr-xr-x. 2 root root 4096 Apr 11 2018 lib
drwxr-xr-x. 2 root root 4096 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 4096 Apr 11 2018 libexec
drwxr-xr-x 9 mysql mysql 4096 Apr 19 22:40 mysql
drwxr-xr-x 13 root root 4096 Apr 12 21:14 qcloud
drwxr-xr-x. 2 root root 4096 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 4096 Mar 7 2019 share
drwxr-xr-x. 2 root root 4096 Apr 11 2018 src
6.找一个空间充足点的磁盘空间目录,创建data、logs、binlog等目录
查看系统空间挂载信息可利用 df -hl 命令
[root@VM-1-9-centos mysql]# df -hl
Filesystem Size Used Avail Use% Mounted on
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 24K 32G 1% /dev/shm
tmpfs 32G 672K 32G 1% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/vda1 50G 4.2G 43G 9% /
/dev/vdb 4.9T 149G 4.5T 4% /mnt/datadisk0
tmpfs 6.3G 0 6.3G 0% /run/user/0
[root@VM-1-9-centos local]# cd /mnt/datadisk0/
[root@VM-1-9-centos datadisk0]# mkdir mysql
[root@VM-1-9-centos datadisk0]# cd mysql/
[root@VM-1-9-centos mysql]# mkdir data
[root@VM-1-9-centos mysql]# mkdir logs
[root@VM-1-9-centos mysql]# mkdir binlog
[root@VM-1-9-centos mysql]# mkdir undo
[root@VM-1-9-centos mysql]# ll
total 16
drwxr-xr-x 2 root root 4096 Apr 19 22:57 binlog
drwxr-xr-x 2 root root 4096 Apr 19 22:56 data
drwxr-xr-x 2 root root 4096 Apr 19 22:56 logs
drwxr-xr-x 2 root root 4096 Apr 19 22:57 undo
[root@VM-1-9-centos mysql]# cd /mnt/datadisk0/mysql/logs/
#创建下面几个文件,要不然后面会报错
[root@VM-1-9-centos logs]# touch err.log
[root@VM-1-9-centos logs]# touch mysql-slow.log
[root@VM-1-9-centos logs]# echo '' > mysql.pid
[root@VM-1-9-centos logs]# ll
total 4
-rw-r--r-- 1 mysql mysql 0 Apr 19 23:40 err.log
-rw-r--r-- 1 mysql mysql 1 Apr 19 23:40 mysql.pid
-rw-r--r-- 1 mysql mysql 0 Apr 19 23:40 mysql-slow.log
7.更改目录所属账户、用户组
[root@VM-1-9-centos mysql]# cd /mnt/datadisk0
[root@VM-1-9-centos datadisk0]# chown -R mysql.mysql mysql/
[root@VM-1-9-centos datadisk0]# cd mysql/
[root@VM-1-9-centos mysql]# ll
total 16
drwxr-xr-x 2 mysql mysql 4096 Apr 19 22:57 binlog
drwxr-xr-x 2 mysql mysql 4096 Apr 19 22:56 data
drwxr-xr-x 2 mysql mysql 4096 Apr 19 22:56 logs
drwxr-xr-x 2 mysql mysql 4096 Apr 19 22:57 undo
8.创建my.cnf文件
[root@VM-1-9-centos mysql]# cd /usr/local/mysql/
[root@VM-1-9-centos mysql]# ll
total 408
drwxr-xr-x 2 mysql mysql 4096 Dec 11 19:03 bin
drwxr-xr-x 2 mysql mysql 4096 Dec 11 19:03 docs
drwxr-xr-x 3 mysql mysql 4096 Dec 11 19:03 include
drwxr-xr-x 6 mysql mysql 4096 Dec 11 19:03 lib
-rw-r--r-- 1 mysql mysql 382369 Dec 11 15:42 LICENSE
drwxr-xr-x 4 mysql mysql 4096 Dec 11 19:03 man
-rw-r--r-- 1 mysql mysql 666 Dec 11 15:42 README
drwxr-xr-x 28 mysql mysql 4096 Dec 11 19:03 share
drwxr-xr-x 2 mysql mysql 4096 Dec 11 19:03 support-files
[root@VM-1-9-centos mysql]# cd support-files/
[root@VM-1-9-centos support-files]# touch my-default.cnf
[root@VM-1-9-centos support-files]# chmod 777 ./my-default.cnf
[root@VM-1-9-centos support-files]# ll
total 20
-rwxrwxrwx 1 root root 0 Apr 19 23:03 my-default.cnf
-rwxr-xr-x 1 mysql mysql 1061 Dec 11 15:42 mysqld_multi.server
-rw-r--r-- 1 mysql mysql 2048 Dec 11 18:22 mysql-log-rotate
-rwxr-xr-x 1 mysql mysql 10576 Dec 11 18:22 mysql.server
[root@VM-1-9-centos support-files]# cp my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? yes
9.向my.cnf中写入配置信息写入
[root@VM-1-9-centos support-files]# vim /etc/my.cnf
#键入一下信息:
[client]
port = 3306
socket = /mnt/datadisk0/mysql/logs/mysql.sock
[mysqld]
init-connect='SET NAMES utf8'
#mysql安装目录
basedir=/usr/local/mysql
#数据存放目录
datadir=/mnt/datadisk0/mysql/data
socket=/mnt/datadisk0/mysql/logs/mysql.sock
#允许最大连接数
max_connections=1024
#等待超时
wait_timeout=28800
interactive_timeout=28800
#单次写入数据最大大小
max_allowed_packet=128M
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#开启慢查询
slow_query_log = 1
#慢查询执行的秒数,必须达到此值可被记录
long_query_time = 5
#慢查询sql日志设置
slow_query_log_file =/mnt/datadisk0/mysql/logs/mysql-slow.log
#binlog日志设置
binlog_format = row
log_bin_trust_function_creators = 1
log_bin = /mnt/datadisk0/mysql/binlog/mysql-bin
relay-log = /mnt/datadisk0/mysql/binlog/mysql-relay-bin
#mysql binlog日志文件保存的过期时间,过期后自动删除
expire_logs_days = 10
#undo
innodb_undo_directory = /mnt/datadisk0/mysql/undo
#错误日志
log-error = /mnt/datadisk0/mysql/logs/err.log
#pid
pid-file= /mnt/datadisk0/mysql/logs/mysql.pid
log_timestamps = SYSTEM
更多配置参数可查看 performance_schema 库下面的 global_variables 表和 session_variables 表
10.设置开机自启
[root@VM-1-9-centos support-files]# cp mysql.server /etc/init.d/mysql
[root@VM-1-9-centos support-files]# chmod +x /etc/init.d/mysql
11.注册mysql服务
[root@VM-1-9-centos support-files]# chkconfig --add mysql
[root@VM-1-9-centos support-files]# chkconfig --list mysql
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
12.修改etc/ld.so.conf配置文件
[root@VM-1-9-centos support-files]# vim /etc/ld.so.conf
#修改为:
include ld.so.conf.d/*.conf
/usr/local/mysql/lib
13.配置环境变量
[root@VM-1-9-centos support-files]# vim /etc/profile
#在最后加入:
#MySQL
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
14.更新环境变量
[root@VM-1-9-centos support-files]# source /etc/profile
15.初始化mysql
[root@VM-1-9-centos support-files]# cd /usr/local/mysql/
#datadir为上面my.cnf配置的datadir参数值
[root@VM-1-9-centos mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/mnt/datadisk0/mysql/data
如果出现以下错误:
bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
利用命令 yum -y install numactl 下载 numactl 即可
[root@VM-1-9-centos mysql]# yum -y install numactl
[root@VM-1-9-centos mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/mnt/datadisk0/mysql/data
2021-04-19T15:30:35.264543Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.23) initializing of server in progress as process 14204
2021-04-19T15:30:35.272368Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-04-19T15:30:36.555307Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-04-19T15:30:38.946927Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: _SqfBHRY)7gh
这里请记下初始密码,后续设置会用到
A temporary password is generated for root@localhost: _SqfBHRY)7gh
16.启动mysql服务
[root@VM-1-9-centos mysql]# service mysql start
Starting MySQL SUCCESS!
17.进入mysql进行设置
[root@VM-1-9-centos mysql]# mysql -u root -p
Enter password: #输入刚刚的密码 _SqfBHRY)7gh
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23
Copyright (c) 2000, 2021, 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>
18.修改 root 的密码
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.02 sec)
19.创建新的账户并授权
mysql> create database test;
Query OK, 1 row affected (0.01 sec)
mysql> create user test identified by '123456';
Query OK, 0 rows affected (0.02 sec)
mysql> grant all privileges ON test.* TO 'test'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges; #刷新后才生效
Query OK, 0 rows affected (0.01 sec)
20.防火墙开放3306端口
[root@VM-1-9-centos logs]# systemctl start firewalld.service #启动防火墙
[root@VM-1-9-centos logs]# firewall-cmd --zone=public --add-port=3306/tcp --permanent #开发3306端口
success
[root@VM-1-9-centos logs]# systemctl restart firewalld.service #重启防火墙
[root@VM-1-9-centos logs]# firewall-cmd --list-ports
3306/tcp
21.Navicat连接测试
至此,mysql8.0.23搭建成功。