#Linux 版本信息
Linux version 4.18.0-240.el8.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)) #1 SMP Fri Sep 25 19:48:47 UTC 2020
废话不多说,直接上命令了
当前是root用户操作
cd /home
ll
mkdir software
cd software/
# 下载安装包
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
cd /home
ll
#添加用户及用户组
useradd mysql8
passwd mysql8
cd /home/software
ll
#将下载好的软件包 挪至指定目录
cp mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz /usr/local/
cd /usr/local/
ll
#解压文件包
tar xvJf mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
ll
#个人代码洁癖,同一个目录下不允许有多余文件
rm -rf mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
#修改文件名称
mv mysql-8.0.21-linux-glibc2.12-x86_64 mysql-8.0.21
ll
#相关目录授权
chown mysql8:mysql8 -R /usr/local/mysql-8.0.21/
接下来使用mysql8用户进行操作
su - mysql8
#mysql 用户操作
#创建数据目录
mkdir data
#配置用户变量
vi .bash_profile
#将以下内容添加到用户环境配置
PATH=$PATH:HOME/.local/bin:$HOME/bin
export PATH
export MYSQL_HOME=/usr/local/mysql-8.0.21
export PATH=$MYSQL_HOME/bin:$PATH
#编辑完配置后,记得重新加载下
source .bash_profile
#验证下环境变量使用配置生效
echo $MYSQL_HOME
#初始化mysql,初始化完成后,最后一行是 mysql root用户密码
#下面的这些路径如果与我的有差异,请自行调整
mysqld --user=mysql8 \
--basedir=/usr/local/mysql-8.0.21/ \
--datadir=/home/mysql8/data/ \
--lower-case-table-names=1 \
--initialize
这一步就开始容易报错了,如果出现这种错误
022-08-26T08:04:58.666690Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.21/bin/mysqld (mysqld 8.0.21) initializing of server in progress as process 9456
2022-08-26T08:04:58.668278Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2022-08-26T08:04:58.668309Z 0 [ERROR] [MY-013236] [Server] The designated data directory /home/mysql8/data/ is unusable. You can remove all files that the server added to it.
2022-08-26T08:04:58.668400Z 0 [ERROR] [MY-010119] [Server] Aborting
直接用mysql8进入data目录删除所有内容即可
[mysql8@192 ~]$ cd data/
[mysql8@192 data]$ ll
total 0
-rw-r-----. 1 mysql8 mysql8 0 Aug 26 00:56 binlog.index
[mysql8@192 data]$ rm -rf *
[mysql8@192 data]$ ll
继续重新初化始命令即可
[mysql8@192 ~]$ mysqld --user=mysql8 \
> --basedir=/usr/local/mysql-8.0.21/ \
> --datadir=/home/mysql8/data/ \
> --lower-case-table-names=1 \
> --initialize
2022-08-26T08:11:30.980549Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.21/bin/mysqld (mysqld 8.0.21) initializing of server in progress as process 9515
2022-08-26T08:11:30.988649Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-08-26T08:11:34.082461Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-08-26T08:11:36.709210Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: **odWM:oy7/e
切换 root用户,修改配置文件
su - root
cd /etc/
vi my.cnf
#将以下内容复制到my.cnf中,具体含义请自行百度,还有很多配置,暂不赘述
[mysqld]
basedir=/usr/local/mysql-8.0.21/
datadir=/home/mysql8/data/
socket=/home/mysql8/data/mysql.sock
character-set-server=UTF8MB4
lower-case-table-names=1
[client]
socket=/home/mysql8/data/mysql.sock
配置自启动、服务相关内容
#添加到自启动中
cp /usr/local/mysql-8.0.21/support-files/mysql.server /etc/init.d/mysql8
#可执行权限
chmod +x /etc/init.d/mysql8
#将服务名称 添加到启动进程中
chkconfig --add mysql8
切换至mysql8启动数据库服务
su - mysql8
[mysql8@192 ~]$ service mysql8 start
Starting MySQL........ SUCCESS!
至此数据库已经成功启动,但是还没法用!
#以为能直接连接数据,结果没法用,有报错
[mysql8@192 ~]$ mysql -uroot -p
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
如果你的mysql8用户可以直接执行 sudo 命令可以先跳过,若不可以,先按照我的操作来一遍。
切换root用户
vi /etc/sudoers
#先搜索关键字找到以下内容
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
#文件中添加这行
mysql8 ALL=(ALL) ALL
记得强制保存,因为这个文件好像默认是不可写的,保存完整后,记得删除写权限。
#删除文件写权限
chmod u-w /etc/sudoers
切换mysql8执行命令
[root@192 ~] su - mysql8
Last login: Fri Aug 26 02:28:57 PDT 2022 on pts/0
[mysql8@192 ~]$
[mysql8@192 ~]$
[mysql8@192 ~]$
[mysql8@192 ~]$ sudo ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5
现在即可登录了
[mysql8@192 ~]$ mysql -u root -p
Server version: 8.0.21
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> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.23 sec)
#刷新密码到内存中
mysql> flush privileges;
Query OK, 0 rows affected (0.08 sec)
quit;
#使用修改后的密码登录数据库,即可进行任意操作
mysql8@192 ~]$ mysql -u root -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.21 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.24 sec)
#后边内容请忽略,自己弄着玩
create database if not exists ruoyi;
create user 'ruoyi'@'%' identified by '********';
grant all privileges on ruoyi.* to 'ruoyi'@'%';
flush privileges;
至此mysql 8.0安装完成,如果按照此步骤出现问题,请及时留言。