MySQL: How to allow remote connection to mysql

后端 未结 16 1127
北海茫月
北海茫月 2020-11-21 06:15

I have installed MySQL Community Edition 5.5 on my local machine and I want to allow remote connections so that I can connect from external source.

How can I do that

相关标签:
16条回答
  • 2020-11-21 06:54

    Enabling remote root access can be dangerous. It would be preferable if you were to set up user accounts with more restrictive permissions. The following three steps should do it.

    1. Ensure that the line starting with bind-address ... is at least commented out in your my.ini or my.cnf file. If it doesn't exist, move on. You can find this file in C:\ProgramData\MySQL\MySQL Server 8.0 on Windows.

    2. Afterwards, check that the user account you are establishing the connection with does not have localhost in the Limit to Hosts Matching field. While it isn't recommended, you can instead put % in that field for testing purposes. You can do this by opening a local connection to the server with MySQL Workbench, then going to Server>Users and Privileges from the menu bar and finding the user account you want to connect with.

    The "Limit to Hosts Matching" field is what disallows you to connect non-locally. I.e. it limits the accepted connections to a pattern of IP addresses. Ideally, you should be accessing the MySQL server from a static IP address or subnet, so that you can be as restrictive as possible.

    1. Obviously, your firewall should allow the MySQL Server application to communicate over the port you want. The physical networking equipment in between you and your server should allow communication on the port you want to connect with. (port 3306 typically)
    0 讨论(0)
  • 2020-11-21 06:55

    Just F.Y.I I pulled my hair out with this problem for hours.. finally I call my hosting provider and found that in my case using a cloud server that in the control panel for 1and1 they have a secondary firewall that you have to clone and add port 3306. Once added I got straight in..

    0 讨论(0)
  • 2020-11-21 06:56

    After doing all of above I still couldn't login as root remotely, but Telnetting to port 3306 confirmed that MySQL was accepting connections.

    I started looking at the users in MySQL and noticed there were multiple root users with different passwords.

    select user, host, password from mysql.user;
    

    So in MySQL I set all the passwords for root again and I could finally log in remotely as root.

    use mysql;
    update user set password=PASSWORD('NEWPASSWORD') where User='root';
    flush privileges;
    
    0 讨论(0)
  • 2020-11-21 06:57

    If mysqld has a bind address set to a loopback/local address (e.g. 127.0.0.1), the server will not be reachable from remote hosts, because a loopback interface cannot be reached from any remote host.

    Set this option to 0.0.0.0 (:: for IPv4+6) to accept connections from any host, or to another externally-reachable address if you want to only allow connections on one interface.

    Source

    0 讨论(0)
  • 2020-11-21 06:57

    I had to this challenge when working on a Java Project with MySQL server as the database.

    Here's how I did it:

    First, modify your confirm that your MySQL server configuration to allow for remote connections. Use your preferred text editor to open the MySQL server configuration file:

    sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
    

    Scroll down to the bind-address line and ensure that is either commented out or replaced with 0.0.0.0 (to allow all remote connections) or replaced with Ip-Addresses that you want remote connections from.

    Once you make the necessary changes, save and exit the configuration file. Apply the changes made to the MySQL config file by restarting the MySQL service:

    sudo systemctl restart mysql
    

    Next, log into the MySQL server console on the server it was installed:

    mysql -u root -p
    

    Enter your mysql user password

    Check the hosts that the user you want has access to already. In my case the user is root:

    SELECT host FROM mysql.user WHERE user = "root";
    

    This gave me this output:

    +-----------+
    | host      |
    +-----------+
    | localhost |
    +-----------+
    

    Next, I ran the command below to grant the root user remote access to the database named my_database:

    USE my_database;
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'my-password';
    

    Note: % grants a user remote access from all hosts on a network. You can specify the Ip-Address of the individual hosts that you want to grant the user access from using the command - GRANT ALL PRIVILEGES ON *.* TO 'root'@'Ip-Address' IDENTIFIED BY 'my-password';

    Afterwhich I checked the hosts that the user now has access to. In my case the user is root:

    SELECT host FROM mysql.user WHERE user = "root";
    

    This gave me this output:

    +-----------+
    | host      |
    +-----------+
    | %         |
    | localhost |
    +-----------+
    

    Finally, you can try connecting to the MySQL server from another server using the command:

    mysql -u username -h mysql-server-ip-address -p
    

    Where u represents user, h represents mysql-server-ip-address and p represents password. So in my case it was:

    mysql -u root -h 34.69.261.158 -p
    

    Enter your mysql user password

    You should get this output depending on your MySQL server version:

    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.7.31 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> 
    

    Resources: How to Allow Remote Connections to MySQL

    That's all.

    I hope this helps

    0 讨论(0)
  • 2020-11-21 07:00

    Just a note from my experience, you can find configuration file under this path /etc/mysql/mysql.conf.d/mysqld.cnf.

    (I struggled for some time to find this path)

    0 讨论(0)
提交回复
热议问题