This tutorial will introduce how to install, configure and manage MySQL on a Ubuntu Linux 16.04 LTS (Xenial Xerus). The latest version of MySQL database server is 5.7 and can be installed using the apt-get command or apt command:
- mysql-server – Metapackage depending on the latest version (server)
- mysql-client – Metapackage depending on the latest version (client)
Step 1: Update your system by typing the following commands:
$ sudo apt update
$ sudo apt upgrade
Step 2: Install mysql version 5.7 on Ubuntu 16.04
Type the following command:
$ sudo apt install mysql-server mysql-client
Sample outputs:
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18 libterm-readkey-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7 Suggested packages: libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl libipc-sharedcache-perl mailx tinyca The following NEW packages will be installed: libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18 libterm-readkey-perl mysql-client mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7 0 upgraded, 12 newly installed, 0 to remove and 0 not upgraded. Need to get 21.7 MB of archives. After this operation, 155 MB of additional disk space will be used. Do you want to continue? [Y/n] y
You need to type the password for the MySQL root user:
Step 3: Run mysql_secure_installation to secure your installation
For all production server you must run the following command:
$ sudo mysql_secure_installation
Sample outputs:
The mysql_secure_installation command will change the root password, disable and remove anonymous users from MySQL server, turn off the MySQL root user login remotely, delete test database, and reload privilege tables on the system. If you’ve completed all of the steps and answer yes to these questions. Your MySQL installation should now be secure.
Step 4: How do I use MySQL server?
To log in as the root user, type:
$ mysql -u root -p
When prompted, enter the MySQL root password, and you should see mysql> prompt as follows:
Step 5: How do I create a new MySQL server database and user account?
The sql syntax is as follows to create a new mysql database and setup password:
CREATE DATABASE DATABASE-NAME-HERE; GRANT ALL ON DATABASE-NAME-HERE.* TO 'DATABASE-USERNAME-HERE' IDENTIFIED BY 'DATABASE-PASSWORD-HERE'; |
For example, create a wpblogdb as the database, vivek as the user, and fooBarPwd8-4_2 as the user’s password, run (type at the mysql> prompt:)
mysql> create database wpblogdb;
mysql> grant all on wpblogdb.* to 'vivek' identified by 'fooBarPwd8-4_2';
mysql> quit;
Sample session:
Now, try to log in as vivek user, enter:
$ mysql -u USERNAME -p DB-NAME-HERE
$ mysql -u vivek -p wpblogdb
Sample session:
Of course you can create a mysql tables and insert data too:
mysql> CREATE TABLE authors (id INT, name VARCHAR(20), email VARCHAR(20));
mysql> INSERT INTO authors (id,name,email) VALUES(1,"Vivek","xuz@foo.gmail.com");
mysql> INSERT INTO authors (id,name,email) VALUES(2,"Wendy","bar@foo.gmail.com");
mysql> INSERT INTO authors (id,name,email) VALUES(3,"Tom","tom@foo.gmail.com");
mysql> SELECT * FROM authors;
mysql> quit;
Sample outputs:
How do I start MySQL server?
$ sudo systemctl start mysql
OR
$ sudo systemctl start mysql.service
How do I stop MySQL server?
$ sudo systemctl stop mysql
OR
$ sudo systemctl stop mysql.service
How do I restart MySQL server?
$ sudo systemctl restart mysql
OR
$ sudo systemctl restart mysql.service
How do I find out if MySQL running/active?
$ sudo systemctl status mysql.service
Sample outputs:
â— mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enab
Active: active (running) since Mon 2016-03-28 14:20:54 CDT; 8s ago
Process: 24181 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exi
Process: 24176 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exite
Main PID: 24180 (mysqld_safe)
Tasks: 23 (limit: 512)
Memory: 106.1M
CPU: 514ms
CGroup: /system.slice/mysql.service
├─24180 /bin/sh /usr/bin/mysqld_safe
└─24528 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plug
Mar 28 14:20:53 xenial systemd[1]: Starting MySQL Community Server...
Mar 28 14:20:53 xenial mysqld_safe[24180]: 160328 14:20:53 mysqld_safe Can't log t
Mar 28 14:20:53 xenial mysqld_safe[24180]: 160328 14:20:53 mysqld_safe Logging to
Mar 28 14:20:53 xenial mysqld_safe[24180]: 160328 14:20:53 mysqld_safe Starting my
Mar 28 14:20:54 xenial systemd[1]: Started MySQL Community Server.How do I reset the mysql root account password?
You need to type the following command, if you would like to change the MySQL root password:
$ sudo dpkg-reconfigure mysql-server-5.7
See “Recover the MySQL root Password” for more information.
A note about MySQL server configuration
You may edit the /etc/mysql/my.cnf file to configure the basic settings such as TCP/IP port, IP address binding, and other options. However, The MySQL database server configuration file on the Ubuntu 16.04 LTS is located at /etc/mysql/mysql.conf.d/mysqld.cnf and one can edit using a text editor such as vi or nano:
$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
OR
$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
After making a change to /etc/mysql/mysql.conf.d/mysqld.cnf the MySQL server will need to be restarted:
$ sudo systemctl restart mysql.service
And, there you have it, the MySQL database version 5.7 installed and working correctly on the latest version of Ubuntu Linux 16.04 LTS. For more information see MySQL 5.7 Reference Manual.



19 comment
Don’t install vanilla Oracle MySQL, and don’t install 5.6, install MariaDB 10.1 which is combination of 5.6 and 5.7. It sports new engines for special purposes in addition to TokuDB, an improved InnoDB.
Hey
Your post is now outdated, MySQL default version now is 5.7 in Ubuntu 16.04, but there is still 5.6 version available.
No. I checked it again:
There is no 5.7 version. Here is a search result.
Anyone got an idea on how to change the datadir in MySQL?
The old way doesn’t seem to work (even though I changed the data path in the apparmor config).
Thanks, you saved my day.
How do i configure mysql service for low RAM usage, right now it is taking around 800 MB RAM. Please help.
I have a problem. Didn’t open the place that I need to type the password for the MySQL root user. How can I fix this?
Thanks! Very clear!
It works for me ;)
:~$ sudo apt install mysql-server
Reading package lists… Done
Building dependency tree
Reading state information… Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
mysql-server : Depends: mysql-community-server (= 5.7.12-1ubuntu15.10) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
Sorry, This is false advertising. I came here to get help installing 5.6 not 5.7 and certainly not mariadb (ever tried going back?).
IMO 16.04 desktop maybe great, as a server it sucks.
Silent install script –
I am having mysql 5.7 on my linux server Ubuntu16.04 . i am installing Atutor (a learning Management system ) on my server .Every thing is fine but mysql 5.7 is find disabled or not found on it how can i fix this issue Please guide me i am in trouble.
Thank you very much! very good tutorial was of great help.
Wow ,this is what i am looking for very useful thanks
i finishing Run mysql_secure_installation to secure your installation then next it shows
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!
nisith@nisith-inspiron-3521:~$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user ‘root’@’localhost’
nisith@nisith-inspiron-3521:~$
Very very Thanks vivek.
Thanks for this posting. It saved my life. I was struggling with a wrong installation of mysql and this article made it so easy for me!
Many thanks for the tutorial.
May I add:
*** DO NOT USE JUST NUMERICS FOR A PASSWORD ***.
Yes I know I am shouting but a numeric password is accepted but does not work as I learned to my detriment. I just spent quite a few hours trying to find a solution.
Followed a link to get 5.6 and find an article on mysql 5.7 which I am more than capable of doing on my own just struggling with 5.6 as this is required by the business, next time can you correctly advertise or have your links accurately labelled or linking
I have tried so many links none of them rectify my error but this link successfully rectify thank you so much and make a great job