MacOSX homebrew mysql root password

前端 未结 21 1626
自闭症患者
自闭症患者 2020-12-04 05:43

For some reason MySQL stopped giving access for root. Uninstalled and reinstalled with Homebrew. Fresh install, fresh tables but when I enter

mysql -u root -         


        
21条回答
  •  死守一世寂寞
    2020-12-04 06:20

    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.

    Stop the currently running MySQL instance

    launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    

    Now start mysql by hand skipping the grant tables and networking

    $(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.

    Once you have done this, you now should have a running, unprotected MySQL instance up.

    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> 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> exit
    Bye
    

    Stop server:

    mysqladmin -u root shutdown
    

    Now, lets put back the launch daemon so we have our MySQL at the ready again:

    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!

提交回复
热议问题