Bootstrap

Linux安装mysql8.0


1: 在 /usr/local下 创建mysql文件夹:

mkdir /usr/local/mysql

2: 切换到mysql文件夹下:

cd mysql

3: 下载mysql:

wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
或者163镜像源下载: https://mirrors.163.com/mysql/Downloads/MySQL-8.0/mysql-8.0.27-el7-x86_64.tar.gz

4: 解压mysql:

tar -zxvf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

5: 重命名文件夹:

mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql-8.0

6: 进入mysql-8.0文件夹:

cd mysql-8.0

7: 创建data文件夹 存储文件

mkdir data 

8: 创建用户组以及用户和密码

groupadd mysql
useradd -g mysql mysql

9: 授权用户:

chown -R mysql.mysql /usr/local/mysql/mysql-8.0

10: 切换到bin目录下:

cd bin

11: 初始化基础信息(并获得密码):

./mysqld --user=mysql --basedir=/usr/local/mysql/mysql-8.0 --datadir=/usr/local/mysql/mysql-8.0/data/ --initialize
如果报错提示:
    ./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
安装libaio:
    yum install -y libaio
再次执行初始化

12: 编辑my.cnf文件:

vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql/mysql-8.0/
datadir=/usr/local/mysql/mysql-8.0/data/
socket=/tmp/mysql.sock
character-set-server=UTF8MB4

13: 添加mysqld服务到系统:

cp -a ../support-files/mysql.server /etc/init.d/mysql

14: 授权以及添加服务

 chmod +x /etc/init.d/mysql

 chkconfig --add mysql

15: 启动mysql

service mysql start

可能会启动失败: mysqld_safe error: log-error set to ‘/var/log/mariadb/mariadb.log’

整体my.cnf的配置没有什么问题,然后一直启动失败,最后发现是mysqld_safe 配置的日志目录并没有创建

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

所以直接创建授权

说明:我这里配置的log-error的路径是在/var/log/mariadb 底下,创建的路径根据自己的实际路径来配置,注意一定也别忘了创建mariadb.log,要不然还是会报错

mkdir /var/log/mariadb     
touch /var/log/mariadb/mariadb.log

mysql 用户和用户对目录进行授权

chown -R mysql:mysql /var/log/mariadb/

再次启动mysql服务

16: 查看启动状态

service mysql status

17: 将mysql命令添加到服务

ln -s /usr/local/mysql/mysql-8.0/bin/mysql /usr/bin

18: 登录mysql

mysql -uroot -p 

回车后输入第11步骤生成的随机密码

本人用腾讯云 centos8 复现评论区错误:(原文大佬环境错误)

mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory

执行下面操作即可:

cp /lib64/libtinfo.so.6 /lib64/libtinfo.so.5

19: 修改root密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

其中123456是新的密码自己设置

20: 使密码生效

 flush privileges;

21: 选择mysql数据库

use mysql;

22: 修改远程连接并生效

update user set host='%' where user='root';

flush privileges;
;