mysql -u root -p
我遇到了这个错误:
访问被拒绝,用户'root'@'localhost'(未使用密码)。
我已经重新安装了MySQL五次,但它仍然要求输入密码。我该如何解决这个问题?
这些方法都没有对我起作用。我想我已经在电脑上安装了MySQL,所以密码可能已经被设置过了。在尝试了很多解决方案之后,以下方法最终成功了:
$ brew services stop mysql
$ pkill mysqld
$ rm -rf /usr/local/var/mysql/ # NOTE: this will delete your existing database!!!
$ brew postinstall mysql
$ brew services restart mysql
$ mysql -uroot
所有的功劳归于@Ghrua
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOURNEWPASSWORD';
来设置您的新根密码。 - Martin Braunbrew postinstall mysql
对我很有帮助。谢谢! - lucasvscnbrew services stop mysql; brew uninstall mysql; pkill mysqld; rm -rf /usr/local/var/mysql; rm /usr/local/etc/my.cnf; brew postinstall mysql; brew install mysql; brew services restart mysql; mysql -uroot
- aubreypwd只需运行以下命令(其中NEWPASS
为您的密码):
$(brew --prefix mysql)/bin/mysqladmin -u root password NEWPASS
我曾经遇到了同样的错误,并用以下方法解决了它。
$(brew --prefix mariadb)/bin/mysqladmin -u root password NEWPASS
。 - bryceadamssudo $(brew --prefix mariadb)/bin/mysqladmin -u root password NEWPASS
- upful如果您无意中设置并忘记了root密码,而且您不想擦除所有数据库并重新开始,因为您很懒,并忘记了备份解决方案,而且您正在使用相当新的Homebrew安装(2013年冬季),那么以下是重置MySQL密码的步骤。
停止当前正在运行的MySQL实例
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
现在手动启动mysql,跳过授权表和网络设置
$(brew --prefix mysql)/bin/mysqld_safe --skip-grant-tables --skip-networking
mysql -u root
mysql> update mysql.user set password=PASSWORD('new_password_here') WHERE user='root';
Query OK, 1 row affected (0.02 sec)
Rows matched: 4 Changed: 1 Warnings: 0
退出MySQL提示符。
mysql> exit
Bye
停止服务器:
mysqladmin -u root shutdown
现在,让我们重新安装启动守护进程,这样我们就可以再次使用MySQL了:
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
mysqladmin -u root shutdown
- Vebjorn Ljosapassword
不是一个有效的列名。相反,我必须使用authentication_string
代替。其余的指示完美地运作。谢谢! - M. Scott Fordupdate user set authentication_string=password('new_password_here') where user='root';
。 - Panayotisupdate mysql.user set authentication_string=password('none') where user='root';
。(与在我上面的评论相同,但在“user”前添加“mysql.”) - Steve Meisner几天前我遇到了同样的问题。如果你是通过homebrew
安装mysql
并在启动mysql
守护进程之前运行初始化脚本mysql_install_db
, 就会出现这个问题。
为了解决这个问题,你可以删除mysql
数据文件,重新启动服务,然后再运行初始化脚本:
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
rm -r /usr/local/var/mysql/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
在使用Homebrew安装mysql后出现了这个错误。
首先删除已安装的mysql,然后通过Homebrew重新安装。
brew update
brew doctor
brew install mysql
然后重新启动mysql服务
mysql.server restart
然后运行此命令以设置您的新根密码。
mysql_secure_installation
最后它会要求重新加载权限。选择“是”。然后再次登录到MySQL。使用您设置的新密码。
mysql -u root -p
mysql_secure_installation
,但关键部分是 mysql.server restart
。 - Johan如果您正在运行Mojave、Catalina、Big Sur,并且现在是在macOS Monterey上:
brew install mariadb
...
brew services start mariadb
==> Successfully started `mariadb` (label: homebrew.mxcl.mariadb)
$(brew --prefix mariadb)/bin/mysqladmin -u root password newpass
/usr/local/opt/mariadb/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost''
同时使用root账户登录也失败了:
mariadb -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
默认管理员用户的用户名与您的MacOS帐户 用户名 相同,例如johnsmit。
要登录为根用户并设置根密码,请使用以下命令(使用您的用户名):
mariadb -u johnsmit
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 10.4.11-MariaDB Homebrew
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'ROOT-PASSWORD';
Query OK, 0 rows affected (0.006 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> exit
Bye
如何在本地更改mysql的root密码。
额外提示:要更改当前用户或其他用户的密码,可以使用mysqladmin
命令:
$(brew --prefix mariadb)/bin/mysqladmin -u arunas password 'newsecret'
但出于某种原因,这不会影响本地主机,但应该适用于应用程序登录。
或者使用本机MySQL更改用户密码SQL,明确指定主机,在我的情况下为用户的'localhost'账户:
mariadb -u arunas
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.5.9-MariaDB Homebrew
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> ALTER USER 'arunas'@'localhost' IDENTIFIED BY 'newsecret';
Query OK, 0 rows affected (0.006 sec)
MariaDB [(none)]> exit
Bye
现在让我们尝试不用密码登录:
mariadb -u arunas
ERROR 1045 (28000): Access denied for user 'arunas'@'localhost' (using password: NO)
你看到登录失败的提示,因此我们现在需要指定密码:
mariadb -u arunas -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.5.9-MariaDB Homebrew
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye
愉快的使用!
由于问题在很长时间前就被提出/回答了,那些最佳答案对我来说不起作用。这是我的解决方案,在2020年。
背景: 使用Homebrew安装了新的mysql/mariadb。
问题: root用户的密码不为空且未知。
解决方法:
原因:
这对我有用:
sudo mysql -u root
希望这有所帮助。
使用sudo
命令可避免"访问被拒绝"错误:
sudo $(brew --prefix mariadb)/bin/mysqladmin -u root password NEWPASS
mysql_secure_installation
命令并回答相应问题即可,非常简单。 - Abhinav Singh