How Do I Enable TCP Connections to MySQL?

Note

If you use the Network Security Toolkit Web User Interface (NST WUI) to setup MySQL, you will have a simple check box which allows you to choose whether or not to enable TCP connections and what systems are permitted to connect.

By default, the MySQL server will accept connection on port 3306. You can verify this using the simple telnet command as shown below:


[pkb@localhost ~]$ telnet 192.168.0.65 3306
Trying 192.168.0.65...
Connected to 192.168.0.65 (192.168.0.65).
Escape character is '^]'.
(
3.23.58!kyAvOi@,^A^]closeConnection closed by foreign host.
[pkb@localhost ~]$ 

Even though MySQL permits one to make TCP connections, it won't actually allow anyone to log in. For example, if I try to connect remotely using mysql, I get the following message:


[pkb@localhost ~]$ mysql -u root -h 192.168.0.65 -D mysql -p
ERROR 1130 (00000): Host '192.168.0.58' is not allowed to connect to this MySQL server
[pkb@localhost ~]$ 

By default, MySQL won't allow ANY users access to any of the databases if they connect over a TCP connection. In order to permit the connection, you must create a entry in the user table of the mysql database (make sure you select the PASSWORD function to encrypt your password). In particular, the Host field needs to indicate which host(s) are permitted to connect. If you specify % (which I would not recommend), then a user would be able to connect from any host.

Figure 6.4. The user Table After Permitting 192.168.0.58 Access

The user Table After Permitting 192.168.0.58 Access

After creating the new user, you will need to restart the MySQL server. Once it has been restarted, you should be able to connect as demonstrated below:


[pkb@localhost ~]$ mysql -u root -h 192.168.0.65 -D mysql -p
Enter password:PASSWORD (not echoed)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18 to server version: 3.23.58

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> select User,Host from user;
+------+--------------+
| User | Host         |
+------+--------------+
| root | 192.168.0.58 |
|      | localhost    |
| root | localhost    |
|      | probe        |
| root | probe        |
+------+--------------+
5 rows in set (0.01 sec)

mysql> quit
Bye
[pkb@localhost ~]$