Solve the Problem of remote access to the mysql database on linux.

Source: Internet
Author: User

Solve the problem that the virtual machine linux mysql database could not be remotely accessed the day before yesterday, boring to install a mysql on the CentOS in the virtual machine, and suddenly wanted to use jdbc connection to operate the database in java, but it could not be connected, details: 1. ping 192.168.16.128 successfully 2. telnet 192.168.16.128 3306 rejects access to port 3306. The CentOS firewall shields port 3306 and switches to the root user to open the iptables configuration file: vi/etc/sysconfig/iptables when modifying the CentOS firewall, note: You must leave yourself with the following information: # Firewall configuration written by system-config-securitylevel # Manual customization of this file is not recommend Ed. * filter: input accept [0: 0]: forward accept [0: 0]: output accept [0: 0]: RH-Firewall-1-INPUT-[0: 0]-a input-j RH-Firewall-1-INPUT-A FORWARD-j RH-Firewall-1-INPUT-A RH-Firewall-1-INPUT-I lo-j ACCEPT-A RH-Firewall-1-INPUT-p icmp-type any-j ACCEPT-A RH-Firewall-1-INPUT-p 50-j ACCEPT-A RH-Firewall-1-INPUT-p 51-j ACCEPT-A RH-Firewall-1-INPUT-m state-state ESTABLISHED, RELATED-j ACCEPT- A RH-Firewall-1-INPUT-m state-state NEW-m tcp-p tcp-dport 22-j ACCEPT-A RH-Firewall-1-INPUT-j REJECT-reject-with icmp-host-prohibitedCOMMIT visible my firewall is only on port 22, therefore, we need to add Port 3306 to modify the CentOS firewall. You must modify this file based on your server. For example, if you do not want to open port 80 to provide web services, delete the line accordingly: -A RH-Firewall-1-INPUT-m state-state NEW-m tcp-p tcp-dport 80-j ACCEPT we add-A RH-Firewall-1-INPUT-m state-state NEW-m tcp-p tcp-dport 3306-j ACCEPT restart iptables after all modifications: service iptables restart: You can verify whether all the rules have taken effect: iptables-L. In this way, the settings of the CentOS firewall are modified. In this case, telnet 192.168.16.128 3306 is successful, but we still cannot access it because mysql does not authorize a remote user. For example, if you want root2 to use root2 to connect to the mysql server from any host. Switch to mysql> grant all privileges on *. * TO 'root2' @ '%' identified by 'root2' with grant option; or if you want TO allow myuser TO connect TO the mysql server from a host WITH the ip address 192.168.1.6, use mypassword as the password mysql> grant all privileges on *. * TO 'myuser' @ '192. 168.1.3 'identified BY 'mypassword' with grant option; then run mysql> flush rivileges; mysql> exit to make the modification take effect. in this case, mysql-h 192.168.16.128-u root2-p enters the password root2 remotely ~ Connect with jdbc ~ Success ~ Sahua ~~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.