How To Install MySQL on Ubuntu 18.04

I can pretty much tell how you just want to quickly get a MySQL server up and running for usage, well, here we go:
Update current package data with:

$ sudo apt update

Now install the mysql server:

$ sudo apt install mysql-server

And to make sure the server is setup safe and securely, we run:

$ sudo mysql_secure_installation

.
For sudo mysql_secure_installation You will be prompted to provide your preferred values for some options, and/or just press Enter to go with whatever value is default.

What these do is help to secure your database with removing things like anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.

Extras:

  • Adjusting User Authentication and Privileges
    1. Access the mysql command line by typing in: sudo mysql
    1. To list all users and their authentication methods we would use the SELECT keyword as such: SELECT user,authentication_string,plugin,host FROM mysql.user;

Output should approximate to something like this:

Output
+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
    1. Next to change a password for a user we simply provide a new password along the command line like this: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; (Change root and password to your desired user and password)
    1. Now we go ahead and run FLUSH PRIVILEGES; (Writing all changes to the database for usage.) and exit (To exit the mysql ommand line)
  • Adding a new user:

    1. Depending if you’ve done the step above, you may need to type in sudo mysql -u root -p to login with said root password, otherwise all thats needed is: sudo mysql
    1. While inside, to create a new user, we use CREATE USER 'newUsername'@'localhost' IDENTIFIED BY 'newPassword'; (Replacing newUsername and newPassword with the preffered user and password.)
    1. Now we assign it privileges so that the new user can access the database: GRANT ALL PRIVILEGES ON *.* TO 'newUsername '@'localhost' WITH GRANT OPTION; (Again replacing newUsername with your preferred username.)
    1. Now because we didn’t use statements like: INSERT , UPDATE , or DELETE there is no need to use FLUSH PRIVILEGES;

There you go :slight_smile:

  • You have successfully setup, secured, and optionally added a new user to your MySQL database.