重学Msql之Centos7上安装Mysql8

  • Post author:
  • Post category:mysql



目录


查询当前是centos还是ubuntu


禁用 SELinux


查询centos上是否安装了Mysql


下载 MySQL 8 Repo for CentOS 7


安裝 MySQL 8 和 MySQL Client for CentOS 7


启用和启动 MySQL 8 服务


错误一:Unsupported redo log format (0). The redo log was created before MySQL 5.7.9


错误二:Cannot upgrade server earlier than 5.7 to 8.0


获取MySQL 8根密码


使用默认的密码登录


更改默认的MYSQL8密码策略


方法一:修改默认值


方法二:卸载该策略How do I turn off the mysql password validation? – Stack Overflow


更改默认的MySQL 8根密码


远程连接Mysql前的工作


mysql8.0添加用户


拓展阅读:使用 systemd 配置多个 MySQL 实例



查询当前是centos还是ubuntu


Different ways you can check the CentOS or Ubuntu Linux OS version (how2shout.com)

cat /etc/os-release

如果是ubuntu,显示如下

$ cat /etc/os-release

NAME=”Ubuntu”

VERSION=”16.04.5 LTS (Xenial Xerus)”

ID=ubuntu

ID_LIKE=debian

PRETTY_NAME=”Ubuntu 16.04.5 LTS”

VERSION_ID=”16.04″

HOME_URL=”http://www.ubuntu.com/”

SUPPORT_URL=”http://help.ubuntu.com/”

BUG_REPORT_URL=”http://bugs.launchpad.net/ubuntu/”

VERSION_CODENAME=xenial

UBUNTU_CODENAME=xenial

如果是centos,显示如下

[root@localhost ~]# cat /etc/os-release

NAME=”CentOS Linux”

VERSION=”7 (Core)”

ID=”centos”

ID_LIKE=”rhel fedora”

VERSION_ID=”7″

PRETTY_NAME=”CentOS Linux 7 (Core)”

ANSI_COLOR=”0;31″

CPE_NAME=”cpe:/o:centos:centos:7″

HOME_URL=”https://www.centos.org/”

BUG_REPORT_URL=”https://bugs.centos.org/”

CENTOS_MANTISBT_PROJECT=”CentOS-7″

CENTOS_MANTISBT_PROJECT_VERSION=”7″

REDHAT_SUPPORT_PRODUCT=”centos”

REDHAT_SUPPORT_PRODUCT_VERSION=”7″

此处,查询结果为centos 7

禁用 SELinux

在我们的第一步中,我们必须禁用SELinux,我们必须提到这个特定的步骤

不是强制性

的,但是为了避免在权限方面出现一些可能的问题,我们将首先禁用它。如果您的服务器分配了公共IP,并且可以从Internet访问端口3306,那么出于安全原因,请保持SELinux启用状态,或者至少

在MySQL安装完成后重新启用它

sudo vim /etc/selinux/config

最终配置文件设置应如下所示

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

查询centos上是否安装了Mysql


如何在 CentOS | 上列出已安装的软件包Linuxize

sudo yum list installed | grep mysql

查询是否安装了

同时看看/var/lib下是否有mysql目录

ls /var/lib | grep mysql

如果存在,那么请参考如何从低版本升级到高版本


How to upgrade from MySQL 5.7 to MySQL 8.0 successfully (minervadb.com)

否则,你的服务器应该是新服务器,请按照下面的步骤安装mysql

下载 MySQL 8 Repo for CentOS 7

yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

安裝 MySQL 8 和 MySQL Client for CentOS 7

yum install -y mysql mysql-server

错误信息一:

或者

解决方法workaround


MySQL GPG keys expired, preventing installation/upgrade of MySQL packages from the official repository. – cPanel

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

ubuntu

wget -q -O - https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 | apt-key add -

再次安装,成功

再次查询安装了的mysql

sudo yum list installed | grep mysql

启用和启动 MySQL 8 服务

systemctl status mysqld

如果成功的输出如下:

上面的输出证实了我们的MySQL 8安装成功的事实,但也表明我们的服务当前处于非活动状态(),这意味着MySQL服务尚未启动,没有什么可担心的,因为我们将很快启动此服务。

在启动服务之前,让我们确保每次重新启动服务器时,首先启用我们的服务以自动启动,请使用以下命令才能使MySQL服务自动启动:

systemctl enable mysqld

最后,我们现在可以通过在终端窗口中运行下一个CLI命令来启动MySQL服务:

systemctl start mysqld

启动出错:

systemctl start mysqld

Job for mysqld.service failed because the control process exited with error code. See “systemctl status mysqld.service” and “journalctl -xe” for details.

执行systemctl status mysqld.service

systemctl status mysqld

● mysqld.service – MySQL Server

Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

Active:


failed


(Result: exit-code) since 一 2022-02-14 14:35:32 CST; 16s ago

Docs: man:mysqld(8)

http://dev.mysql.com/doc/refman/en/using-systemd.html

Process: 23005 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS


(code=exited, status=1/FAILURE)



Process: 22973 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

Main PID: 23005 (code=exited, status=1/FAILURE)

Status: “Data Dictionary upgrade from MySQL 5.7 in progress”

2月 14 14:35:25 localhost.localdomain systemd[1]: Starting MySQL Server…

2月 14 14:35:32 localhost.localdomain systemd[1]:

mysqld.service: main process exited, code=exited, status=1/FAILURE


2月 14 14:35:32 localhost.localdomain systemd[1]:


Failed to start MySQL Server.



2月 14 14:35:32 localhost.localdomain systemd[1]:

Unit mysqld.service entered failed state.


2月 14 14:35:32 localhost.localdomain systemd[1]:

mysqld.service failed.

执行 journalctl -xe,找到重启时时间的记录,如下

2月 14 14:48:05 localhost.localdomain polkitd[1566]:

Registered Authentication Agent for unix-process:29476:206717142 (system bus name :1.83916 [/usr/bin/pkttyagent –notify-fd 5 –fallback], object path /org/freede


2月 14 14:48:05 localhost.localdomain systemd[1]: Starting MySQL Server…

— Subject: Unit mysqld.service has begun start-up

— Defined-By: systemd

— Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel



— Unit mysqld.service has begun starting up.

2月 14 14:48:12 localhost.localdomain systemd[1]:

mysqld.service: main process exited, code=exited, status=1/FAILURE


2月 14 14:48:12 localhost.localdomain systemd[1]:


Failed to start MySQL Server.



— Subject: Unit mysqld.service has failed

— Defined-By: systemd

— Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel



— Unit mysqld.service has failed.



— The result is failed.

2月 14 14:48:12 localhost.localdomain systemd[1]:

Unit mysqld.service entered failed state.


2月 14 14:48:12 localhost.localdomain systemd[1]:

mysqld.service failed.


2月 14 14:48:12 localhost.localdomain polkitd[1566]:

Unregistered Authentication Agent for unix-process:29476:206717142 (system bus name :1.83916, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale

查看log日志

tail -20 /var/log/mysqld.log

错误一:Unsupported redo log format (0). The redo log was created before MySQL 5.7.9


参考:关于[InnoDB] Unsupported redo log format (0). The redo log was created before MySQL 5.7.9的解决办法_kwame211的博客-CSDN博客

2022-02-14T06:05:20.989585Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.28) starting as process 7663

2022-02-14T06:05:21.062358Z 1 [System] [MY-011012] [Server] Starting upgrade of data directory.

2022-02-14T06:05:21.062524Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.


2022-02-14T06:05:21.199268Z 1 [ERROR] [MY-013090] [InnoDB] Unsupported redo log format (0). The redo log was created before MySQL 5.7.9


2022-02-14T06:05:21.199316Z 1 [ERROR] [MY-012930] [InnoDB] Plugin initialization aborted with error Generic error.

2022-02-14T06:05:21.604390Z 1 [ERROR] [MY-011013] [Server] Failed to initialize DD Storage Engine.

2022-02-14T06:05:21.604694Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.

2022-02-14T06:05:21.604834Z 0 [ERROR] [MY-010119] [Server] Aborting

2022-02-14T06:05:21.605885Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.28)  MySQL Community Server – GPL.

解决方法

切换到mysql目录

cd /var/lib/mysql/

备份

cp -p ib_logfile0 ib_logfile0_bak
cp -p ib_logfile1 ib_logfile1_bak

删除日志文件(如果您需要数据库中的表数据,请在备份数据库之后再这样操做)

rm ib_logfile0 ib_logfile1

再次启动mysql并查看日志,此时就应该看不到该问题了,但是我们重启失败的问题依然存在

systemctl status mysqld
tail -20  /var/log/mysqld.log

再次查看log日志

tail -20 /var/log/mysqld.log

2022-02-14T07:50:30.717031Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.28) starting as process 29205

2022-02-14T07:50:30.727812Z 1 [System] [MY-011012] [Server] Starting upgrade of data directory.

2022-02-14T07:50:30.727974Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.


2022-02-14T07:50:31.609199Z 1 [ERROR] [MY-013168] [InnoDB] Cannot upgrade server earlier than 5.7 to 8.0


2022-02-14T07:50:36.615562Z 1 [ERROR] [MY-011013] [Server] Failed to initialize DD Storage Engine.

2022-02-14T07:50:36.616111Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.

2022-02-14T07:50:36.616632Z 0 [ERROR] [MY-010119] [Server] Aborting

2022-02-14T07:50:36.619156Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.28)  MySQL Community Server – GPL.

错误二:Cannot upgrade server earlier than 5.7 to 8.0


mysql – 无法在 centos 中将 5.7 之前的服务器升级到 8.0。服务器启动失败 – 堆栈溢出 (stackoverflow.com)

这个问题应该是以前安装过mysql(奇怪的是在安装之前执行sudo yum list installed | grep mysql时并没有发现有任何mysql版本)

移动原mysql到备份文件,重启mysql后会生成新的mysql目录


(所以执行下面这一步要小心,如果你的mysql里面有重要数据那么尽量不要这样操作。)

mv /var/lib/mysql /var/lib/mysql5.7_bak

再次执行重启mysql,成功了

systemctl status mysqld

/var/lib下有两个MySQL目录

获取MySQL 8根密码

默认情况下,安装MySQL时,将自动为root用户提供密码,以便我们可以访问所有默认数据库。这个自动生成的MySQL密码可以在默认的MySQL日志文件上找到

cat /var/log/mysqld.log | grep 'password is'

使用默认的密码登录

mysql -u root -p

更改默认的MYSQL8密码策略

在MYSQL8安装时,默认启用了Password Validation Component System Variables(密码验证组件系统变量),该变量将会校验修改的值是否符合策略。

关于该部分的详细内容请见:

重学Mysql之Mysql8.0修改密码策略_Rocket MAN的博客-CSDN博客

默认值如下:

mysql> SHOW VARIABLES LIKE ‘validate_password%’;

+—————————————————–+————-+

| Variable_name                                        | Value       |

+—————————————————–+————-+

| validate_password.check_user_name    | ON          |

| validate_password.dictionary_file           |                 |

| validate_password.length                       | 8              |

| validate_password.mixed_case_count   | 1              |

| validate_password.number_count          | 1              |

| validate_password.policy                        | MEDIUM |

| validate_password.special_char_count  | 1              |

+—————————————————–+————-+

7 rows in set (0.01 sec)

————————————————

有两种方式修改该策略

方法一:修改默认值

首先修改check_user_name,如果这个值为ON,那么账号和密码就不能相同

set global validate_password.check_user_name=OFF

再修改policy,该策略为LOW时,只校验密码长度,不会校验大小写、特殊字符等

set global validate_password.policy=LOW

如果对密码长度有要去,可以修改validate_password.length,该值有最小长度要求,其公式为

validate_password.number_count

+ validate_password.special_char_count

+ (2 * validate_password.mixed_case_count)

因此,密码的默认最低长度为4位,如果需要修改到更小,需要先修改上面三个参数再修改length

set global validate_password.length=4

方法二:卸载该策略

How do I turn off the mysql password validation? – Stack Overflow

uninstall plugin validate_password;


如果上面一个不生效(新mysql版本),执行

UNINSTALL COMPONENT 'file://component_validate_password';

更改默认的MySQL 8根密码

在本教程的最后一步中,我们将了解如何更改默认的MySQL密码。如果不更改MySQL提供的默认密码,我们将无法执行任何SQL查询。

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';

FLUSH PRIVILEGES;

远程连接Mysql前的工作

远程连接时,会提示连接错误,一般需要关闭防火墙或者增加Mysql端口

firewall-cmd --zone=public --add-port=YOURPORT/tcp --permanent 

对应的,如果想要删除端口

firewall-cmd --zone=public --remove-port=YOURPORT/tcp --permanent

如果还是提示错误,说明MYSQL设置了仅本地连接 ,查看当前账号可用的IP,可以看到可用IP仅仅是本地可连接

mysql> SELECT host FROM mysql.user WHERE user = 'root';
+-----------+
| host      |
+-----------+
| localhost |
+-----------+

执行修改并刷新

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

再次远程访问,就成功了。

mysql8.0添加用户

请参考之前的文章:

Mysql 8.0创建用户并授权_Rocket MAN的博客-CSDN博客_mysql8创建用户

拓展阅读:



使用 systemd 配置多个 MySQL 实例




MySQL :: MySQL 8.0 参考手册 :: 2.5.9 使用 systemd 管理 MySQL 服务器

本文参考:


在 CentOS 7 上安裝 MySQL 8 (tufora.com)


How to upgrade from MySQL 5.7 to MySQL 8.0 successfully (minervadb.com)


关于[InnoDB] Unsupported redo log format (0). The redo log was created before MySQL 5.7.9的解决办法_kwame211的博客-CSDN博客


mysql – 无法在 centos 中将 5.7 之前的服务器升级到 8.0。服务器启动失败 – 堆栈溢出 (stackoverflow.com)


MySQL :: MySQL 8.0 参考手册 :: 2.5.9 使用 systemd 管理 MySQL 服务器