centos系统安装MySQL8


使用yum仓库安装MySQL8

1.查看centos系统版本

# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

2.下载并安装repo仓库文件

下载地址:https://dev.mysql.com/downloads/repo/yum/


# wget https://repo.mysql.com//mysql80-community-release-el7-5.noarch.rpm

# yum localinstall -y mysql80-community-release-el7-5.noarch.rpm 
已加载插件:fastestmirror
正在检查 mysql80-community-release-el7-5.noarch.rpm: mysql80-community-release-el7-5.noarch
mysql80-community-release-el7-5.noarch.rpm 将被安装
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql80-community-release.noarch.0.el7-5 将被 安装
--> 解决依赖关系完成

依赖关系解决

================================================================================================
 Package                     架构     版本      源                                         大小
================================================================================================
正在安装:
 mysql80-community-release   noarch   el7-5     /mysql80-community-release-el7-5.noarch   9.1 k

事务概要
================================================================================================
安装  1 软件包

总计:9.1 k
安装大小:9.1 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mysql80-community-release-el7-5.noarch                                      1/1 
  验证中      : mysql80-community-release-el7-5.noarch                                      1/1 

已安装:
  mysql80-community-release.noarch 0:el7-5                                                      

完毕!

3.验证仓库软件


# yum makecache

# yum repolist all|grep mysql8
mysql80-community/x86_64            MySQL 8.0 Community Server      启用:    321
mysql80-community-source            MySQL 8.0 Community Server - So 禁用

# yum repolist enabled | grep "mysql*"            
mysql-connectors-community/x86_64       MySQL Connectors Community           230
mysql-tools-community/x86_64            MySQL Tools Community                138
mysql80-community/x86_64                MySQL 8.0 Community Server           321

4.安装MySQL8

说明:此时仓库中只有一个MySQL版本,因此可以直接安装

# yum install -y mysql-community-server

# 使用以下方式检查安装包
# rpm -qa|grep -i "mysql.*8.*"
mysql80-community-release-el7-5.noarch
mysql-community-client-plugins-8.0.28-1.el7.x86_64
mysql-community-client-8.0.28-1.el7.x86_64
mysql-community-libs-compat-8.0.28-1.el7.x86_64
mysql-community-common-8.0.28-1.el7.x86_64
mysql-community-libs-8.0.28-1.el7.x86_64
mysql-community-icu-data-files-8.0.28-1.el7.x86_64
mysql-community-server-8.0.28-1.el7.x86_64

默认配置文件内容

# cat /etc/my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

查看默认密码

yum方式安装的MySQL8卸载

使用rpm文件安装MySQL8

下载rpm安装软件

地址:https://dev.mysql.com/downloads/mysql/

使用通用二进制文件安装MySQL8

下载地址:https://dev.mysql.com/downloads/mysql/

1.安装依赖
mysql依赖libaio,如果未在本地安装,则数据目录初始化和后续的服务启动将失败

# 默认是已安装的

[root@localhost ~]# rpm -qa|grep libaio
libaio-0.3.109-13.el7.x86_64

# yum -y install libaio # 若不存在则安装

2.下载二进制安装文件

# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz

3.安装

# 添加mysql组和mysql用户,所有的文件和目录应该在mysql用户下
# groupadd mysql
# useradd -r -g mysql -s /bin/false mysql

# 设置安装位置为/usr/local,可以修改为其他位置
# tar -xvf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/

# 添加软链接,以后升级版本,只需要解压到相同的位置,更改符号链接就行了
# cd /usr/local/
# ll
总用量 0
drwxr-xr-x. 2 root root   6 4月  11 2018 bin
drwxr-xr-x. 2 root root   6 4月  11 2018 etc
drwxr-xr-x. 2 root root   6 4月  11 2018 games
drwxr-xr-x. 2 root root   6 4月  11 2018 include
drwxr-xr-x. 2 root root   6 4月  11 2018 lib
drwxr-xr-x. 2 root root   6 4月  11 2018 lib64
drwxr-xr-x. 2 root root   6 4月  11 2018 libexec
drwxr-xr-x  9 root root 129 3月  30 13:52 mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 4月  11 2018 sbin
drwxr-xr-x. 5 root root  49 4月   9 2021 share
drwxr-xr-x. 2 root root  56 3月  30 13:47 src

# ln -s mysql-8.0.28-linux-glibc2.12-x86_64 mysql

# ll
总用量 0
drwxr-xr-x. 2 root root   6 4月  11 2018 bin
drwxr-xr-x. 2 root root   6 4月  11 2018 etc
drwxr-xr-x. 2 root root   6 4月  11 2018 games
drwxr-xr-x. 2 root root   6 4月  11 2018 include
drwxr-xr-x. 2 root root   6 4月  11 2018 lib
drwxr-xr-x. 2 root root   6 4月  11 2018 lib64
drwxr-xr-x. 2 root root   6 4月  11 2018 libexec
lrwxrwxrwx  1 root root  35 3月  30 13:53 mysql -> mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x  9 root root 129 3月  30 13:52 mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 4月  11 2018 sbin
drwxr-xr-x. 5 root root  49 4月   9 2021 share
drwxr-xr-x. 2 root root  56 3月  30 13:47 src

# 创建必要的目录并将所有权更改为mysql

# cd mysql
# ll
总用量 284
drwxr-xr-x  2 7161 31415   4096 12月 18 02:28 bin
drwxr-xr-x  2 7161 31415     55 12月 18 02:28 docs
drwxr-xr-x  3 7161 31415    282 12月 18 02:28 include
drwxr-xr-x  6 7161 31415    201 12月 18 02:28 lib
-rw-r--r--  1 7161 31415 276595 12月 18 00:07 LICENSE
drwxr-xr-x  4 7161 31415     30 12月 18 02:28 man
-rw-r--r--  1 7161 31415    666 12月 18 00:07 README
drwxr-xr-x 28 7161 31415   4096 12月 18 02:28 share
drwxr-xr-x  2 7161 31415     77 12月 18 02:28 support-files

[root@localhost mysql]# mkdir data
[root@localhost mysql]# mkdir mysql-files
[root@localhost mysql]# chmod 750 mysql-files
[root@localhost mysql]# chmod 750 data
[root@localhost mysql]# chown -R mysql .
[root@localhost mysql]# chgrp -R mysql .
[root@localhost mysql]# ll
总用量 284
drwxr-xr-x  2 mysql mysql   4096 12月 18 02:28 bin
drwxr-x---  2 mysql mysql      6 3月  30 02:28 data
drwxr-xr-x  2 mysql mysql     55 12月 18 02:28 docs
drwxr-xr-x  3 mysql mysql    282 12月 18 02:28 include
drwxr-xr-x  6 mysql mysql    201 12月 18 02:28 lib
-rw-r--r--  1 mysql mysql 276595 12月 18 00:07 LICENSE
drwxr-xr-x  4 mysql mysql     30 12月 18 02:28 man
drwxr-x---  2 mysql mysql      6 3月  30 13:57 mysql-files
-rw-r--r--  1 mysql mysql    666 12月 18 00:07 README
drwxr-xr-x 28 mysql mysql   4096 12月 18 02:28 share
drwxr-xr-x  2 mysql mysql     77 12月 18 02:28 support-files

# 清除mariadb有关文件
# rpm -qa | grep "mariadb"
mariadb-libs-5.5.68-1.el7.x86_64

# rm -rf /etc/my.cnf
# rm -rf /etc/my.cnf.d/

# 初始化MySQL,会生成一个临时密码
# 此时data目录下会有初始化后的数据
[root@localhost mysql]# bin/mysqld --initialize --user mysql
2022-03-30T06:28:50.697879Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.28-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 2346
2022-03-30T06:28:50.704665Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-03-30T06:28:51.016806Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-03-30T06:28:51.942504Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ,ai:FtMdM8tX

# 设置SSL的RSA,此时会在data存储目录下生成有关文件
[root@localhost mysql]# bin/mysql_ssl_rsa_setup 

[root@localhost data]# pwd
/usr/local/mysql/data
[root@localhost data]# ll

-rw------- 1 mysql mysql     1680 3月  30 14:53 ca-key.pem
-rw-r--r-- 1 mysql mysql     1112 3月  30 14:53 ca.pem
-rw-r--r-- 1 mysql mysql     1112 3月  30 14:53 client-cert.pem
-rw------- 1 mysql mysql     1680 3月  30 14:53 client-key.pem
-rw------- 1 mysql mysql     1680 3月  30 14:53 private_key.pem
-rw-r--r-- 1 mysql mysql      452 3月  30 14:53 public_key.pem
-rw-r--r-- 1 mysql mysql     1112 3月  30 14:53 server-cert.pem
-rw------- 1 mysql mysql     1676 3月  30 14:53 server-key.pem

# 将二进制文件的所有权更改为root,数据文件的所有权更改为mysql
[root@localhost mysql]# chown -R root .
[root@localhost mysql]# chown -R mysql data
[root@localhost mysql]# chown -R mysql mysql-files
[root@localhost mysql]# ll
总用量 284
总用量 288
drwxr-xr-x  2 root  mysql   4096 12月 18 02:28 bin
drwxr-x---  6 mysql mysql   4096 3月  30 14:53 data
drwxr-xr-x  2 root  mysql     55 12月 18 02:28 docs
drwxr-xr-x  3 root  mysql    282 12月 18 02:28 include
drwxr-xr-x  6 root  mysql    201 12月 18 02:28 lib
-rw-r--r--  1 root  mysql 276595 12月 18 00:07 LICENSE
drwxr-xr-x  4 root  mysql     30 12月 18 02:28 man
drwxr-x---  2 mysql mysql      6 3月  30 14:52 mysql-files
-rw-r--r--  1 root  mysql    666 12月 18 00:07 README
drwxr-xr-x 28 root  mysql   4096 12月 18 02:28 share
drwxr-xr-x  2 root  mysql     77 12月 18 02:28 support-files

# 将启动脚本复制到init.d中
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld

# 将mysql的二进制文件到处到PATH环境变量中
# 临时办法,重启就不能用了
[root@localhost mysql]# export PATH=$PATH:/usr/local/mysql/bin

# 永久办法
[root@localhost mysql]# cat >> /etc/profile.d/mysql.sh << "EOF"
#!/bin/bash

export PATH=$PATH:/usr/local/mysql/bin
EOF

source /etc/profile.d/mysql.sh

安装完毕后将在/usr/local/mysql中获得下图中列出的目录

启动MySQL8

默认没有/etc/my.cnf配置文件

[root@localhost mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
. SUCCESS! 
# 使用生成的临时密码登录,并为root用户设置自定义密码
# mysql -hlocalhost -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28

Copyright (c) 2000, 2022, 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;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPass5!';
Query OK, 0 rows affected (0.01 sec)

mysql> exit;
Bye

# 退出MySQL,并重启MySQL就能使用新密码登录了
[root@localhost mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

设置开机启动

chkconfig --add mysqld
chkconfig mysqld on
chkconfig mysql --list

日常运行用service或者/etc/init.d/mysqld都可以

[root@localhost mysql]# service mysqld status
 ERROR! MySQL is not running
[root@localhost mysql]# service mysqld start
Starting MySQL. SUCCESS! 
[root@localhost mysql]# service mysqld status
 SUCCESS! MySQL running (1908)
[root@localhost mysql]# service mysqld stop
Shutting down MySQL. SUCCESS! 
[root@localhost mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 
[root@localhost mysql]# service mysqld status
 SUCCESS! MySQL running (2083)


二进制安装方式卸载