在 Centos 上第一次运行 Mysql 时访问被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30334914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:01:40  来源:igfitidea点击:

Access denied while running Mysql for the first time on Centos

mysqlcentosroot

提问by Animesh Pandey

I just installed Mysql for the first time on a CentOS machine using yum. The installation had no errors. Then I followed those steps:

我第一次在 CentOS 机器上安装了 Mysql,使用yum. 安装没有错误。然后我按照以下步骤操作:

$ sudo /sbin/service mysqld start --skip-grant-tables --skip-networking

$ sudo /usr/bin/mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
Enter current password for root (enter for none): 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
Enter current password for root (enter for none): 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
Enter current password for root (enter for none): 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
Enter current password for root (enter for none): 

It can be seen that even after first installation there is issue in logging in to the DB. I have even tried all command in sudo, but the error is still there. I can't even figure out how to reconfigure MySQL. I halso have installed-removed mysql 3 times.

可以看出,即使在第一次安装后,登录数据库也存在问题。我什至尝试了所有命令sudo,但错误仍然存​​在。我什至不知道如何重新配置​​ MySQL。我也已经安装-删除了 3 次 mysql。

How can I solve this issue?

我该如何解决这个问题?

采纳答案by Fernando Garcia

Maybe you already setted it at some point. You can try this:

也许你已经在某个时候设置了它。你可以试试这个:

yum remove mysql-server
rm -rf /var/lib/mysql
yum install mysql-server
systemctl start mysqld.service
/usr/bin/mysql_secure_installation

Anyway, I think this question should be in ServerFault.

无论如何,我认为这个问题应该在ServerFault中。

回答by SubRed

Just for anyone who has this issue on MySQL 5.7. or higher.

仅适用于在 MySQL 5.7 上遇到此问题的任何人。或更高。

MySQL v 5.7 or higher generates a temporary random password after installation and stored that in mysql error log file, located at /var/log/mysqld.logfor an installation by the MySQL Yum repository on CentOS 7.

MySQL v 5.7 或更高版本在安装后会生成一个临时随机密码,并将其存储在 mysql 错误日志文件中,该文件位于/var/log/mysqld.logCentOS 7 上 MySQL Yum 存储库的安装位置。

use below command to see the password:

使用以下命令查看密码:

sudo grep 'temporary password' /var/log/mysqld.log

Ref: MySQL 5.7.7 - Centos 7 el7 - Access denied

参考:MySQL 5.7.7 - Centos 7 el7 - 拒绝访问

EDIT1

编辑1

For anyone that has different error log file, you can find it using @Drew's answer herebelow.

对于任何有不同错误日志文件的人,您可以下面使用@Drew 的回答找到它。

回答by Drew

A visual of the temporary root password written to the Error Log file

写入错误日志文件的临时 root 密码的可视化

enter image description here

在此处输入图片说明

Not that one can exactly rifle off the command

并不是说一个人可以完全取消命令

select @@log_error;

to find the location of the Error Log file without logging in (a which came first, the chicken or the egg predicament).

在不登录的情况下找到错误日志文件的位置(先有鸡还是先有蛋的困境)。

But the typical locations are

但典型的位置是

C:\ProgramData\MySQL\MySQL Server 5.7\Data\

(for Windows. You must unhide the C:\ProgramData dir)

(对于 Windows。您必须取消隐藏 C:\ProgramData 目录)

and

mysql> select @@log_error;
+---------------------+
| @@log_error         |
+---------------------+
| /var/log/mysqld.log |
+---------------------+

(for Linux)

(适用于 Linux)

回答by Ivan

you misunderstood. Because of you havn't set the password yet. you should just enter for none.

你误会了。因为你还没有设置密码。你应该只输入无。

In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.

为了登录 MariaDB 以保护它,我们需要 root 用户的当前密码。如果您刚刚安装了 MariaDB,并且尚未设置 root 密码,则密码将为空白,因此您只需在此处按 Enter。

回答by none

  1. sudo mysql_secure_installation
  2. When it asks for password type pass
  3. Follow the prompts to change the root password
  1. sudo mysql_secure_installation
  2. 当它要求输入密码时 pass
  3. 按照提示修改root密码