grant remote access of MySQL database from any IP address

后端 未结 21 1284
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 09:18

I am aware of this command:

GRANT ALL PRIVILEGES
ON database.*
TO \'user\'@\'yourremotehost\'
IDENTIFIED BY \'newpassword\';

But then it on

21条回答
  •  伪装坚强ぢ
    2020-11-22 09:46

    To be able to connect with your user from any IP address, do the following:

    Allow mysql server to accept remote connections. For this open mysqld.conf file:

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

    Search for the line starting with "bind-address" and set it's value to 0.0.0.0

    bind-address                    = 0.0.0.0
    

    and finally save the file.

    Note: If you’re running MySQL 8+, the bind-address directive will not be in the mysqld.cnf file by default. In this case, add the directive to the bottom of the file /etc/mysql/mysql.conf.d/mysqld.cnf.

    Now restart the mysql server, either with systemd or use the older service command. This depends on your operating system:

    sudo systemctl restart mysql # for ubuntu    
    sudo systemctl restart mysqld.service # for debian
    

    Finally, mysql server is now able to accept remote connections.

    Now we need to create a user and grant it permission, so we can be able to login with this user remotely.

    Connect to MySQL database as root, or any other user with root privilege.

    mysql -u root -p
    

    now create desired user in both localhost and '%' wildcard and grant permissions on all DB's as such .

    CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypass';
    CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypass';
    

    Then,

    GRANT ALL ON *.* TO 'myuser'@'localhost';
    GRANT ALL ON *.* TO 'myuser'@'%';
    

    And finally don't forget to flush privileges

    FLUSH PRIVILEGES;
    

    Note: If you’ve configured a firewall on your database server, you will also need to open port 3306 MySQL’s default port to allow traffic to MySQL.

    Hope this helps ;)

提交回复
热议问题