Making MySQL Accessible remotely

Wednesday, Jun 10, 2020 | 2 minute read | Updated at Wednesday, Jun 10, 2020

@
Making MySQL Accessible remotely

Os: Ubuntu Server 20.04

If you ever find yourself unable to log-in or connect to your newly created MySQL server, with errors such as these being thrown around,

- Host ''xxx.xx.xxx.xxx'' is not allowed to connect to this MySQL server
- Unknown authentication method
- Port closed

You have come to the right place

**Assumption: mysql server is already installed.

  1. Log into your server and connect to the DB using the root account. sudo mysql -u <user> -p <enter password at prompt>
  2. check for available account on the DB SELECT user,authentication_string,plugin,host FROM mysql.user;
  3. Create a new user if not present or alter existing user with the following commands
(creates user to connect via the internet & localhost)
- CREATE USER 'user'@'%' IDENTIFIED WITH mysql_native_password IDENTIFIED BY 'password';
- CREATE USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password IDENTIFIED BY 'password';
- GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
- GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' WITH GRANT OPTION;
- FLUSH PRIVILEGES;

or

(Alters existing users)
- ALTER USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- ALTER USER 'ruser'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
- GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
- GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' WITH GRANT OPTION;
- FLUSH PRIVILEGES;
  1. Exit mysql exit
  2. Enable firewall (ufw) and allow port 3306 through
ufw enable
ufw allow 3306

If you have a specific IP you will be connecting from (safest),

ufw enable
ufw allow from remote_IP_address to any port 3306
  1. Mysql must listen for an external IP address or all addresses(not safe). To enable this, open up your mysqld.cnf file:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
  1. Scroll down till you reach bind-address = 127.0.0.1 Edit 127.0.0.1 to specific IP mysql should listen for or to 0.0.0.0 for everyone(not safe)
  2. Uncomment #port = 3306 if it is commented.
  3. Save the document and restart mysql sudo systemctl restart mysql

Now you can connect to your DB, using your new user and the password via the internet.

© 2025 Reveries

🌱 Powered by Hugo with theme Dream.

About Me

Self Introduction

Passionate about people & technology. With a plethora of experience managing both people and technology. I am currently a Technical Officer with the Communications Division of CSIR - Institute for Scientific and Technological Information. Here, I am responsible for transforming research outputs into software, tools, equipments and communication systems as tools for national development.

I am BIG about community and have spent the better part of the last decade; growing and managing various tech communities in Ghana. Most recent being the Google Developer Group (GDG). I ASPIRE to INSPIRE.

I like to dabble in a lot of technology related things; from code to Rpi’s.