考虑:

./mysqladmin -u root -p** '_redacted_'  

输出(包括输入密码):

输入密码: Mysqladmin: connect to server at 'localhost' failed错误 '用户'root'@'localhost'(使用密码:YES)拒绝访问'

我该如何解决这个问题?


打开并编辑/etc/my.cnf或/etc/mysql/my.cnf,具体取决于你的发行版。 在[mysqld]下添加skip-grant-tables 重新启动MySQL 你现在应该可以使用下面的命令MySQL -u root -p登录MySQL 运行mysql> flush特权; 设置新密码ALTER USER 'root'@'localhost' IDENTIFIED by 'NewPassword' 回到/etc/my.cnf,删除/comment skip-grant-tables 重新启动MySQL 现在你可以用新密码mysql -u root -p登录了

在你的MySQL Workbench中,你可以到左边的侧边栏,在Management下选择“Users and Privileges”,在User Accounts下点击root,在右边的部分点击tab“Account Limits”增加最大查询,更新等,然后点击tab“Administrative Roles”,勾选复选框赋予该帐户访问权限。

我这样做是为了在OS x中MySQL的初始设置中设置我的根密码。

sudo sh -c 'echo /usr/local/mysql/bin > /etc/paths.d/mysql'

关闭终端并打开一个新终端。

在Linux中,可以使用以下方法设置根密码。

sudo /usr/local/mysql/support-files/mysql.server stop
sudo mysqld_safe --skip-grant-tables

(sudo mysqld_safe——skip-grant-tables:第一次对我不起作用。但在第二次尝试时,成功了。)

然后登录MySQL:

mysql -u root

FLUSH PRIVILEGES;

现在修改密码:

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

重新启动MySQL:

sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start

我找到的所有解决方案都比必要的要复杂得多,没有一个适合我。这是解决我问题的办法。不需要重新启动mysqld或使用特殊权限启动它。

sudo mysql

-- for MySQL
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

-- for MariaDB
ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');

通过一个查询,我们将auth_plugin更改为mysql_native_password,并将根密码设置为root(可以在查询中随意更改)。

现在您应该可以使用root登录了。更多信息可以在MySQL文档或MariaDB文档中找到。

(使用Ctrl + D或输入Exit退出MySQL控制台。)

对于Ubuntu/Debian用户

(它可能适用于其他发行版,尤其是基于debian的发行版。)

运行以下命令以root身份连接(不需要任何密码)

sudo /usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf

如果你不想每次以根用户连接时都添加——defaults-file,你可以将/etc/mysql/debian.cnf复制到你的主目录:

sudo cp /etc/mysql/debian.cnf ~/.my.cnf

然后:

sudo mysql

我尝试了很多步骤来纠正这个问题。这个问题有很多可能的解决方案,很难从废话中筛选出有意义的东西。我终于在这里找到了一个很好的解决方案:

步骤1:确定数据库版本

mysql --version

使用MySQL,你会看到如下输出:

mysql  Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using  EditLine wrapper

MariaDB的输出如下:

mysql  Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1

请注意您正在运行的数据库和版本,因为稍后将使用它们。接下来,您需要停止数据库,以便手动访问它。

步骤2:停止数据库服务器

要修改root密码,必须先关闭数据库服务器。

你可以这样做MySQL:

sudo systemctl stop mysql

对于MariaDB来说:

sudo systemctl stop mariadb

步骤3:不进行权限检查,重新启动数据库服务器

如果您运行MySQL和MariaDB而不加载用户权限信息,它将允许您使用根权限访问数据库命令行,而无需提供密码。这将允许您在不知情的情况下访问数据库。

为此,您需要停止数据库加载授权表,授权表存储用户特权信息。因为这有一点安全风险,所以您也应该跳过网络连接,以防止其他客户端连接。

启动数据库而不加载授权表或启用网络:

sudo mysqld_safe --skip-grant-tables --skip-networking &

此命令末尾的&号将使此进程在后台运行,以便您可以继续使用您的终端。

现在您可以作为根用户连接到数据库,不需要输入密码。

mysql -u root

您将立即看到一个数据库shell提示符。

MySQL提示

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

MariaDB 提示符

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

现在您拥有了根访问权限,您可以更改根密码。

步骤4:修改root用户密码

mysql> FLUSH PRIVILEGES;

现在我们可以修改根密码了。

对于MySQL 5.7.6及更新版本,以及MariaDB 10.1.20及更新版本,使用以下命令:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

对于MySQL 5.7.5及以上版本,以及MariaDB 10.1.20及以上版本,使用:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');

确保用您选择的新密码替换new_password。

注意:如果ALTER USER命令不起作用,通常表明有更大的问题。但是,您可以尝试UPDATE…SET重置根密码。

[重要]这句话解决了我的问题:

mysql> UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost';

记住在此之后重新加载授权表。

在这两种情况下,您都应该看到命令已成功执行的确认。

Query OK, 0 rows affected (0.00 sec)

密码已经更改,因此现在可以停止数据库服务器的手动实例,并像以前一样重新启动它。

步骤5:正常重启数据库服务器

本教程介绍了一些重启数据库的进一步步骤,但我使用的唯一部分是:

对于MySQL,使用:

sudo systemctl start mysql

对于MariaDB,使用:

sudo systemctl start mariadb

现在,您可以通过运行以下命令确认新密码已正确应用:

mysql -u root -p

该命令现在应该提示输入新分配的密码。输入它,您应该可以像预期的那样访问数据库提示符。

结论

现在恢复了对MySQL或MariaDB服务器的管理访问。确保你选择的新根密码是强大和安全的,并把它放在安全的地方。

对于新Linux用户来说,这可能是一项艰巨的任务。让我用MySQL 8更新这个(目前可用的最新版本是8.0.12,截至2018-09-12)

Open "mysqld.cnf" configuration file at "/etc/mysql/mysql.conf.d/". Add skip-grant-tables to the next line of [mysql] text and save. Restart the MySQL service as "sudo service mysql restart". Now your MySQL is free of any authentication. Connect to the MySQL client (also known as mysql-shell) as mysql -u root -p. There is no password to be keyed in as of now. Run SQL command flush privileges; Reset the password now as ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPassword'; Now let's get back to the normal state; remove that line "skip-grant-tables" from "mysqld.cnf" and restart the service.

就是这样。

在我的Debian 10的情况下,错误

ERROR 1698 (28000): Access denied for user 'root'@'localhost'

(好方法)解决了

sudo mysql -u root -p mysql

不好的方法:

mysql -u root -p mysql

在尝试了所有其他答案后,这是最后对我有用的:

sudo mysql -- It does not ask me for any password

-- Then in MariaDB/MySQL console:
update mysql.user set plugin = 'mysql_native_password' where User='root';
FLUSH PRIVILEGES;
exit;

我发现答案在博客文章解决:错误“访问拒绝用户' root ' @ ' localhost '”的MySQL - codementor。科技(媒介)。

如果你像我一样,在前面的答案中所有的信息都失败了,继续卸载你机器上的所有版本的MySQL,使用命令sudo find / name " MySQL "和rm -rf每个附加" MySQL "名称的文件或目录(你应该跳过与编程语言库相关的文件)搜索所有剩余的MySQL文件。

现在安装一个新版本的MySQL,开始享受吧。注:你将失去所有的数据,所以先权衡你的选择。

如果你没有足够的特权,就会发生这种情况。

输入su,输入root密码后重试。

之前的答案都没有帮助我解决这个问题,所以这里是我找到的解决方案。

相关部分:

In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) to access the user. In order to use a password to connect to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password. To do this, open up the MySQL prompt from your terminal: sudo mysql Next, check which authentication method each of your MySQL user accounts use with the following command: SELECT user,authentication_string,plugin,host FROM mysql.user; Output +------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | root | | auth_socket | localhost | | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 4 rows in set (0.00 sec) In this example, you can see that the root user does in fact authenticate using the auth_socket plugin. To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing, and note that this command will change the root password you set in Step 2: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect: FLUSH PRIVILEGES; Check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket plugin: SELECT user,authentication_string,plugin,host FROM mysql.user; Output +------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | root | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost | | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 4 rows in set (0.00 sec) You can see in this example output that the root MySQL user now authenticates using a password. Once you confirm this on your own server, you can exit the MySQL shell: exit

如果你像我一样通过谷歌到达这个页面,以前的解决方案都不起作用,原来是错误的是我100%的愚蠢。我没有连接到服务器。一旦连接,一切都是一帆风顺的。

如果它有助于了解我的设置,我正在使用Sequel Pro,并试图使用NPM包连接到它的节点,mysql。我不认为我需要实际连接(除了运行Sequel Pro),因为我已经从我的应用程序中这样做了。

啊,对我来说什么都没用!我有一台运行MariaDB 5.5.64的CentOS 7.4机器。

我必须这么做,在YUM安装MariaDB之后;

systemctl restart mariadb
mysql_secure_installation

mysql_secure_installation将带您完成许多步骤,包括“设置根密码?”[Y / n]”。只要输入“y”并输入密码。随你怎么回答其他问题。

然后你可以用你的密码,使用

mysql -u root -p

它会存活下来

systemctl restart mariadb

的关键

然后,我检查了/bin/mysql_secure_installation源代码,看看它是如何神奇地改变根密码的,而这里的其他答案都不能。导入位是:

do_query "UPDATE mysql.user SET Password=PASSWORD('$esc_pass') WHERE User='root';"

...上面写着SET Password=…而不是SET authentication_string = PASSWORD....因此,本版本(5.5.64)的正确步骤是:

使用mysql -u root -p登录,使用您已经设置的密码。

或者,停止数据库并启动它:

mysql_safe --skip-grant-tables --skip-networking &

从mysql>提示符:

use mysql;
select host,user,password from user where user = 'root';
(observe your existing passwords for root).
UPDATE mysql.user set Password = PASSWORD('your_new_cleartext_password') where user = 'root' AND host = 'localhost';
select host,user,password from user where user = 'root';
flush privileges;
quit;

停止运行mysqld_safe。重启MariaDB。以root用户登录:mysql -u -p。请使用新密码。

如果您愿意,您可以一次设置所有的根密码。我认为这是明智的:

mysql -u root -p

(login)
use mysql;
select host,user,password from user where user = 'root';
UPDATE mysql.user set Password = PASSWORD('your_new_cleartext_password') where user = 'root';
select host,user,password from user where user = 'root';
flush privileges;
quit;

这将执行更新所有的根密码:即,“localhost”,“127.0.0.1”,和“::1”

将来,当我去RHEL 8或其他什么时候,我会试着记得检查/bin/mysql_secure_installation,看看那些家伙是怎么做的,谁是为这个操作系统配置MariaDB的人。

解决方法:放弃!

听我说完。我花了整整两天的时间试图让MySQL工作,但没有任何效果,总是被权限错误卡住,没有一个是通过这个问题的答案解决的。我想,如果我继续下去,我会疯掉的。

由于没有耐心,我发送了安装SQLite的命令,只使用了450 KB,它从一开始就工作得很好。

如果你没有圣人的耐心,那就使用SQLite吧,这样可以为你节省大量的时间、精力、痛苦和存储空间。

在设置mysql-8 zip版本时,我得到了同样的错误。最后,切换到无缝工作的安装版本。在安装过程中,会提示设置root密码。一旦设置好,它就一定能工作。

根据我的经验,如果你没有sudo,它是行不通的。所以确保你的命令是;

sudo mysql -uroot -p

修复macOS

Install MySQL from https://downloads.mysql.com/archives/community/ (8.x is the latest as on date, but ensure that the version is compatible with the macOS version) Give password for root (let <root-password> be the password) during installation (don't forget to remember the password!) Select Use Legacy Password Encryption option (that is what I had used and did not try for Use Strong Password Encryption option) Search and open MySQL.prefPane (use search tool) Select Configuration tab Click Select option of Configuration File Select /private/etc/my.cnf From terminal open a new or existing file with name /etc/my.cnf (vi /etc/my.cnf) add the following content: [mysqld] skip-grant-tables Restart mysqld as follows: ps aux | grep mysql kill -9 <pid1> <pid2> ... (grab pids of all MySQL related processes) mysqld gets restarted automatically Verify that the option is set by running the following from terminal: ps aux | grep mysql > mysql/bin/mysqld ... --defaults-file=/private/etc/my.cnf ... (output) Run the following command to connect (let mysql-<version>-macos<version>-x86_64 be the folder where MySQL is installed. To grab the actual folder, run ls /usr/local/ and copy the folder name): /usr/local/mysql-<version>-macos<version>-x86_64/bin/mysql -uroot -p<root-password>

'-p'参数不要求参数名和值之间有空格。

而不是

./mysqladmin -u root -p 'redacted'

Use

./mysqladmin -u root -p'redacted'

或者只是

./mysqladmin -u root -p

这将提示您输入密码。

根据MariaDB官方文档,在MariaDB 10.4.3及以后版本中,默认安装了unix_socket身份验证插件。

为了禁用它,并恢复到前面的mysql_native_password认证方法,在my.cnf文件的[mysqld]部分中添加下面的行:

[mysqld]
unix_socket=OFF

然后运行:

mysql_install_db --auth-root-authentication-method=normal

然后启动mysqld

这个命令可以正常工作:

mysqladmin -u root password CHANGEME

有关更多信息,请参见配置mysql_install_db恢复到以前的身份验证方法。

有时会在安装时设置默认密码-如文档中所述。可以通过以下命令确认。

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

我试图利用Mac上的Docker桌面来运行5.7.35和这个Docker -compose。Yml配置允许它工作:

特别是这一行…

命令:default-authentication-plugin = mysql_native_password

...这招奏效了

version: '3.3'
services:
  mysql_db:
    image: mysql:5.7
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 'your_password'
    ports:
      - '3306:3306'
    expose:
      - '3306'
    volumes:
      - ~/your/volume/path:/var/lib/mysql

使用sudo修改密码:

sudo mysql

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

来源:Phoenixnap -拒绝访问用户根localhost

在尝试了很多之后,给出了以下答案:

ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');

和类似的答案,我的终端仍然向我抛出以下错误:

你的SQL语法有错误;检查手册,对应于您的MariaDB服务器版本的正确语法使用近…

所以在网上研究后,这一行解决了我的问题,让我改变root用户密码:

sudo mysqladmin --user=root password "[your password]"

我在这里的位置:

UBUNTU 21.04 PHP 5.6.40-57 MYSQL 5.7.37

我们来配置一下

nano /etc/mysql/mysql.conf.d/mysqld.cnf

在底部,写下这个

skip-grant-tables

重新加载它

service mysql restart

窗口:

cd \ampps\mysql\bin :

mysql.exe -u root -pmysql

mysql启动后(可以看到shell是这样的mysql>) 使用这个查询:

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

请再次使用root访问

需要检查的一件事是from-host筛选器。默认情况下可能是“localhost”。您正在尝试从远程客户端连接吗?将其更改为“%”。

在Arch Linux上

软件包:mysql 8.0.29-1

对我有用的是:

Edit my.cnf file, normally can be found at /etc/mysql/my.cnf and append this skip-grant-tables at the bottom/end of the file. Restart mysql service by invoking sudo systemctl restart mysqld Ensuring mysql service has started properly by invoking sudo systemctl status mysqld Login to mysql using 'root' by invoking mysql -u root -p Flush privileges by invoking flush privileges; Create new user by CREATE USER 'root'@'localhost' IDENTIFIED BY 'rootpassword'; (If you plan to use this db with PHP), you should instead use this CREATE USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'rootpassword'; Check whether your changes have reflected in db by invoking the following in sequence: use mysql; SELECT User, password_last_changed FROM user; Exit mysql console and comment/remove skip-grant-tables by editing my.cnf file (Refer to step 1 for the location) Restart the mysql service (Refer to step 2 and step 3)

就这些。