1.首先确认服务器出于安全的状态,也就是没有人可以任意地链接MySQL数据库。mysql
由于在从新设置MySQL的root密码的期间,MySQL数据库彻底出于没有密码保护的状态下,其余的用户也能够任意地登陆和修改MySQL的信息。能够采用将MySQL对外的端口封闭,而且中止Apache以及全部的用户进程的方法实现服务器的准安全状态。最安全的状态是到服务器的Console上面操做,而且拔掉网线。sql
2.修改MySQL的登陆设置:数据库
1
|
vim /etc/my.cnf
|
在[mysqld]的段中加上一句:skip-grant-tablesvim
例如:安全
1
2
3
4
|
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-
grant
-tables
|
保存而且退出vi。服务器
3.从新启动mysqldsocket
1
2
3
|
service mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
|
4.登陆并修改MySQL的root密码tcp
mysqlui
1
2
3
4
5
6
7
8
9
10
11
|
Welcome
to
the MySQL monitor. Commands
end
with
;
or
\g.
Your MySQL
connection
id
is
3
to
server version: 3.23.56
Type ‘help;
' or ‘\h'
for
help. Type ‘\c
' to clear the buffer.
mysql> USE mysql ;
Database changed
mysql> UPDATE user SET Password = password ( '
new-
password
' ) WHERE User = '
root' ;
Query OK, 0
rows
affected (0.00 sec)
Rows
matched: 2 Changed: 0 Warnings: 0
mysql> flush
privileges
;
Query OK, 0
rows
affected (0.01 sec)
mysql> quit
|
5.将MySQL的登陆设置修改回来spa
1
|
vim /etc/my.cnf
|
将刚才在[mysqld]的段中加上的skip-grant-tables删除
保存而且退出vim
6.从新启动mysqld
1
2
3
|
service mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
|
容许远程链接
1
|
GRANT
ALL
PRIVILEGES
ON
.
TO
root@
'%'
IDENTIFIED
BY
‘your
password
';
|
%表示多有机器。
打开3306端口,为防火墙设置例外,放行3306.
打开iptables的配置文件:
1
|
vi /etc/sysconfig/iptables
|
在中间添加一行
1
|
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
|
所有修改完以后重启iptables:
1
|
service iptables restart
|