In this article, we are going to demonstrate how to install MySQL 5.7 on Ubuntu 16.04 using command line.
MySQL is the world’s most popular open source relational database management system. It is widely used with web server like apache2,Nginx,IIS,etc.
MySQL has client/Server architecture , supports InnoDB storage engine,can be installed on various operating system like Ubuntu,Debian,CentOS,Windows.,etc.
Step 1: Add MySQL APT Repository in Ubuntu
Ubuntu comes with default package repositories. So,if we want to add/install latest repositories then we have to add/install package repositories.
Below are commands to add/download latest MySQL APT repository using command line,
$ sudo wget http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb
--2019-03-23 17:31:39-- http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb
Resolving repo.mysql.com (repo.mysql.com)… 184.108.40.206
Connecting to repo.mysql.com (repo.mysql.com)|220.127.116.11|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 35950 (35K) [application/x-debian-package]
Saving to: ‘mysql-apt-config_0.8.9-1_all.deb’
mysql-apt-config_0.8.9-1_all.deb 100%[===================================================================================>] 35.11K --.-KB/s in 0.01s
2019-03-23 17:31:40 (2.40 MB/s) - ‘mysql-apt-config_0.8.9-1_all.deb’ saved [35950/35950]
then, below command is to install above downloaded apt repository,
$ sudo dpkg -i mysql-apt-config_0.8.9-1_all.deb
when we run above command like below prompt will open, click on Ok.
Step 2: Update MySQL Repository and Install MySQL 5.7 on Ubuntu
Using above commands , we have downloaded apt repository and installed.
Next we have to update apt repository and install MySQL 5.7 using below commands,
$ sudo apt-get update
$ sudo apt-get install mysql-server
Now, Installation process will prompt default password for root user and again same password.
Step 3: Secure MySQL Installation
MySQL Server comes with a script mysql_secure_installation this can do multiple security related operations,
Run the below script on command prompt, asks below options.
Securing the MySQL server deployment.
Enter password for user root:
Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: No
Change the password for root ? ((Press y|Y for Yes, any other key for No) : No
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Step 4: Login to MySQL
Now, Login to MySQL 5.7 Server using below command and use password to login entered during installation.
$ mysql -u root -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Now, We have successfully installed MySQL 5.7 Database on your Ubuntu server.
Step 5: Create MySQL Remote User
First, Login to MySQL Server with root user using command line, Below is command is to create user , here i am creating user “fosstechnix“.
mysql> CREATE USER 'fosstechnix'@'%' IDENTIFIED BY '[email protected]';
Next, assign the privileges to database with below command , here i am assigning all databases privileges to user fosstechnix,
If you want to assign privileges to specific database replace ” .” with database name.
mysql> GRANT ALL PRIVILEGES ON * . * TO 'fosstechnix'@'%';
To take effect reload the privileges using below command,
mysql> FLUSH PRIVILEGES;
Step 6: How to Enable MySQL Remote Access in Ubuntu 16.04
By default, In MySQL database server remote access is disabled for security reason.
To enable remote connections of MySQL Server, we have to change bind-address in MySQL configuration file.
Open the /etc/mysql/mysql.conf.d/mysqld.cnf file
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
Below the [mysqld] section find the Line,
bind-address = 127.0.0.1
And replace it to
bind-address = 0.0.0.0
Restart the MySQL Server to take effect.
$ sudo systemctl restart mysql.service
Step 7: Start/Restart/Stop MySQL Server Using Command Line
Below are commands to start/restart/stop MySQL 5.7 on Ubuntu 16.04
$ sudo systemctl start mysql.service
$ sudo systemctl restart mysql.service
$ sudo systemctl stop mysql.service