茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1670858 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00061.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

DB研討會 : [轉貼]MySQL 修改密碼與忘記密碼重設

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]MySQL 修改密碼與忘記密碼重設

MySQL修改密碼與忘記密碼重設

How to change / reset MySQL password.

說明MySQL如何修改密碼與忘記密碼時如何重設密碼。

設定root密碼

一開始安裝好mysql時,root可能尚未設定密碼,可以用以下指令設定


# mysqladmin -u root password '你的密碼'

或者


# mysqladmin -u root password

再輸入密碼

修改使用者密碼

方法一

使用有權限或要修改的使用者本身登入mysql


# mysql -u 登入使用者 -p
mysql> SET PASSWORD FOR '目標使用者'@'主機' = PASSWORD('密碼');
mysql> flush privileges;

例如


# mysql -u root -p
mysql> SET PASSWORD FOR 'emn178'@'localhost' = PASSWORD('password');
mysql> flush privileges;

方法二

使用有權限的使用者登入mysql


# mysql -u 登入使用者 -p
mysql> use mysql;
mysql> UPDATE user SET Password=PASSWORD("密碼") WHERE User='目標使用者';
mysql> flush privileges;

例如


# mysql -u root -p
mysql> use mysql;
mysql> UPDATE user SET Password=PASSWORD("password") WHERE User='emn178';
mysql> flush privileges;

上面是不分主機位址的修改,若要像方法一區分主機的話再加上Host條件,例如


mysql> UPDATE user SET Password=PASSWORD("password") WHERE User='emn178' AND Host = 'localhost';

方法三

同樣利用mysqladmin指令可以修改root或其他使用者密碼,但該使用者必須有SUPER權限


# mysqladmin -u 使用者 -p'舊密碼' password '新密碼'

或者


# mysqladmin -u 使用者 -p password

再輸入密碼

忘記密碼重設

如果忘記root密碼可以用以下方式重設


# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &

用上面方式啟動mysql後可以不用輸入密碼直接連入


# mysql -u root

接者使用修改使用者密碼的方法二修改root密碼,例如


mysql> use mysql;
mysql> UPDATE user SET Password=PASSWORD("password") WHERE User='root';
mysql> flush privileges;
mysql> quit

最後重新啟動mysql


# /etc/init.d/mysql stop
# /etc/init.d/mysql start


原文出處:MySQL修改密碼與忘記密碼重設 @ 小殘的程式光廊 :: 痞客邦 PIXNET ::
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]MySQL How to Reset the Root Password

B.5.3.2 How to Reset the Root Password

If you have never assigned a root password for MySQL, the server does not require a password at all for connecting as root. However, this is insecure. For instructions on assigning a password, see Section 2.10.4, “Securing the Initial MySQL Accounts”.

If you know the root password and want to change it, see Section 13.7.1.1, “ALTER USER Syntax”, and Section 13.7.1.7, “SET PASSWORD Syntax”.

If you assigned a root
password 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.5.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 a root account with a different host name part, modify the instructions to use that host name.

  1. Log on to your system as Administrator.

  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 the Start menu, select Control Panel, then
    Administrative Tools, then Services. 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.

    MySQL 5.7.6 and later:

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

    MySQL 5.7.5 and earlier:

    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
  4. Save the file. This example assumes that you name the file C:\mysql-init.txt.

  5. Open a console window to get to the command prompt: From the Start menu, select Run
    , then enter cmd as the command to be run.

  6. Start the MySQL server with the special --init-file option (notice that the backslash in the option value is doubled):


    C:\> mysqld --init-file=C:\\mysql-init.txt

    If you installed MySQL to a different location, adjust the cd command accordingly.

    The server executes the contents of the file named by the --init-file option 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 --console option to the mysqld command.


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


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

    The appropriate --defaults-file setting can be found using the Services Manager: From the Start menu, select Control Panel, then Administrative Tools, then Services. Find the MySQL service in the list, right-click it, and choose the Properties option. The Path to executable field contains the --defaults-file setting.

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

You should now be able to connect to the MySQL server as root
using 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.

If the ALTER USER statement fails to reset the password, try repeating the procedure using the following statements to modify the user table directly:


UPDATE mysql.user
SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;

B.5.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 a root account with a different host name part, modify the instructions to use that host name.

The instructions assume that you will start the MySQL server from the Unix login account that you normally use for running it. For example, if you run the server using the mysql login account, you should log in as mysql before using the instructions. Alternatively, you can log in as root, but in this case you must start
mysqld
with the --user=mysql option. If you start the server as root without using --user=mysql, the server may create root-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 will need to either change the ownership of the files to mysql or 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 .pid
    file 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 .pid and begins with either mysqld or your system's host name.

    Stop the MySQL server by sending a normal kill (not kill -9) to the mysqld process. Use the actual path name of the .pid file in the following command:

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

    Use backticks (not forward quotation marks) with the cat command. These cause the output of cat to be substituted into the kill command.

  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.

    MySQL 5.7.6 and later:

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

    MySQL 5.7.5 and earlier:

    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('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 as mysql
    (the user the server runs as), make sure that the file has permissions that permit mysql to read it.

  5. Start the MySQL server with the special --init-file option:

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

    The server executes the contents of the file named by the --init-file option 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-file
    may be needed before --init-file.

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

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

If the ALTER USER statement fails to reset the password, try repeating the procedure using the following statements to modify the user table directly:


UPDATE mysql.user
SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;

B.5.3.2.3 Resetting the Root Password: Generic 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 the mysql client (but this approach is less secure):

  1. Stop the MySQL server if necessary, then restart it with the --skip-grant-tables
    option. This enables anyone to connect without a password and with all privileges, and disables account-management statements such as ALTER USER and SET PASSWORD. Because this is insecure, you might want to use --skip-grant-tables in conjunction with --skip-networking to prevent remote clients from connecting.


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

    shell> mysql
  3. In the mysql client, 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 a root
    account with a different host name part, modify the instructions to use that host name.

    MySQL 5.7.6 and later:

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

    MySQL 5.7.5 and earlier:

    mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');

You should now be able to connect to the MySQL server as root using the new password. Stop the server and restart it normally (without the --skip-grant-tables and --skip-networking options).

If the
ALTER USER statement fails to reset the password, try repeating the procedure using the following statements to modify the user table directly:


UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPass')
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;


原文出處: MySQL :: MySQL 5.7 Reference Manual :: B.5.3.2 How to Reset the Root Password
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|