使用SKIP-GRANT-TABLES 解决 MYSQL ROOT密码丢失(转)

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 passwords, see Section 2.18.4, “Securing the Initial MySQL Accounts”.

If you know the root password and want to change it, see Section 13.7.1.6, “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 rootaccount 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, selectControl 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 following statement on a single line. Replace the password with the password that you want to use.
    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:\> cd "C:\Program Files\MySQL\MySQL Server 5.0\bin"
    C:\> mysqld-nt --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-nt
             --defaults-file="C:\\Program Files\\MySQL\\MySQL Server 5.0\\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, thenAdministrative 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.

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 youmust 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 following statement on a single line. Replace the password with the password that you want to use.
    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_safe --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.

  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.

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 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> 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).

 

http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html

 

介绍一个非常有用的mysql启动参数—— --skip-grant-tables。顾名思义,就是在启动mysql时不启动grant-tables,授权表。有什么用呢?当然是忘记管理员密码后有用。
    操作方法:
    1、杀掉原来进行着的mysql:
       rcmysqld stop
       或者:
       service mysqld stop
       或者:
       kill -TERM mysqld
    2、以命令行参数启动mysql:
       /usr/bin/mysqld_safe --skip-grant-tables &
    3、修改管理员密码:
       use mysql;
       update user set password=password('yournewpasswordhere') where user='root';
       flush privileges;
       exit;
    4、杀死mysql,重启mysql

 

也可以在配置文件/etc/my.cnf中配置这个参数

忘记MySQL(和PHP搭配之最佳组合) ROOT密码是在MySQL(和PHP搭配之最佳组合)使用中很常见的问题,可是有很多朋友并不会重置ROOT密码,那叫苦啊,我有深有感触,特写此文章与大家交流:

1、编辑MySQL(和PHP搭配之最佳组合)配置文件:

windows环境中:%MySQL(和PHP搭配之最佳组合)_installdir%\my.ini //一般在MySQL(和PHP搭配之最佳组合)安装目录下有my.ini即MySQL(和PHP搭配之最佳组合)的配置文件。

linux环境中:/etc/my.cnf

在[MySQL(和PHP搭配之最佳组合)d]配置段添加如下一行:

skip-grant-tables

保存退出编辑。

2、然后重启MySQL(和PHP搭配之最佳组合)服务

windows环境中:

net stop MySQL(和PHP搭配之最佳组合)

net start MySQL(和PHP搭配之最佳组合)

linux环境中:

/etc/init.d/MySQL(和PHP搭配之最佳组合)d restart

3、设置新的ROOT密码

然后再在命令行下执行:

MySQL(和PHP搭配之最佳组合) -uroot -p MySQL(和PHP搭配之最佳组合)

直接回车无需密码即可进入数据库了。

现在我们执行如下语句把root密码更新为 7758521:

update user set password=PASSWORD("123456") where user='root';

quit 退出MySQL(和PHP搭配之最佳组合)。

4、还原配置文件并重启服务

然后修改MySQL(和PHP搭配之最佳组合)配置文件把刚才添加的那一行删除。

再次重起MySQL(和PHP搭配之最佳组合)服务,密码修改完毕。

修改完毕。

用新密码123456试一下吧,又能登入MySQL(和PHP搭配之最佳组合)的感觉就是不一样吧?

总结:

1. 先杀掉mysqld的进程:

小技巧:让你忘记MySQL密码也不害怕

2. 使用skip-grant-tables这个选项启动MySQL:

小技巧:让你忘记MySQL密码也不害怕

3. 登录修改密码:

小技巧:让你忘记MySQL密码也不害怕

4. 关掉MySQL

小技巧:让你忘记MySQL密码也不害怕

5. 启动MySQL

小技巧:让你忘记MySQL密码也不害怕

以上就是本文给大家介绍的使用SKIP-GRANT-TABLES 解决 MYSQL ROOT密码丢失,希望大家喜欢。

http://www.bkjia.com/Mysql/1058276.html

时间: 2024-12-09 22:04:18

使用SKIP-GRANT-TABLES 解决 MYSQL ROOT密码丢失(转)的相关文章

使用SKIP-GRANT-TABLES 解决 MYSQL ROOT密码丢失_Mysql

忘记MySQL(和PHP搭配之最佳组合) ROOT密码是在MySQL(和PHP搭配之最佳组合)使用中很常见的问题,可是有很多朋友并不会重置ROOT密码,那叫苦啊,我有深有感触,特写此文章与大家交流: 1.编辑MySQL(和PHP搭配之最佳组合)配置文件: windows环境中:%MySQL(和PHP搭配之最佳组合)_installdir%\my.ini //一般在MySQL(和PHP搭配之最佳组合)安装目录下有my.ini即MySQL(和PHP搭配之最佳组合)的配置文件. linux环境中:/e

linux重置mysql root密码的6种方法

shell脚本重启mysql密码 注:记得给此脚本属于执行权限哦.(chmod u+x reset_mysql_root_password.sh) 此shell脚本如下:  代码如下 复制代码 #!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # Check if user is root if [ $(id -u) != "0" ]; then

破解mysql root密码的几种方法

   几种破解MySQL root密码的几种方法: 方法一 使用phpmyadmin,这是最简单的了,修改mysql库的user表,不过别忘了使用PASSWORD函数. 方法二 使用mysqladmin,这是前面声明的一个特例. mysqladmin -u root -p password mypasswd 输入. 方法一  使用phpmyadmin,这是最简单的了,修改mysql库的user表,不过别忘了使用PASSWORD函数. 方法二  使用mysqladmin,这是前面声明的一个特例. 

如何解决Linux的Root密码丢失问题

简介: Linux root密码丢失了怎么办?给你一个方法. GRUB:在引导装载程序菜单上,键入 [e] 来进入编辑模式.你会面对一个引导项目列表.查找其中类似以下输出 的句行: kernel /vmlinuz-2.4.18-0.4 ro root=/dev/hda2 按箭头键直到这一行被突出显示,然后按 [e].你现在可在文本结尾处空一格再添加 single 来告诉 GRUB 引导单用户 Linux 模式.按 [Enter] 键来使编辑结果生效. 你会被带会编辑模式屏幕,从这里,按 [b],

Linux中重置MySQL ROOT密码详解

重启mysql root密码 方法一.使用mysqladmin  代码如下 复制代码 # ./mysqladmin -u root password 'newpassword' # ./mysqladmin -u root -h host_name password 'newpassword' Usually mysqladmin's path is /usr/bin, host_name is your real host name, e.g. localhost.localdomain. p

mysql的密码丢失问题--重置过密码之后也无法用密码登陆,不输入密码可以登陆

问题描述 mysql的密码丢失问题--重置过密码之后也无法用密码登陆,不输入密码可以登陆 解决方案 将Apache->Alias->http://localhost/phpmyadmin->Edit alias打开 将其中的 Options Indexes FollowSymLinks ExecCGI AllowOverride all Order Deny,Allow Deny from all Allow from 127.0.0.1 Allow from ::1 Allow fro

如何修改mysql root密码

关于如何修改mysql教程 root密码我们列出了很多种关于mysql密码的修改,好了费话不说多了我们来看看这几天修改root密码的有效手段与方法吧. 方法一 使用php教程myadmin,这是最简单的了,修改mysql库的user表,不过别忘了使用PASSWORD函数. www.111cn.net 方法二 www.111cn.net 使用mysqladmin,这是前面声明的一个特例. www.111cn.net mysqladmin -u root -p password mypasswd  

忘mysql root密码之MYSQLADMIN修改密码方法

一般出来忘记密码都是一新mysql新手朋友,下面我们来告诉你如何利用mysqadmin来把密码修改过来吧. 我来做几个例子相信很快就明白了 . 1.原来的密码是123456 C:>type mysql5.bat @echo off mysql -uroot -p123456 -P3306 正确的修改MYSQL用户密码的格式是: 我们这里用 用户:root(可以换成其他的) 密码:woshiduide 来演示新密码. 代码如下  C:>mysqladmin -uroot -p password

windows 下忘记mysql root密码的更改方法_Mysql

mysql数据库忘记了root密码是件很痛苦的事,本文介绍如何解决windows环境下mysql服务器忘记root密码的解决方法. 1.关闭MySQL服务 window+r运行输入net stop mysql 2.在mysql的bin目录下打开命令行 空白处shift+鼠标右键---->找到在此处打开命令窗口 3.输入mysqld -nt --skip-grant-tables 出现信息以"set to off"结束,说明成功 4.同样地点另开一个命令行,输入mysql,回车 5