MacOSX 自制 mysql 根密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9695362/
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
MacOSX homebrew mysql root password
提问by Alisher Ulugbekov
For some reason MySQL stopped giving access for root. Uninstalled and reinstalled with Homebrew. Fresh install, fresh tables but when I enter
出于某种原因,MySQL 停止授予 root 访问权限。使用 Homebrew 卸载并重新安装。全新安装,全新表,但当我进入时
mysql -u root -p
I get this error:
我收到此错误:
Access denied for user 'root'@'localhost' (using password: NO)
用户 'root'@'localhost' 访问被拒绝(使用密码:NO)
I reinstalled MySQL five times but it is still asking for a password. How do I fix this?
我重新安装了 MySQL 5 次,但它仍然要求输入密码。我该如何解决?
回答by egermano
Just run this command (where NEWPASS
is your password):
只需运行此命令(NEWPASS
您的密码在哪里):
$(brew --prefix mysql)/bin/mysqladmin -u root password NEWPASS
I have had the same error and fixed it this way.
我有同样的错误并以这种方式修复它。
回答by stackPusher
None of these worked for me. I think i already had mysql somewhere on my computer so a password was set there or something. After spending hours trying every solution out there this is what worked for me:
这些都不适合我。我想我的电脑上已经有 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
all credit to @Ghrua
所有功劳都归功于@Ghrua
回答by Britto
I had the same problem a couple days ago. It happens when you install mysql
via homebrew
and run the initialization script (mysql_install_db
) beforestarting the mysql
daemon.
几天前我遇到了同样的问题。在启动守护程序之前mysql
通过安装homebrew
并运行初始化脚本 ( mysql_install_db
)时会发生这种情况。mysql
To fix it, you can delete mysql
data files, restart the service and thenrun the initialization script:
要修复它,您可以删除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
回答by ablemike
In case you have inadvertently set and forgot the root password, and you don't want to wipe all your databases and start over because you are lazy and forgot to have a back up solution in place, and you are using a fairly recent Homebrew install (Winter 2013), here are steps to reset your password for MySQL.
如果您无意中设置并忘记了 root 密码,并且您不想擦除所有数据库并重新开始,因为您很懒惰并且忘记了备份解决方案,并且您使用的是最近的 Homebrew 安装(2013 年冬季),这里是重置 MySQL 密码的步骤。
Stop the currently running MySQL instance
停止当前运行的 MySQL 实例
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Now start mysql by hand skipping the grant tables and networking
现在手动启动 mysql 跳过授权表和网络
$(brew --prefix mysql)/bin/mysqld_safe --skip-grant-tables --skip-networking
Note that if when you run echo $(brew --prefix mysql)and it does not respond as "/usr/local/opt/mysql"in bash, you will need to adjust the path accordingly.
请注意,如果当您运行echo $(brew --prefix mysql)并且它在 bash中没有响应为“/usr/local/opt/mysql”时,您将需要相应地调整路径。
Once you have done this, you now should have a running, unprotected MySQL instance up.
完成此操作后,您现在应该有一个正在运行的、未受保护的 MySQL 实例。
Log in and set the password
登录并设置密码
mysql -u root
At the prompt, enter the following MySQL command to set a new password for the effected user.
在提示符下,输入以下 MySQL 命令为受影响的用户设置新密码。
mysql> update mysql.user set password=PASSWORD('new_password_here') WHERE user='root';
If all went to plan it should say:
如果一切按计划进行,它应该说:
Query OK, 1 row affected (0.02 sec)
Rows matched: 4 Changed: 1 Warnings: 0
Exit out of the MySQL prompt.
退出 MySQL 提示符。
mysql> exit
Bye
Stop server:
停止服务器:
mysqladmin -u root shutdown
Now, lets put back the launch daemon so we have our MySQL at the ready again:
现在,让我们放回启动守护进程,让我们的 MySQL 再次准备就绪:
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Congratulations. You've just reset your mysql root password. Pour yourself a coffee and get a backup solution in place!
恭喜。您刚刚重置了您的 mysql 根密码。给自己倒杯咖啡,准备一个备用解决方案!
回答by Nirojan Selvanathan
Got this error after installing mysql via home brew.
通过 Home brew 安装 mysql 后出现此错误。
So first remove the installation. Then Reinstall via Homebrew
所以先卸载安装。然后通过 Homebrew 重新安装
brew update
brew doctor
brew install mysql
Then restart mysql service
然后重启mysql服务
mysql.server restart
Then run this command to set your new root password.
然后运行此命令来设置新的 root 密码。
mysql_secure_installation
Finally it will ask to reload the privileges. Say yes. Then login to mysql again. And use the new password you have set.
最后它会要求重新加载权限。说是。然后再次登录mysql。并使用您设置的新密码。
mysql -u root -p
回答by Arunas Bartisius
If you run
如果你跑
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''
also login with root account fails:
使用 root 帐户登录也失败:
mariadb -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
then default admin user is created same name as your MacOS account username, e.g. johnsmit.
然后创建默认管理员用户与您的 MacOS 帐户用户名相同的名称,例如 johnsmit。
To login as root, issue:
要以 root 身份登录,请发出:
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)]> exit
Bye
回答by Béatrice Cassistat
This worked for me:
这对我有用:
sudo mysql -u root
回答by Spyder
I've just noticed something common to most of the answers here, and confirmed on my fresh install. It's actually obvious if you look at the recommendations to run mysqladmin -u root
without -p
.
我刚刚注意到这里的大多数答案都有一些共同点,并在我的全新安装中得到了确认。如果您查看mysqladmin -u root
不使用-p
.
There is no password.
没有密码。
Brew sets mysql up with just a root user and no password at all. This makes sense, I guess, but the post-install Caveats don't mention it at all.
Brew 只使用 root 用户设置 mysql,根本没有密码。我想这是有道理的,但安装后的警告根本没有提到它。
回答by Matias Miranda
Try with sudo
to avoid the "Access denied" error:
尝试sudo
避免“拒绝访问”错误:
sudo $(brew --prefix mariadb)/bin/mysqladmin -u root password NEWPASS
sudo $(brew --prefix mariadb)/bin/mysqladmin -u root password NEWPASS
回答by user8845989
Check that you don't have a .my.cnf hiding in your homedir. That was my problem.
检查您的 homedir 中是否没有 .my.cnf 文件。那是我的问题。