# mysql server installation and configuration on Ubuntu Server 22.04

*Reference titles:*

- *https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-22-04*
- https://techglimpse.com/error-grant-identified-by-password/



1. Update ubuntu server first:

   ```console
   sudo apt update
   ```

2. Then upgrade ubuntu server:

   ```console
   sudo apt upgrade
   ```

3. Install mysql server:

   ```console
   sudo apt install mysql-server
   ```

4. Start mysql service:

   ```console
   sudo systemctl start mysql
   ```

​		Type `systemctl status mysql` in the console to check mysql server running status.

​		If mysql server is installed and started correctly. It would be looked like this:

​		![image-20230627172104628](doc/readme/img/install_and_configure_mysql_server_on_ubuntu/image-20230627172104628.png)

5. Start mysql initial configuration:

   ```console
   sudo mysql_secure_installation
   ```

   Do it step by step following the prompt.

6. Grant specific ip to access this mysql server.

   Enter mysql console:

   ```console
   sudo mysql -u root -p
   ```

   Create root user for specific ip:

   ```console
   CREATE USER 'root'@'your-specific-ip' IDENTIFIED WITH mysql_native_password BY 'your-password';
   ```

   Grant specific ip to access mysql:

   ```console
   GRANT ALL PRIVILEGES ON *.* TO 'root'@'your-specific-ip' WITH GRANT OPTION;
   ```

   ```console
   FLUSH PRIVILEGES;
   ```

   ```console
   exit;
   ```

   Since mysql 5.7.6, `GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your-password' WITH GRANT OPTION` is [deprecated](https://techglimpse.com/error-grant-identified-by-password/). If you try with that, you'll get errors like this:

      ```console
      ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY '123456' WITH GRANT OPTION' at line 1
      ```

   So don't try `GRANT ALL xxx`, just do it as mentioned above (CREATE USER and GRANT).

6. Configure your mysql server to allow dedicated IPs to access.

   First, backup mysql server configuration file:
   
   ```console
   sudo cp /etc/mysql/mysql.conf.d/mysqld.conf /etc/mysql/mysql.conf.d/mysqld.conf.bak
   ```
   
   Second, edit the configuration file to allow remote access. Open mysqld.conf and edit:
   
   ```console
   sudo vim  /etc/mysql/mysql.conf.d/mysqld.conf
   ```
   
   Change bind-address from 127.0.0.1 to 0.0.0.0:

​		![image-20230627183522646](doc/readme/img/mysql_server_installation_and_configuration_on_Ubuntu_Server_22.04/image-20230627183522646.png)

​	   Save the file and quit (via vim wq).

7. Restart mysql service:

   ```console
   sudo systemctl restart mysql
   ```

8. Configure mysql server to allow remote access from specific ip.

9. For security reasons, **NEVER** allow any unknown ip address to access your server. Enable and configure your firewall to allow dedicated IPs to access mysql serivice on this server:

   ```console
   sudo ufw enable
   sudo ufw allow from <your-specific-ip> to any port 3306
   sudo ufw reload
   sudo ufw status
   ```


​		If everything is configured correctly, you'll see things like this:

​		![image-20230627182439999](doc/readme/img/mysql_server_installation_and_configuration_on_Ubuntu_Server_22.04/image-20230627182439999.png)

10. Now you can test the connection to mysql server through Navicat:

    ![image-20230627185346672](doc/readme/img/mysql_server_installation_and_configuration_on_Ubuntu_Server_22.04/image-20230627185346672.png)