Wednesday 26 April 2017

Install MySQL on Ubuntu 16.04


#1 
On Ubuntu 16.04, only the latest version of MySQL is included in the APT package repository by default. Mean time that's MySQL 5.7
To install it, simply update the package index on your server and install the default package with
apt-get.
#sudo apt-get update
#sudo apt-get install mysql-server
#root@mayank-VirtualBox:~# sudo apt-get update
Hit:1 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Get:3 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]    
Get:5 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]  
Get:6 http://security.ubuntu.com/ubuntu xenial-security/main i386 DEP-11 Metadata [54.6 kB]
Get:7 http://security.ubuntu.com/ubuntu xenial-security/main DEP-11 64x64 Icons [42.4 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/universe i386 DEP-11 Metadata [32.2 kB]
Get:9 http://security.ubuntu.com/ubuntu xenial-security/universe DEP-11 64x64 Icons [37.0 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu xenial-updates/main i386 Packages [500 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu xenial-updates/main i386 DEP-11 Metadata [289 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu xenial-updates/main DEP-11 64x64 Icons [191 kB]
Get:13 http://in.archive.ubuntu.com/ubuntu xenial-updates/universe i386 Packages [443 kB]
Get:14 http://in.archive.ubuntu.com/ubuntu xenial-updates/universe i386 DEP-11 Metadata [159 kB]
Get:15 http://in.archive.ubuntu.com/ubuntu xenial-updates/universe DEP-11 64x64 Icons [181 kB]
Get:16 http://in.archive.ubuntu.com/ubuntu xenial-updates/multiverse i386 Packages [7,720 B]
Get:17 http://in.archive.ubuntu.com/ubuntu xenial-updates/multiverse i386 DEP-11 Metadata [2,520 B]
Get:18 http://in.archive.ubuntu.com/ubuntu xenial-backports/main i386 DEP-11 Metadata [3,324 B]
Fetched 2,249 kB in 10s (209 kB/s)                                             
AppStream cache update completed, but some metadata was ignored due to errors.
Reading package lists... Done

#root@mayank-VirtualBox:~# sudo apt-get install mysql-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libaio1 libevent-core-2.0-5 libhtml-template-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common
  mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
  libipc-sharedcache-perl mailx tinyca
The following NEW packages will be installed:
  libaio1 libevent-core-2.0-5 libhtml-template-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common
  mysql-server mysql-server-5.7 mysql-server-core-5.7
0 upgraded, 9 newly installed, 0 to remove and 449 not upgraded.
Need to get 18.1 MB of archives.
After this operation, 157 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu xenial-updates/main i386 mysql-common all 5.7.17-0ubuntu0.16.04.2 [15.7 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu xenial/main i386 libaio1 i386 0.3.110-2 [6,646 B]
Get:3 http://in.archive.ubuntu.com/ubuntu xenial-updates/main i386 mysql-client-core-5.7 i386 5.7.17-0ubuntu0.16.04.2 [6,104 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu xenial-updates/main i386 mysql-client-5.7 i386 5.7.17-0ubuntu0.16.04.2 [1,739 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu xenial-updates/main i386 mysql-server-core-5.7 i386 5.7.17-0ubuntu0.16.04.2 [7,513 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu xenial-updates/main i386 libevent-core-2.0-5 i386 2.0.21-stable-2ubuntu0.16.04.1 [75.7 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu xenial-updates/main i386 mysql-server-5.7 i386 5.7.17-0ubuntu0.16.04.2 [2,528 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu xenial/main i386 libhtml-template-perl all 2.95-2 [60.4 kB]   
Get:9 http://in.archive.ubuntu.com/ubuntu xenial-updates/main i386 mysql-server all 5.7.17-0ubuntu0.16.04.2 [10.8 kB]
Fetched 18.1 MB in 55s (324 kB/s)                                                                       
Preconfiguring packages ...
Selecting previously unselected package mysql-common.
(Reading database ... 174568 files and directories currently installed.)
Preparing to unpack .../mysql-common_5.7.17-0ubuntu0.16.04.2_all.deb ...
Unpacking mysql-common (5.7.17-0ubuntu0.16.04.2) ...
Selecting previously unselected package libaio1:i386.
Preparing to unpack .../libaio1_0.3.110-2_i386.deb ...
Unpacking libaio1:i386 (0.3.110-2) ...
Selecting previously unselected package mysql-client-core-5.7.
Preparing to unpack .../mysql-client-core-5.7_5.7.17-0ubuntu0.16.04.2_i386.deb ...
Unpacking mysql-client-core-5.7 (5.7.17-0ubuntu0.16.04.2) ...
Selecting previously unselected package mysql-client-5.7.
Preparing to unpack .../mysql-client-5.7_5.7.17-0ubuntu0.16.04.2_i386.deb ...
Unpacking mysql-client-5.7 (5.7.17-0ubuntu0.16.04.2) ...
Selecting previously unselected package mysql-server-core-5.7.
Preparing to unpack .../mysql-server-core-5.7_5.7.17-0ubuntu0.16.04.2_i386.deb ...
Unpacking mysql-server-core-5.7 (5.7.17-0ubuntu0.16.04.2) ...
Selecting previously unselected package libevent-core-2.0-5:i386.
Preparing to unpack .../libevent-core-2.0-5_2.0.21-stable-2ubuntu0.16.04.1_i386.deb ...
Unpacking libevent-core-2.0-5:i386 (2.0.21-stable-2ubuntu0.16.04.1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up mysql-common (5.7.17-0ubuntu0.16.04.2) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Selecting previously unselected package mysql-server-5.7.
(Reading database ... 174737 files and directories currently installed.)
Preparing to unpack .../mysql-server-5.7_5.7.17-0ubuntu0.16.04.2_i386.deb ...
Unpacking mysql-server-5.7 (5.7.17-0ubuntu0.16.04.2) ...
Selecting previously unselected package libhtml-template-perl.
Preparing to unpack .../libhtml-template-perl_2.95-2_all.deb ...
Unpacking libhtml-template-perl (2.95-2) ...
Selecting previously unselected package mysql-server.
Preparing to unpack .../mysql-server_5.7.17-0ubuntu0.16.04.2_all.deb ...
Unpacking mysql-server (5.7.17-0ubuntu0.16.04.2) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu4) ...
Setting up libaio1:i386 (0.3.110-2) ...
Setting up mysql-client-core-5.7 (5.7.17-0ubuntu0.16.04.2) ...
Setting up mysql-client-5.7 (5.7.17-0ubuntu0.16.04.2) ...
Setting up mysql-server-core-5.7 (5.7.17-0ubuntu0.16.04.2) ...
Setting up libevent-core-2.0-5:i386 (2.0.21-stable-2ubuntu0.16.04.1) ...
Setting up mysql-server-5.7 (5.7.17-0ubuntu0.16.04.2) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
Setting up libhtml-template-perl (2.95-2) ...
Setting up mysql-server (5.7.17-0ubuntu0.16.04.2) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu4) ...
Prompt to create a root password during the installation. Choose a secure one and make sure you remember it,
because you'll need it later.

#2 Configuring MySQL

For fresh installations, you'll want to run the included security script. This changes some of the less secure
default options for things like remote root logins and sample users. On older versions of MySQL, you needed to initialize
the data directory manually as well,but this is done automatically now.
Run the security script.
#sudo mysql_secure_installation
This will prompt you for the root password you created in Step 1. You can press Y and then ENTER to accept the defaults for all the subsequent questions, with the exception of the one that asks if you'd like to change the root password. You just set it in Step 1, so you don't have to change it now. To initialize the MySQL data directory, you would use
mysql_install_db for versions before 5.7.6, and mysqld --initialize for 5.7.6 and later. However, if you installed MySQL
from the Debian distribution, like in Step 1, the data directory was initialized automatically; you don't have to do anything.
If you try running the command anyway, you'll see the following error:
#root@mayank-VirtualBox:~# sudo mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
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: 0
Using existing password for root.
Estimated strength of the password: 25
Change the password for root ? ((Press y|Y for Yes, any other key for No) : k
... skipping.
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) : n
... skipping.
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!

#3 Testing MySQL

Regardless of how you installed it, MySQL should have started running automatically. To test this, check its status.
systemctl status mysql.service
#root@mayank-VirtualBox:~# systemctl status mysql.service
mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2017-04-14 20:30:43 IST; 9min ago
Main PID: 16438 (mysqld)
Tasks: 28 (limit:512)
CGroup:/system.slice/mysql.service

└─16438 /usr/sbin/mysqld
mayank-VirtualBox systemd[1]: Starting MySQL Community Server...
mayank-VirtualBox systemd[1]: Started MySQL Community Server.
If MySQL isn't running, you can start it with sudo systemctl
mysql start.
For an additional check, you can try connecting to the database using the mysqladmin tool,
which is a client that lets you run administrative commands. For example, this
command says to connect to MySQL as root (-u root), prompt for a password (-p), and return the version.
mysqladmin -p -u root version
#root@mayank-VirtualBox:~# systemctl status mysql.service
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2017-04-14 20:30:43 IST; 9min ago
Main PID: 16438 (mysqld)
Tasks: 28 (limit:512)
CGroup:/system.slice/mysql.service

└─16438 /usr/sbin/mysqld
#mayank-VirtualBox systemd[1]: Starting MySQL Community Server...
mayank-VirtualBox systemd[1]: Started MySQL Community Server.
#root@mayank-VirtualBox:~# mysql -u root -p

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.17-0ubuntu0.16.04.2 (Ubuntu)
Copyright (c) 2000,
2016, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>Exit

No comments:

Post a Comment