Linux Centos8 Mysql启动(Active: failed (Result: exit-code))及登录报错(ERROR 1045 (28000): Access denied )

解决CentOS8环境下MySQL启动失败的问题,通过修改配置文件、重建数据库等步骤成功启动MySQL服务。
摘要由CSDN通过智能技术生成

 Linux Centos8 环境上启动mysql 启动报错如下:

[root@MaxwellDBA ~]# 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.
[root@MaxwellDBA etc]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2022-10-11 15:55:05 CST; 20min ago
  Process: 2439 ExecStopPost=/usr/libexec/mysql-wait-stop (code=exited, status=0/SUCCESS)
  Process: 2437 ExecStart=/usr/libexec/mysqld --basedir=/usr (code=exited, status=1/FAILURE)
  Process: 2398 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
  Process: 2372 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 2437 (code=exited, status=1/FAILURE)
   Status: "Server startup in progress"
    Error: 13 (Permission denied)

Oct 11 15:55:05 MaxwellDBA systemd[1]: Starting MySQL 8.0 database server...
Oct 11 15:55:05 MaxwellDBA mysql-check-socket[2372]: Socket file /var/lib/mysql/mysql.sock exists.
Oct 11 15:55:05 MaxwellDBA mysql-check-socket[2372]: No process is using /var/lib/mysql/mysql.sock, which means it is a garbage, so it will be removed automatically.
Oct 11 15:55:05 MaxwellDBA systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE
Oct 11 15:55:05 MaxwellDBA systemd[1]: mysqld.service: Failed with result 'exit-code'.
Oct 11 15:55:05 MaxwellDBA systemd[1]: Failed to start MySQL 8.0 database server.
[root@MaxwellDBA etc]# ^C

根据报错修改my.cnf文件:

添加以下两行到my.cnf文件中。

[mysqld]
explicit_defaults_for_timestamp=true
[root@MaxwellDBA etc]# cat my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
# socket=/var/lib/mysql/mysql.sock
[mysqld]
explicit_defaults_for_timestamp=true

启动服务器,若仍然报错, 则将 /var/lib/mysql/路径下的文件全部删除(等于删库重建,慎重操作

[root@MaxwellDBA etc]# rm -rf /var/lib/mysql/*
[root@MaxwellDBA etc]# ls -ltr /var/lib/mysql
total 0
[root@MaxwellDBA etc]# systemctl status mysqld  
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2022-10-11 15:55:05 CST; 26min ago
  Process: 2439 ExecStopPost=/usr/libexec/mysql-wait-stop (code=exited, status=0/SUCCESS)
  Process: 2437 ExecStart=/usr/libexec/mysqld --basedir=/usr (code=exited, status=1/FAILURE)
  Process: 2398 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
  Process: 2372 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 2437 (code=exited, status=1/FAILURE)
   Status: "Server startup in progress"
    Error: 13 (Permission denied)

Oct 11 15:55:05 MaxwellDBA systemd[1]: Starting MySQL 8.0 database server...
Oct 11 15:55:05 MaxwellDBA mysql-check-socket[2372]: Socket file /var/lib/mysql/mysql.sock exists.
Oct 11 15:55:05 MaxwellDBA mysql-check-socket[2372]: No process is using /var/lib/mysql/mysql.sock, which means it is a garbage, so it will be removed automatically.
Oct 11 15:55:05 MaxwellDBA systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE
Oct 11 15:55:05 MaxwellDBA systemd[1]: mysqld.service: Failed with result 'exit-code'.
Oct 11 15:55:05 MaxwellDBA systemd[1]: Failed to start MySQL 8.0 database server.
[root@MaxwellDBA etc]# systemctl start mysqld 
[root@MaxwellDBA etc]# systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-10-11 16:22:15 CST; 5s ago
  Process: 2439 ExecStopPost=/usr/libexec/mysql-wait-stop (code=exited, status=0/SUCCESS)
  Process: 3429 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 3302 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
  Process: 3277 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 3384 (mysqld)
   Status: "Server is operational"
    Tasks: 38 (limit: 22719)
   Memory: 489.0M
   CGroup: /system.slice/mysqld.service
           └─3384 /usr/libexec/mysqld --basedir=/usr

Oct 11 16:22:09 MaxwellDBA systemd[1]: Starting MySQL 8.0 database server...
Oct 11 16:22:09 MaxwellDBA mysql-prepare-db-dir[3302]: Initializing MySQL database
Oct 11 16:22:15 MaxwellDBA systemd[1]: Started MySQL 8.0 database server.
[root@MaxwellDBA etc]#

[root@MaxwellDBA log]# mysql -u root -p         
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

 Step 1: 打开文件 /etc/my.cnf

第一步:修改配置文件免密码登录mysql
1、进入文件:vi /etc/my.cnf
2、按i键表示可以编辑;添加skip-grant-tables;按esc键,输入:wq保存退出
3、重启mysql:systemctl restart mysqld

 

[root@MaxwellDBA log]# vim /etc/my.cnf
[root@MaxwellDBA log]# systemctl restart mysqld
root@MaxwellDBA log]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.26 Source distribution

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> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>

 第二步免密码登录mysql
1、登录:mysql -u root -p
2、提示输入密码按回车进入
3、进入数据库,输入:use mysql;
4、查看root用户信息:select host, user, authentication_string, plugin from user;
第二步免密码登录mysql
1、登录:mysql -u root -p
2、提示输入密码按回车进入
3、进入数据库,输入:use mysql;
4、查看root用户信息:select host, user, authentication_string, plugin from user; (当前密码设置默认为空字符串,无须进行第五步操作,若不是空,则需要进行第五步操作
5、更新root用户信息,把密码设置为空字符串:update user set authentication_string=’’ where user=‘root’;

[root@MaxwellDBA log]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.26 Source distribution

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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host      | user             | authentication_string                                                  | plugin                |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | root             |                                                                        | mysql_native_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye

第三步、退出mysql;注释掉/etc/my.cnf文件最后的 skip-grant-tables ;重启:systemctl restart mysqld

[root@MaxwellDBA log]# vim /etc/my.cnf         
[root@MaxwellDBA log]# systemctl restart mysqld

Step4:设置密码
1、重新开启一个客户端;
2、登录mysql(这时候还是不用输入密码,因为上面已经把密码设置为空字符串了);
3、修改root用户密码:
4、退出mysql后就可以用密码登录了
 

[root@MaxwellDBA log]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 Source distribution

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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> ALTER user'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye
[root@MaxwellDBA log]# mysql -u root -p        
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.26 Source distribution

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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值