10bet网址
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr)- 41.5Mb
PDF (A4)- 41.6Mb
Man Pages (TGZ)- 262.1Kb
Man Pages (Zip)- 372.3Kb
Info (Gzip)- 4.0Mb
Info (Zip)- 4.0Mb
Excerpts from this Manual

B.3.3.2 How to Reset the Root Password

If you have never assigned arootpassword for MySQL, the server does not require a password at all for connecting asroot. However, this is insecure. For instructions on assigning a password, seeSection 2.10.4, “Securing the Initial MySQL Account”.

If you know therootpassword and want to change it, seeSection 13.7.1.1, “ALTER USER Statement”, andSection 13.7.1.10, “SET PASSWORD Statement”.

If you assigned arootpassword previously but have forgotten it, you can assign a new password. The following sections provide instructions for Windows and Unix and Unix-like systems, as well as generic instructions that apply to any system.

B.3.3.2.1 Resetting the Root Password: Windows Systems

On Windows, use the following procedure to reset the password for the MySQL'root'@'localhost'account. To change the password for arootaccount with a different host name part, modify the instructions to use that host name.

  1. 登录到您的系统管理员。

  2. Stop the MySQL server if it is running. For a server that is running as a Windows service, go to the Services manager: From theStartmenu, selectControl Panel, thenAdministrative Tools, thenServices. Find the MySQL service in the list and stop it.

    If your server is not running as a service, you may need to use the Task Manager to force it to stop.

  3. Create a text file containing the password-assignment statement on a single line. Replace the password with the password that you want to use.

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
  4. Save the file. This example assumes that you name the fileC:\mysql-init.txt.

  5. Open a console window to get to the command prompt: From theStartmenu, selectRun, then entercmdas the command to be run.

  6. Start the MySQL server with theinit_filesystem variable set to name the file (notice that the backslash in the option value is doubled):

    C:\> cd "C:\Program Files\MySQL\MySQL Server 8.0\bin" C:\> mysqld --init-file=C:\\mysql-init.txt

    If you installed MySQL to a different location, adjust thecdcommand accordingly.

    The server executes the contents of the file named by theinit_filesystem variable at startup, changing the'root'@'localhost'account password.

    To have server output to appear in the console window rather than in a log file, add the--consoleoption to themysqldcommand.

    If you installed MySQL using the MySQL Installation Wizard, you may need to specify a--defaults-fileoption. For example:

    C:\> mysqld --defaults-file="C:\\ProgramData\\MySQL\\MySQL Server 8.0\\my.ini" --init-file=C:\\mysql-init.txt

    The appropriate--defaults-filesetting can be found using the Services Manager: From theStartmenu, selectControl Panel, thenAdministrative Tools, thenServices. Find the MySQL service in the list, right-click it, and choose thePropertiesoption. ThePath to executablefield contains the--defaults-filesetting.

  7. After the server has started successfully, deleteC:\mysql-init.txt.

You should now be able to connect to the MySQL server asrootusing the new password. Stop the MySQL server and restart it normally. If you run the server as a service, start it from the Windows Services window. If you start the server manually, use whatever command you normally use.

B.3.3.2.2 Resetting the Root Password: Unix and Unix-Like Systems

On Unix, use the following procedure to reset the password for the MySQL'root'@'localhost'account. To change the password for arootaccount with a different host name part, modify the instructions to use that host name.

The instructions assume that you start the MySQL server from the Unix login account that you normally use for running it. For example, if you run the server using themysqllogin account, you should log in asmysqlbefore using the instructions. Alternatively, you can log in asroot, but in this case youmuststartmysqldwith the--user=mysqloption. If you start the server asrootwithout using--user=mysql, the server may createroot-owned files in the data directory, such as log files, and these may cause permission-related problems for future server startups. If that happens, you must either change the ownership of the files tomysqlor remove them.

  1. Log on to your system as the Unix user that the MySQL server runs as (for example,mysql).

  2. Stop the MySQL server if it is running. Locate the.pidfile that contains the server's process ID. The exact location and name of this file depend on your distribution, host name, and configuration. Common locations are/var/lib/mysql/,/var/run/mysqld/, and/usr/local/mysql/data/. Generally, the file name has an extension of.pidand begins with eithermysqldor your system's host name.

    Stop the MySQL server by sending a normalkill(notkill -9) to themysqldprocess. Use the actual path name of the.pidfile in the following command:

    shell> kill `cat /mysql-data-directory/host_name.pid`

    Use backticks (not forward quotation marks) with thecatcommand. These cause the output ofcatto be substituted into thekillcommand.

  3. Create a text file containing the password-assignment statement on a single line. Replace the password with the password that you want to use.

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
  4. Save the file. This example assumes that you name the file/home/me/mysql-init. The file contains the password, so do not save it where it can be read by other users. If you are not logged in asmysql(the user the server runs as), make sure that the file has permissions that permitmysqlto read it.

  5. Start the MySQL server with theinit_filesystem variable set to name the file:

    shell> mysqld --init-file=/home/me/mysql-init &

    The server executes the contents of the file named by theinit_filesystem variable at startup, changing the'root'@'localhost'account password.

    Other options may be necessary as well, depending on how you normally start your server. For example,--defaults-filemay be needed before theinit_fileargument.

  6. After the server has started successfully, delete/home/me/mysql-init.

You should now be able to connect to the MySQL server asrootusing the new password. Stop the server and restart it normally.

B.3.3.2.3根密码重置:通用Instructions

The preceding sections provide password-resetting instructions specifically for Windows and Unix and Unix-like systems. Alternatively, on any platform, you can reset the password using themysqlclient (but this approach is less secure):

  1. Stop the MySQL server if necessary, then restart it with the--skip-grant-tablesoption. This enables anyone to connect without a password and with all privileges, and disables account-management statements such asALTER USERandSET PASSWORD. Because this is insecure, if the server is started with the--skip-grant-tablesoption, it also disables remote connections by enablingskip_networking.

  2. Connect to the MySQL server using themysqlclient; no password is necessary because the server was started with--skip-grant-tables:

    shell> mysql
  3. In themysqlclient, tell the server to reload the grant tables so that account-management statements work:

    mysql> FLUSH PRIVILEGES;

    Then change the'root'@'localhost'account password. Replace the password with the password that you want to use. To change the password for arootaccount with a different host name part, modify the instructions to use that host name.

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

You should now be able to connect to the MySQL server asrootusing the new password. Stop the server and restart it normally (without the--skip-grant-tables选项,没有启用skip_networkingsystem variable).