Installing MySQL on Ubuntu is a rite of passage for many budding server admins and software engineers. Almost every website needs a database on the server these days and MySQL is an excellent choice. This article covers how to install and setup MySQL server; we assume you only have little experience working with MySQL.
Let’s get started!
Install MySQL Server
Installing MySQL Server is exceedingly simple on Ubuntu; it is provided by the mysql-server package. If you look closely at the details of the package you will see that mysql-server is an empty package that depends on the mysql-server-5.5 package. Execute the following command and read the output to confirm for yourself:
sudo apt-cache show mysql-server
This structure allows people and other packages to refer to the general mysql-server package while letting the package maintainers determine which version of MysQL Server is the best in general. Let’s take a look and see if there any other versions of MySQL Server available to us:
sudo apt-cache search mysql-server
At the time this article was written, mysql-server-5.6 is also available in the repository. Therefore, we could install that package directly if we wanted mysql-server-5.6 instead.
With this information in mind, we are now ready to install MySQL Server on Ubuntu. Execute ONE of the following commands to start the installation.
1 2 3
sudo apt-get install mysql-server # Install current approved version of mysql-server sudo apt-get install mysql-server-5.5 # Install MySQL Server 5.5 sudo apt-get install mysql-server-5.6 # Install MySQL Server 5.6
Any of these commands will install a number of packages. In particular they will install mysql-client-x.x which will allow us to control the control MySQL Server from the command line via the mysql command.
During the installation process you will be asked for a root password. This password helps maintain the security and integrity of your data so make sure you choose something very difficult.
Secure the Installation
Once the install has completed, run the following command to have mysql run the secure installation wizard:
You will be asked a series of questions about your installation and you should take all of the recommendations provided by the wizard.
Create User and Database
If you already know the user and database you would like to create, you can execute the following couple of commands from the command line. First login to MySQL:
mysql -u root -p
Next, type the following commands while changing the appropriate values:
1 2 3 4
CREATE DATABASE exampledb; # Creates a database named exampledb CREATE USER 'exampleuser' IDENTIFIED BY 'examplepass'; # Creates exampleuser with password examplepass GRANT ALL PRIVILEGES ON exampledb.* TO 'exampleuser'; # Grants all privileges to exampleuser exit # Exits mysql console
Graphical User Interface Options
You can login in the mysql console and perform almost all mysql commands via the command line. If you prefer working with a GUI, there are many tools available that will allow you to connect to your Ubuntu server via a method like SSH and then interact directly with your MySQL Server. Some options include: MySQL Workbench (free), SQL Yog (paid) and Heidi SQL (free).
We hope you found this article useful. If you see any mistakes, missing features or ways to improve it, please let us know in the comments below so we can update its contents. If you're willing to link to us, we would sincerely appreciate it!