Setting the MySQL root user password on OS X

2019-01-04 04:28发布

I just installed MySQL on Mac OS X. The next step was setting the root user password, so I did this next:

  1. Launch the terminal app to access the Unix command line.
  2. Under the Unix prompt I executed these commands:

    $ cd /usr/local/mysql/bin
    $ ./mysqladmin -u root password 'password'
    

But, when I execute the command

$ ./mysql -u root, this is the answer:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 224
Server version: 5.5.13 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

I can get into the mysql command line without any password!

Why is this?

16条回答
Emotional °昔
2楼-- · 2019-01-04 05:19
  1. Stop the mysqld server.

    • Mac OSX: System Preferences > MySQL > Stop MySQL Server
    • Linux (From Terminal): sudo systemctl stop mysqld.service
  2. Start the server in safe mode with privilege bypass

    • From Terminal: sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
  3. In a new terminal window:

    • sudo /usr/local/mysql/bin/mysql -u root
  4. This will open the mysql command line. From here enter:

    • UPDATE mysql.user SET authentication_string=PASSWORD('NewPassword') WHERE User='root';

    • FLUSH PRIVILEGES;

    • quit

  5. Stop the mysqld server again and restart it in normal mode.

    • Mac OSX (From Terminal): sudo /usr/local/mysql/support-files/mysql.server restart
    • Linux Terminal: sudo systemctl restart mysqld
查看更多
【Aperson】
3楼-- · 2019-01-04 05:20

The methods mentioned in existing answers don't work for mysql 5.7.6 or later. According mysql documentation this is the recommended way.

B.5.3.2.3 Resetting the Root Password: Generic Instructions

MySQL 5.7.6 and later:

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

Reference: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

查看更多
相关推荐>>
4楼-- · 2019-01-04 05:21

If you can't remember your password, @radtek's answer worked for me except in my case I had set up MySQL using brew which meant that steps 1 and 2 of his answer had to be changed to:

  1. /usr/local/bin/mysql.server stop

  2. /usr/local/bin/mysqld_safe --skip-grant-tables

Note: the lack of sudo.

查看更多
女痞
5楼-- · 2019-01-04 05:21

Stopping MySQL Server

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

Starting MySQL in safe mode

sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &

Changing the root password

/usr/local/mysql/bin/mysql -u root

use mysql;
UPDATE user SET authentication_string=PASSWORD('NEW_PASSWORD') WHERE user='root';
FLUSH PRIVILEGES;
exit

Testing

Run /usr/local/mysql/bin/mysql -u root

Now enter the new password to start using MySQL.

查看更多
登录 后发表回答