Installing relational databases with MySQL
In this recipe, we will learn how to install and configure the MySQL database on an Ubuntu server.
Getting ready
You will need access to a root account or an account with sudo
privileges.
Make sure that the MySQL default port 3306
is available and not blocked by any firewall.
How to do it…
Follow these steps to install the relational database MySQL:
- To install the MySQL server, use the following command:
$ sudo apt-get update $ sudo apt-get install mysql-server-5.7
The installation process will download the necessary packages and then prompt you to enter a password for the MySQL root account. Choose a strong password:
- Once the installation process is complete, you can check the server status with the following command. It should return an output similar to the following:
$ sudo service mysql status mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service Active: active (running) since Tue 2016-05-10 05:
- Next, create a copy of the original configuration file:
$ cd /etc/mysql/mysql.conf.d $ sudo cp mysqld.cnf mysqld.cnf.bkp
- Set MySQL to listen for a connection from network hosts. Open the configuration file
/etc/mysql/mysql.conf.d/mysqld.cnf
and changebind-address
under the[mysqld]
section to your server’s IP address:$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf bind-address = 10.0.2.6
Note
For MySQL 5.5 and 5.6, the configuration file can be found at
/etc/mysql/my.cnf
- Optionally, you can change the default port used by the MySQL server. Find the
[mysqld]
section in the configuration file and change the value of theport
variable as follows:port = 30356
Make sure that the selected port is available and open under firewall.
- Save the changes to the configuration file and restart the MySQL server:
$ sudo service mysql restart
- Now open a connection to the server using the MySQL client. Enter the password when prompted:
$ mysql -u root -p
- To get a list of available commands, type
\h
:mysql> \h
How it works…
MySQL is a default database server available in Ubuntu. If you are installing the Ubuntu server, you can choose MySQL to be installed by default as part of the LAMP stack. In this recipe, we have installed the latest production release of MySQL (5.7) from the Ubuntu package repository. Ubuntu 16.04 contains MySQL 5.7, whereas Ubuntu 14.04 defaults to MySQL version 5.5.
If you prefer to use an older version on Ubuntu 16, then use following command:
$ sudo add-apt-repository ‘deb http://archive.ubuntu.com/ubuntu trusty universe’ $ sudo apt-get update $ sudo apt-get install mysql-server-5.6
After installation, configure the MySQL server to listen for connections from external hosts. Make sure that you open your database installation to trusted networks such as your private network. Making it available on the Internet will open your database to attackers.
There’s more…
Securing MySQL installation
MySQL provides a simple script to configure basic settings related to security. Execute this script before using your server in production:
$ mysql_secure_installation
This command will start a basic security check, starting with changing the root
password. If you have not set a strong password for the root account, you can do it now. Other settings include disabling remote access to the root account and removing anonymous users and unused databases.
MySQL is popularly used with PHP. You can easily install PHP drivers for MySQL with the following command:
$ sudo apt-get install php7.0-mysql
See also
- The Ubuntu server guide mysql page at https://help.ubuntu.com/14.04/serverguide/mysql.html