DEV Community

Harrsh Patel
Harrsh Patel

Posted on

Deploy MySQL in Ubuntu server

Create a new ssh key in your computer.
Add the pub key to the AWS key pair directory.
Create new Ubuntu server and add access to port 3306 to it.
Connect to server using the same key.

Run the following commands in the server.

sudo apt update
sudo apt upgrade -y
sudo reboot
Enter fullscreen mode Exit fullscreen mode

Open ports for UFW.

sudo ufw allow OpenSSH
sudo ufw allow 3306
sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

Install NGINX

sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

Check if NGNIX is working by accessing the public ipv4 address in the browser.

Allow NGINX for UFW.

sudo ufw app list
sudo ufw allow 'Nginx Full'
Enter fullscreen mode Exit fullscreen mode

Setup MySQL.

sudo apt install mysql-server
sudo mysql
Enter fullscreen mode Exit fullscreen mode

Exit if everything worked.

Install phpMyAdmin.

sudo apt install php-fpm php-mysql
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl
Enter fullscreen mode Exit fullscreen mode

Create new SQL user.

sudo mysql

mysql> CREATE USER '<user>'@'localhost' IDENTIFIED WITH caching_sha2_password BY '<password>';
mysql> GRANT ALL PRIVILEGES ON *.* TO '<user>'@'localhost' WITH GRANT OPTION;
mysql> exit
Enter fullscreen mode Exit fullscreen mode

Copy phpMyAdmin configuration to NGINX.

sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
sudo service nginx restart
sudo nano /var/www/html/index.php
Enter fullscreen mode Exit fullscreen mode

Add the following lines

<?php
phpinfo();
?>
Enter fullscreen mode Exit fullscreen mode
sudo service nginx restart
php -v
Enter fullscreen mode Exit fullscreen mode

Check the PHP version from here.

sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

Image description

Change the PHP version.

include fastcgi_params;
fastcgi_index index.php;
fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
Enter fullscreen mode Exit fullscreen mode
sudo service nginx restart
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

# Change the bind address to 0.0.0.0

sudo systemctl restart mysql
Enter fullscreen mode Exit fullscreen mode

Change host of the MySQL user.

sudo mysql
mysql> SELECT User, Host FROM mysql.user;
mysql> UPDATE mysql.user SET HOST='%' WHERE User='<user>';
mysql> SELECT User, Host FROM mysql.user;

# % is the wildcard to connect from everywhere.

mysql> exit
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
naucode profile image
Al - Naucode

Great article, keep the good work! Liked and followed! 🚀

Collapse
 
harrsh profile image
Harrsh Patel

Thank you so much ✌️