Ubuntu 18 : How to install MySQL server

By default Ubuntu 18 usually has the Apache2 server installed, but the MySQL server might not be there. We will require it for omgboards.com and other websites. So we’re going to install it.

ssh to the server, login with a user that can sudo up, and type from the command line:

sudo apt install mysql-server

Follow the on-screen instructions, accepting Y if it wants to install additional things.

Once it’s successfully done. You aren’t. You have to do some basics, such as set a password, remove the test database, and remove the guest user.

To configure the MySQL server the easiest way, type:

sudo mysql_secure_installation

Then follow the on-screen instructions carefully.

Please realise the mysql ‘root’ user is not the same as the server’s root user, and you should pick a completely different password.

floris@server:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.
Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password
and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.

New password: (pick something matching your policy)
Re-enter new password: (repeat it)

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!
floris@server:~$

I hope this information helps you quickly setup mysql server on your Ubuntu 18 server.

In the next blog post I will walk through adding a new database, and a new user to this database.

To check if MySQL installed properly, you can type:

which mysql
and
mysql --version

It should look something like this:

floris@server:~$ which mysql
/usr/bin/mysql
floris@server:~$ mysql --version
mysql Ver 14.14 Distrib 5.7.31, for Linux (x86_64) 
floris@server:~$

Please note that on Ubuntu 18 you probably don’t have to do this (Debian based), but just in case, you might as well type this to initialize the mysql data directory:

sudo mysqld --initialize

It will probably error like this, that’s fine. This just means it’s already there.

floris@server:~$ sudo mysqld --initialize
2020-08-17T07:55:29.266639Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2020-08-17T07:55:29.266708Z 0 [ERROR] Aborting
floris@server:~$

The last step before we (can) start adding using the MySQL server is to change the authentification type from auth_socket to msyql_native_password. To achieve this, connect to the mysql server.

Note: You will notice it will automatically log you in, that’s one of the things we wish to change.

sudo mysql

and then query the server to show how it’s setup:

SELECT user,authentication_string,plugin,host FROM mysql.user;

Output should look like this:

floris@server:~$ sudo mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | | auth_socket | localhost |
| mysql.session | *x | mysql_native_password | localhost |
| mysql.sys | *x | mysql_native_password | localhost |
| debian-sys-maint | *x | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

mysql>

Now run this query, and replace ‘password’ at the end with your mysql root password. If you pick a new pass here, it will change it of course.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'p4ssw0rd-change-me';

Then finally before typing exit, type:

FLUSH PRIVILEGES;

And you’re done, you can type this again to see if it changed:

SELECT user,authentication_string,plugin,host FROM mysql.user;

But you’re done, you can type exit now.

exit
mysql> exit
Bye
floris@server:~$

Instead of connecting to the MySQL server with just ‘mysql’, you now type this to connect (and don’t include the password, provide it when asked for)

mysql -u root -p

You’re now ready to add one or more databases, and then users to that database. But I will cover that in another blog post.

Note: If you try mysql from cli, it should error like this:

root@server:~# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
root@server:~#

 


Posted

in

by

Tags: