Data Base Management
MYSQL
Getting ready
$ vagrant up $ vagrant ssh
How to do it
From the man pages mysql,it the MySQL command-line tool.
Install and configure the MariaDB
$ sudo yum groupinstall mariadb
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing for group install "MariaDB Database Server":
mariadb-server x86_64 1:5.5.47-1.el7_2 updates 11 M
Installing for dependencies:
libaio x86_64 0.3.109-13.el7 base 24 k
mariadb x86_64 1:5.5.47-1.el7_2 updates 8.9 M
perl-Compress-Raw-Bzip2 x86_64 2.061-3.el7 base 32 k
perl-Compress-Raw-Zlib x86_64 1:2.061-4.el7 base 57 k
perl-DBD-MySQL x86_64 4.023-5.el7 base 140 k
perl-DBI x86_64 1.627-4.el7 base 802 k
perl-IO-Compress noarch 2.061-2.el7 base 260 k
perl-Net-Daemon noarch 0.48-5.el7 base 51 k
perl-PlRPC noarch 0.2020-14.el7 base 36 k
Updating for dependencies:
mariadb-libs x86_64 1:5.5.47-1.el7_2 updates 755 k
Transaction Summary
================================================================================
Install 1 Package (+9 Dependent packages)
Upgrade ( 1 Dependent package)
Total download size: 22 M
Is this ok [y/d/N]:
$ sudo yum groupinstall mariadb-client
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing for group install "MariaDB Database Client":
MySQL-python x86_64 1.2.3-11.el7 base 82 k
mysql-connector-odbc x86_64 5.2.5-6.el7 base 146 k
Installing for dependencies:
libtool-ltdl x86_64 2.4.2-21.el7_2 updates 49 k
unixODBC x86_64 2.3.1-11.el7 base 413 k
Transaction Summary
================================================================================
Install 2 Packages (+2 Dependent packages)
Total download size: 689 k
Installed size: 1.9 M
Is this ok [y/d/N]:
Now configure the mariadb
$ sudo systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
$ sudo systemctl start mariadb
$ sudo systemctl status mariadb
Configure firewall
$ sudo firewall-cmd --add-service=mysql --permanent
success
$ sudo firewall-cmd --add-port=3360 --permanent
success
$ sudo firewall-cmd --reload
$ sudo ss -tulpn |grep mysql
tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=5914,fd=14))
Set password mariadb for root secure
$ sudo mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): Enter
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password: <New Password>
Re-enter new password: <New Password>
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] 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? [Y/n] y
... Success!
By default, MariaDB 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? [Y/n] 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? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
Now enable skip-networking
$ sudo vim /etc/my.cnf
[mysqld]
skip-networking=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
~
:wq
$ sudo systemctl restart mariadb
$ sudo ss -tulpn |grep mysql
Query Command in MariaDB
$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.47-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
Check user
MariaDB [(none)]> select USER();
+----------------+
| USER() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
Create database
MariaDB [(none)]> create database corporate;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| corporate |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> use corporate
Database changed
MariaDB [corporate]>
Create user For local host
MariaDB [(none)]> create user mahesh@localhost identified by 'postroll';
Query OK, 0 rows affected (0.00 sec)
Create user For any host
MariaDB [(none)]> create user ranjan@'%' identified by 'postrack';
Query OK, 0 rows affected (0.00 sec)
For select with corporate
MariaDB [(none)]> grant select on corporate.* to ranjan@'%';
Query OK, 0 rows affected (0.00 sec)
For select,insert,update,delete with corporate
MariaDB [(none)]> grant select,insert,update,delete on corporate.* to mahesh@localhost;
Query OK, 0 rows affected (0.00 sec)
How we open the user
$ mysql -h localhost -u mahesh -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.47-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select USER();
+------------------+
| USER() |
+------------------+
| mahesh@localhost |
+------------------+
1 row in set (0.00 sec)
Creating and Restoring table in MariaDB
MariaDB [corporate]> create table company(name varchar(50),age int(3),email varchar(50),contact int(12));
Query OK, 0 rows affected (0.04 sec)
MariaDB [corporate]> show tables;
+---------------------+
| Tables_in_corporate |
+---------------------+
| company |
+---------------------+
1 row in set (0.00 sec)
MariaDB [corporate]> describe company;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(50) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| email | varchar(50) | YES | | NULL | |
| contact | int(12) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
Now insert privilege
MariaDB [corporate]> insert into company(name) values('shyam');
Query OK, 1 row affected (0.03 sec)
MariaDB [corporate]> select * from company;
+-------+------+-------+---------+
| name | age | email | contact |
+-------+------+-------+---------+
| shyam | NULL | NULL | NULL |
+-------+------+-------+---------+
1 row in set (0.00 sec)
MariaDB [corporate]> insert into company(name,age,email,contact) values('shyam','24','[email protected]','9876543210');
Query OK, 1 row affected,(0.00 sec)
MariaDB [corporate]> select * from company;
+-------+------+----------------+------------+
| name | age | email | contact |
+-------+------+----------------+------------+
| shyam | NULL | NULL | NULL |
| shyam | 24 | [email protected] | 2147483647 |
+-------+------+----------------+------------+
2 rows in set (0.00 sec)
Alter the table
MariaDB [corporate]> alter table company add (gender char(8));
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [corporate]> describe company;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(50) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| email | varchar(50) | YES | | NULL | |
| contact | int(12) | YES | | NULL | |
| gender | char(8) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
Now dump the file
$ mysqldump -u root -p corporate >mydump.mdb
Enter password:
$ ls
mydump.mdb
For all database
$ mysqldump -u root -p --all-databases >mariadb.mdb
Enter password:
$ ls
mariadb.mdb mydump.mdb
Now restore the backup file in database
$ mysqldump -u root -p company1 <mydump.mdb
Enter password:
-- MySQL dump 10.14 Distrib 5.5.47-MariaDB, for Linux (x86_64)
--
-- Host: localhost Database: student
-- ------------------------------------------------------
-- Server version 5.5.47-MariaDB
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2016-05-31 6:01:16
Another way to restore backup
$ mysql -u root -p company1 < mydump.mdb
Enter password:
$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 5.5.47-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use company1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [company1]> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| company |
+-------------------+
1 row in set (0.00 sec)