前些天虚拟机安装好了CentOS6.1,但是自己想远程连接自带的mysql发现不知道如何改密码,于是谷歌一下,把结果记录下来,方便后期自己使用:
方法一:
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
# /etc/init.d/mysql restart
# mysql -uroot -p
Enter password:
mysql>
第一种方法本人亲测 好使!
方法二:
直接使用/etc/mysql/debian.cnf文件中[client]节提供的用户名和密码:
# mysql -udebian-sys-maint -p
Enter password:
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
# mysql -uroot -p
Enter password:
mysql>
方法三:
# mysql -uroot -p
Enter password:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
端口开放访问
更改完密码,我就打算在windows机器下连接虚拟机中安装的mysql,发现连接不上,在linux上查看了mysql服务都启动了,3306端口也是开放的,后来在windows机器上telnet3306端口发现telnet不上,于是才想起来,安装的时候防火墙是开启的,于是就再iptables规则中增加如下两行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT(允许80端口通过防火墙)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT(允许3306端口通过防火墙)
位置不要加错了,放在-A INPUT -j REJECT --reject-with icmp-host-prohibited和-A FORWARD -j REJECT --reject-with icmp-host-prohibited前面
我的/etc/sysconfig/iptables文件配置如下:
Iptables配置文件代码
[root@localhost ~]# cat /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport22-j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport80-j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport3306-j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT[root@localhost ~]# cat /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
然后 /etc/init.d/iptables restart重启iptables服务
/etc/init.d/iptables status 查看iptables规则
Iptables规则代码
[root@localhost ~]# /etc/init.d/iptables status
表格:filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1ACCEPT all --0.0.0.0/00.0.0.0/0state RELATED,ESTABLISHED
2ACCEPT icmp --0.0.0.0/00.0.0.0/0
3ACCEPT all --0.0.0.0/00.0.0.0/0
4ACCEPT tcp --0.0.0.0/00.0.0.0/0state NEW tcp dpt:22
5ACCEPT tcp --0.0.0.0/00.0.0.0/0state NEW tcp dpt:80
6ACCEPT tcp --0.0.0.0/00.0.0.0/0state NEW tcp dpt:3306
7REJECT all --0.0.0.0/00.0.0.0/0reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1REJECT all --0.0.0.0/00.0.0.0/0reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination[root@localhost ~]# /etc/init.d/iptables status
表格:filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80
6 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3306
7 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
如果后面要安装tomcat的话,不换端口的话 还是在/etc/sysconfig/iptables文件中增加一行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
然后,在windows机器上telnet虚拟机ip的3306端口,发现现在可以到达了
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
添加进系统启动项
#chkconfig --list|grep mysql 查看列表中是否有mysql服务项
有的话是这样的:
[root@localhost ~]# chkconfig --list|grep mysql
mysqld 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
现在全部都是关闭,及在任意状态下都不随系统启动,现在用chkconfig --level 3 mysqld on 更改启动状态
[root@localhost ~]# chkconfig --list|grep mysql
mysqld 0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:关闭 6:关闭
现在的状态是在 3状态时mysql随系统一起启动!
特别说明:Linux系统不同于windows,Linux是有运行级别只说的,Linux共有7个运行级别,如下:
0:关机。
1:单用户字符界面。
2:不具备网络文件系统(NFS)功能的多用户字符界面。
3:具有网络功能的多用户字符界面。
4: 保留不用。
5:具有网络功能的图形用户界面。
6:重新启动系统。
所以上面提到的数字 3 即linux运行在级别3 具有网络功能的多用户字符界面 级别 此时mysql跟随系统一起启动