CentOS自帶mysql配置(密碼更改、端口開放訪問、添加進系統啓動項)

原文件地址:http://ducaijun.iteye.com/blog/1319666

前些天虛擬機安裝好了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: <輸入新設的密碼newpassword>

mysql>

 

第一種方法本人親測 好使!

 

方法二:

直接使用/etc/mysql/debian.cnf文件中[client]節提供的用戶名和密碼:

# mysql -udebian-sys-maint -p

Enter password: <輸入[client]節的密碼>

mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';

mysql> FLUSH PRIVILEGES;

mysql> quit

# mysql -uroot -p

Enter password: <輸入新設的密碼newpassword>

mysql>

 

方法三:

# mysql -uroot -p

Enter password: <輸入/etc/mysql/debian.cnf文件中[client]節提供的密碼>

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

端口開放訪問

 

更改完密碼,我就打算在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配置文件代碼  收藏代碼
  1. [root@localhost ~]# cat /etc/sysconfig/iptables  
  2. # Firewall configuration written by system-config-firewall  
  3. # Manual customization of this file is not recommended.  
  4. *filter  
  5. :INPUT ACCEPT [0:0]  
  6. :FORWARD ACCEPT [0:0]  
  7. :OUTPUT ACCEPT [0:0]  
  8. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  
  9. -A INPUT -p icmp -j ACCEPT  
  10. -A INPUT -i lo -j ACCEPT  
  11. -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT  
  12. -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT  
  13. -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT  
  14. -A INPUT -j REJECT --reject-with icmp-host-prohibited  
  15. -A FORWARD -j REJECT --reject-with icmp-host-prohibited  
  16. COMMIT  

 

 

然後  /etc/init.d/iptables restart 重啓iptables服務

        /etc/init.d/iptables status  查看iptables規則

 

 

Iptables規則代碼  收藏代碼
  1. [root@localhost ~]# /etc/init.d/iptables status  
  2. 表格:filter  
  3. Chain INPUT (policy ACCEPT)  
  4. num  target     prot opt source               destination  
  5. 1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED  
  6. 2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0  
  7. 3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0  
  8. 4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22  
  9. 5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:80  
  10. 6    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:3306  
  11. 7    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited  
  12.   
  13. Chain FORWARD (policy ACCEPT)  
  14. num  target     prot opt source               destination  
  15. 1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited  
  16.   
  17. Chain OUTPUT (policy ACCEPT)  
  18. 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跟隨系統一起啓動

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章