今天早上收到朋友的信息说他家的数据库被人干了,很是紧张,但是后面逐步排除,发现mysql安装目录下的数据文件都是正常,从linux历史执行命令日志来看也没有迹象表面是被人搞,同时mysql的错误log里面只有些不稳定的日志。最后搜索到两篇这样的文章,确定的确是权限这个问题导致的,但是为什么服务突然会有这种现象,目前还是查看中,同时整理下这两篇文章,希望能够帮到其他人。
参考的原文:
http://www.yebihai.com/kuangjiaxitong/linux/801.html
http://www.unixdo.com/DataBase/1909.html
[root@localhost ~]# mysql -uroot -p123
![](/Images/OutliningIndicators/None.gif)
Welcome to the MySQL monitor. Commands end with ; or \g.
![](/Images/OutliningIndicators/None.gif)
Your MySQL connection id is 7
![](/Images/OutliningIndicators/None.gif)
Server version: 5.0.77 Source distribution
![](/Images/OutliningIndicators/None.gif)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
![](/Images/OutliningIndicators/None.gif)
mysql>show databases;
![](/Images/OutliningIndicators/None.gif)
+--------------------+
![](/Images/OutliningIndicators/None.gif)
| Database |
![](/Images/OutliningIndicators/None.gif)
+--------------------+
![](/Images/OutliningIndicators/None.gif)
| information_schema |
![](/Images/OutliningIndicators/None.gif)
| test |
![](/Images/OutliningIndicators/None.gif)
+--------------------+
![](/Images/OutliningIndicators/None.gif)
只显示这个两个数据库,看不到mysql数据库
![](/Images/OutliningIndicators/None.gif)
解决方法:
![](/Images/OutliningIndicators/None.gif)
此问题实际上是用户没有权限:
![](/Images/OutliningIndicators/None.gif)
1. 关闭mysql,service mysqld stop
![](/Images/OutliningIndicators/None.gif)
2. 启动mysql: mysqld_safe --skip-grant-tables
![](/Images/OutliningIndicators/None.gif)
3. 再打开一个ssh连接服务器,进行mysql操作
![](/Images/OutliningIndicators/None.gif)
[root@localhost ~]#mysql
![](/Images/OutliningIndicators/None.gif)
Welcome to the MySQL monitor. Commands end with ; or \g.
![](/Images/OutliningIndicators/None.gif)
Your MySQL connection id is 7
![](/Images/OutliningIndicators/None.gif)
Server version: 5.0.77 Source distribution
![](/Images/OutliningIndicators/None.gif)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
![](/Images/OutliningIndicators/None.gif)
mysql>show databases;
![](/Images/OutliningIndicators/None.gif)
在这个模式下是可以看到mysql数据库的。
![](/Images/OutliningIndicators/None.gif)
在数据库名mysql下的user表中,修改相应权限,比如:
![](/Images/OutliningIndicators/None.gif)
INSERT INTO `user` VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0);
![](/Images/OutliningIndicators/None.gif)
如果已经存在了host为localhost的记录,则先删除该记录,delete from user where host='localhost';
![](/Images/OutliningIndicators/None.gif)
然后再进行INSERT INTO `user` VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0);
![](/Images/OutliningIndicators/None.gif)
操作。
![](/Images/OutliningIndicators/None.gif)
操作完成后,将两个ssh连接都关闭,然后再重新连接一个ssh,启动mysql,service mysqld start,然后用mysql命令连接mysql数据库
![](/Images/OutliningIndicators/None.gif)
[root@localhost ~]#mysql -uroot -p
![](/Images/OutliningIndicators/None.gif)
密码为空。
![](/Images/OutliningIndicators/None.gif)
[如果此时还连接不上,再重启一下mysql就好了,service mysqld restart]。
![](/Images/OutliningIndicators/None.gif)
然后就可以用 [root@localhost ~]#mysqladmin -uroot password 'newpassword' 来设置密码了。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
第二种情况
1.首先停止mysql服务:service mysqld stop
2.加参数启动mysql:/usr/bin/mysqld_safe --skip-grant-tables &
然后就可以无任何限制的访问mysql了
3.root用户登陆系统:mysql -u root -p mysql
4.切换数据库:use mysql
5.显示所有的表:show tables;
这里就可以访问表了
6.查看user表中root用户的localhost权限:select * from user where user='root' and host='localhost'
7.在显示的列表中显示:root用户的localhost的权限都是'N',表示root用户本地登陆不具有权限
8.修改root用户的localhost权限:
update user set
`Select_priv` = 'Y',
`Insert_priv` = 'Y',
`Update_priv` = 'Y',
`Delete_priv` = 'Y',
`Create_priv` = 'Y',
`Drop_priv` = 'Y',
`Reload_priv` = 'Y',
`Shutdown_priv` = 'Y',
`Process_priv` = 'Y',
`File_priv` = 'Y',
`Grant_priv` = 'Y',
`References_priv` = 'Y',
`Index_priv` = 'Y',
`Alter_priv` = 'Y',
`Show_db_priv` = 'Y',
`Super_priv` = 'Y',
`Create_tmp_table_priv` = 'Y',
`Lock_tables_priv` = 'Y',
`Execute_priv` = 'Y',
`Repl_slave_priv` = 'Y',
`Repl_client_priv` = 'Y',
`Create_view_priv` = 'Y',
`Show_view_priv` = 'Y',
`Create_routine_priv` = 'Y',
`Alter_routine_priv` = 'Y',
`Create_user_priv` = 'Y',
`Event_priv` = 'Y',
`Trigger_priv` = 'Y'
where user='root' and host='localhost' //这里需注意是否有这个条件的用户
9.更新一下:flush privileges;
10.然后重新启动下mysql,可以解决问题了