-2

我有一个在 OpenSUSE 12.3 上运行的 LAMP 服务器。有时我需要恢复我的 MySQL 密码,我每次都执行以下步骤:

  1. 停止 mysql ( service mysql stop)
  2. 编辑 /etc/my.cnf ( vi /etc/my.cnf)
  3. 在服务器部分添加skip-grant-tables
  4. 启动 Mysql ( service mysql start)

在这个阶段你就可以正常登录mysql了(不会提示密码)

现在在控制台中:

  1. mysql
  2. use mysql;
  3. insert into user (Host, User, Password) values ('localhost','root','');
  4. update user set Select_priv='Y',Insert_priv='Y',Update_priv='Y',Delete_priv='Y',Crea Event_priv='Y', Trigger_priv='Y', Create_tablespace_priv='Y' where user='root'; quit;

(所有这些命令都在 mysql 控制台或通过 phpmyadmin 运行)

现在上面的命令告诉 mysql 创建 root 用户(因为它在发行版中不存在)

我们都准备好了,现在恢复

  1. 编辑 my.cnf ( vi /etc/my.cnf)
  2. 消除:skip-grant-tables
  3. mysqladmin shutdown
  4. service mysql start
  5. mysqladmin -u root password XXXXXXXXXX

当我必须恢复我的 MySQL 密码时,我想自动化这个过程。

4

1 回答 1

0

Here is a start:

service mysql stop
vi /etc/my.cnf
# In the server section add skip-grant-tables
service mysql start

mysql
use mysql;
insert into user (Host, User, Password) values ('localhost','root','');
update user set Select_priv='Y',Insert_priv='Y',Update_priv='Y',Delete_priv='Y',Create_Event_priv='Y', Trigger_priv='Y', Create_tablespace_priv='Y' where user='root';
quit;

vi /etc/my.cnf
# remove skip-grant-tables
mysqladmin shutdown
service mysql start
mysqladmin -u root password XXXXXXXXXX

You should recognise the code, it's pretty much what you asked. The missing bits are for you to separate the MySQL commands from the shell commands, or to wrap them as an invocation of mysql:

mysql -D mysql ...

Also, consider placing the password in a suitably protected file or passing it as an argument to your script.

于 2013-06-18T06:12:55.533 回答