MySQL remote connection [not as usual]

前端 未结 4 475
温柔的废话
温柔的废话 2021-02-06 11:20

I\'m not getting to get access to mysql externally. I think it\'s mysql or firewall stuff or some privilege within the mysql.

I already tried doing steps that are on int

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-06 11:55

    Resumed solution:

    I killed the mysqld that was holding the 3306 port and restarted it.

    I think this is a bug or something related check step-by-step what I did:

    1- Created a program to connect on 3306

    First I create a simple program in Java to make sure there wasn't any problem with my firewall. The program just open a port on 3306 with TCP connection by 2 minutes just for testing, the program:

    import java.io.IOException;
    import java.net.ServerSocket;
    
    public class PortMysqlTest {
        public static void main(String[] args) {
            int port = 3306;
    
            ServerSocket ss = null;
            try
            {
                ss = new ServerSocket(port);
                ss.setReuseAddress(true);
            }
            catch (IOException e)
            {
                System.out.println(e.getMessage());
            }
    
            long futureTime = System.currentTimeMillis() + 1000 * 60 * 2;
            while (System.currentTimeMillis() < futureTime)
            {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
            if (ss != null)
            {
                try
                {
                    ss.close();
                }
                catch (IOException e)
                {
                    System.out.println(e.getMessage());
                }
            }       
        }
    

    2- Stopped the mysql service

    Before executing the program I stopped the mysql service:

    sudo service mysql stop
    

    or it can be done with:

    /etc/init.d/mysql stop
    

    3- Started My program with java PortMysqlTest

    My program (PortMysqlTest) throw an exception saying the address/port as already in use by mysqld!

    Surprise? As far as I know the with mysql service stopped the port should be free. Am I right? (I'm not sure of it if someone can answer this...)

    4- I searched for the application PID that was using the address/port with:

    sudo netstat -lpn | grep 3306
    

    the answer:

    tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      6736/mysqld
    

    5- I killed the process with:

    kill -9 6736
    

    OBS, if you don't know what kill do: 6736 is the process that was holding the port, I got this number with step 4, 6736/mysqld

    6- Now I run my program again:

    java PortMysqlTest and connected with telnet (telnet 66.123.173.170 3306) externaly and it worked!

    So the problem wasn't the firewall, because I could connect externally to the machine 66.123.173.170.

    7- Started the mysql service again:

    (I waited 2 minutes for my program do stop and free the 3306 port) and started the mysql service again to test, with:

    sudo service mysql start 
    

    or

    sudo /etc/init.d/mysql start
    

    8- I did connected externally with telnet:

    telnet 66.123.173.170 3306
    

    and worked!!!

    After I tried connect to mysql with:

    mysql -h 66.123.173.170 -u root -p 
    

    and worked!!!

    Conclusion: I think there was a bug with my MySql installation; or something, (I don't know what is), when restarting mysql the mysql doesn't get the configuration of:

    bind-address = 0.0.0.0 
    

    or

    bind-address = * 
    

    I hope this help someone else.

提交回复
热议问题