Solve the problem of remote access to the mysql database on the linux end of the virtual machine _ MySQL

Source: Internet
Author: User
Solve the problem of remote access to bitsCN.com from the mysql database on linux.

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

The day before yesterday, I was bored when I installed a mysql instance on CentOS in the virtual machine. I suddenly wanted to use jdbc to connect to the database and use java to operate the database, but I couldn't connect to it. the specific situation is:

1. ping 192.168.16.128 successfully

2. telnet 192.168.16.128 3306 access is denied on port 3306. the CentOS firewall Shields Port 3306.

Modification method:

Switch to the root user

Open the iptables configuration file vi/etc/sysconfig/iptables.

Note the following when modifying the CentOS firewall: you must leave yourself with a path to the VNC and an SSh management port.

The content is as follows:

# Firewall configuration written by system-config-securitylevel

# Manual customization of this file is not recommended.

* 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

-An RH-Firewall-1-INPUT-p 50-j ACCEPT

-An 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-prohibited

COMMIT

It can be seen that my firewall only opens port 22, so we add Port 3306

To modify the CentOS firewall, you must modify the 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: service iptables restart after all the modifications.

You can verify whether all 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.

However, we still cannot access mysql because mysql does not authorize remote users.

For example, you want root2 to use root2 to connect to the mysql server from any host.

Switch to mysql

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 whose ip address is 192.168.1.6, and use mypassword as the password

Mysql> grant all privileges on *. * TO 'myuser' @ '192. 168.1.3 'identified BY 'mypassword' with grant option;

Then execute:

Mysql> flush rivileges;

Mysql> exit;

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 ~~~

BitsCN.com

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.