Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

How to install mysql in linux with practical

 

Summary

In the previous tutorial we have learned about the introductionof mysql and advantages of mysql . In this article we will learn how to install mysql in linux operating system like centos ,oracle,redhat and ubuntu.

Prerequisite

To install mysql in linux system first we need to have a system with linux operating system. Now you can disable selinux as per your requirement and then configure yum repository to install mysql package from centralized yum repository. By default  mysql runs on port no 3306. You can configure firewall on linux system as per your requirement.

Install mysql in linux

Now we have configured yum repository. First we need to verify that whether mysql server package is installed or not on the server using the below command.

#rpm –qa mysql*

If package is installed on the server you will get the output and you have nothing to do. If this package is not installed the server you need to install mysql package using you repository. Upto linux version 6.x mysql package is available in the yum repository . After 6.x or 7.x onward we can use mysql repository to install mysql server package.

To search mysql package it is available or not and which version will be available we can execute the below command.

#yum provides mysql*

Note:  Mysql* means we can get all mysql related package from the yum repository.

 To install mysql server in linux system(rhel,centos,oracle)  we need to execute the below command.

#yum install mysql-server

How to install mysql in linux
How to install mysql in linux


Start mysql service in linux

After installation of mysql server we need to start the service and check the service status  using the below commands.

[root@mysqldailytasks ~]# service mysqld start

Starting mysqld:                                           [  OK  ]

[root@mysqldailytasks ~]# service mysqld status

mysqld (pid  1547) is running...


To enable mysqld service at the startup mode i.e After reboot the physical server mysqld service will be automatically using the below command.

 

#chkconfig mysqld on

 

How to secure and set mysql root password

After completion above all steps we need to secure mysql root password using the below command.

 [root@mysqldailytasks ~]# mysql_secure_installation 


NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL

      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

 

 

In order to log into MySQL to secure it, we'll need the current

password for the root user.  If you've just installed MySQL, 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):

OK, successfully used password, moving on...

 

Setting the root password ensures that nobody can log into the MySQL

root user without the proper authorisation.

 

You already have a root password set, so you can safely answer 'n'.

 

Change the root password? [Y/n] y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

 ... Success!

 

 

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? [Y/n] n

 ... skipping.

 

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] 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? [Y/n] n

 ... skipping.

 

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 MySQL

installation should now be secure.

 

Thanks for using MySQL!



How to connect mysql server from linux terminal

To connect mysql server from linux shell we need to execute the below command.

Mysql –u root –p

Enter password:


Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 5.1.71 Source distribution

 

Copyright (c) 2000, 2013, 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> 


Check mysql version

To check mysql version we can execute the below query in mysql promt.

mysql> select version();

+-----------+

| version() |

+-----------+

| 5.1.71    |

+-----------+

1 row in set (0.00 sec)

  

Conclusion

 In this article we have learned  how to install mysql in linux operating system like centos ,oracle,redhat and ubuntu,how to start the mysql service,how to secure root password for mysql,how to connect mysql from linux shell and finally how to check version of mysql server.

Introduction of Mysql and its features

 

Summary

In this tutorial we will learn aboutintroduction of mysql. Also we will learn about what is database,type of database, what is mysql, advantages or we can sayfeatures of mysql.


What is database

Database is collection of datas which will stored in the organized manners in database and it will be easily accessed by database admin.

Lets take an example to explain database. Suppose in a school there will be many students and each student has name , age ,roll no so school authority collects each and every record from every student and store all datas into a database. Therefore we can access each and every students data in organized manner and if required we can view ,modify and delete the particular data from the database.


Type of database

Now we have learned what is database. Now we will learn type of database. There is two types of database one is relational database and other is no sql database.
Relational database will stored the data in structure manner and it will be easy to understand and easy to access. Relational database usesSQL (structured query languages). Example of the  relational  is oracle,mysql,postgresql.

Introduction of Mysql
Introduction of MySql


No SQL database will not stored data in structured manner . It will stored data in document format. It will no used SQL language.It can not be maintain any relation between table like relational database. Example of noSQL database is mongodb.
 

What is  Mysql

MySQL is free and open source most popular and well known relational database management system all over the world. It is used for various web based software application.Mysql can be integrated with php,java and many others programming languages.Mysql has cross platform feature means we can use / install mysql server in windows,linux and mac operating system.
 

Advantage of Mysql

  1. Mysql is a free and open source database . so that we can download mysql free.
  2.  Mysql is relational database management system  because in one mysql database there will be many tables and each table has a relation with the other table.
  3.  Mysql supports cross platform because we can use mysql in  the various operating system like Windows , linux and mac operating system.
  4.  Mysql stores data in the structured manner using sql language.
  5.  Mysql is very fast, reliable and scalable database . mysql can handle small site as well as large sites due to its scalability.
  6.  Mysql supports many programming languages like php,java and so on.
  7.  Mysql is very secure database.
  8.  Mysql database is used by many big companies like google,facebook etc.
  9.  Mysql supports roll back and commit operation.
  10.  Moreover mysql is easy to learn.

 

Conclusion

In this tutorial we have learned about introduction of mysql. Also we have learned about what is database,type of database, what is mysql, advantage or we can sayfeatures of mysql.

In next article we will learn how to install mysql in linux. 


Difference between OLTP vs OLAP in database

 

OLTP vs OLAP in database


In this tutorial we will learn  the concept of OLTP  vs  OLAP in database. Every dba should know the concept of OLTP and OLAP.

OLTP vs OLAP


What does OLTP stand for:-

OLTP stands for online transaction processing. Normally we create OLTP database for ongoing transaction environment and OLTP database is used for day to day transaction activity for business need. As day to day transaction activities are happening therefore data always up-to-date.OLTP database stores current data for day to day activity.  Database size of OLTP database normally calculated in GB.OLTP database is handled by large no of people .


We can found details data in OLTP database for business requirements.Datas in the OLTP database found in normalized manner as many small tables are linked with each other using join function.OLTP database normally used by DBA.Normally OLTP database supports read and write both operation .


OLTP Example:- 

Any e-commerce sites use OLTP database for day to day business transaction.


What does OLAP stand for :-

OLAP stands online analytical processing.Normally we create OLAP database for create report as well as dashboard for decision making and trending  analysis. It stores historical summarised data to create report and dashboard. Data comes from OLTP database for that data is not frquently updated and data stores in OLAP database in denormalized Mannar.OLAP Database normally used by MD ,CEO for decision making purpose.

OLAP database normally use to analysis data to make bussiness decision so that OLAP database stores historical data to analysis data for results which is necessary to make decisions.Database size of OLAP database normally calculated in TB as large no of records is stored into OLAP database .OLAP database is handled by few no of people .Normally OLAP databse supports only read operation. 

OLAP Example:- 

If  we need any report for decision making for past 15 years we need OLAP database to make  dashboard using OLAP summarised data.


In this tutorial we have learned the concept of OLTP  vs  OLAP in database.


How to take backup of MySQL database using mysqldump utility and restore MySQL databsse from backup on linux system


Backup Mysql Database & Restore Mysql Database | In this tutorial we learn how to take backup of mysql database and import mysql dump on the linux system. Backup and restoration job is very important for a Database Administrator.

Backup MySQL database using mysqldump utility

To backup MySQL database we have to select the database name which database we want to take backup.After selecting the database we need to execute the below command to take backup of MySQL database.
This backup is the logical backup which is stored the data inside the linux filesystem.

Lets take an example to demonstrate how to take backup mysql database along with mysqldump example.

In my case we select “techrideradmin” database and there is one table named “student” inside the database. We want to take backup the “techrideradmin” database. After taking the backup we delete the table which is student and then restore the table using the backup file which is stored inside the linux file system.

[root@localhost ~]#  mysqldump -u root -p techrideradmin > /backup/backup-$(date +%F).sql
Enter password:

We can list the backup file which we have taken using the below command.

[root@localhost ~]# cd /backup/
[root@localhost backup]# ls
backup-2019-01-15.sql
How to take backup of MySQL database using mysqldump utility and restore MySQL backup on linux system,backup mysql database,import mysql dump
How to take backup of MySQL database using mysqldump utility and restore MySQL backup on linux system 
After enter the password backup will be completed. In the above command –u define which user is taking backup or which user has permission taking the backup and –p define the password of the user.

Restore MySQL database from the backup file

To restore the database using import mysql dump we need to create the MySQL database. After creating the database we need to execute the below command to restore the database from  the backup file.
[root@localhost ~]# mysql -u root -p techrideradmin < /backup/backup-2019-01-15.sql
Enter password:

After executing the above command we can restore the techrideradmin database smoothly.

That’s all. If this tutorial is helpful to know about Backup Mysql Database and import mysql dump please share it!

How to configure MySQL Master-Slave replication setup on Centos/RHEL/Oracle server step by step guide


In this article we learn how to configure MySQL Master Slave replication setup on linux.For this we have two server one is master server and other is slave server. We have to install MySQL on both node.

Master server ip address==========192.168.137.4
Slave server ip address===========192.168.137.3

Configuration Master server for Master-Slave replication

To configure Master server for Master slave configuration first we need to open the my.cnf file and add the below lines into the files.

server-id = 1
log-bin = /var/lib/mysql/mysql-bin
binlog-do-db=techrideradmin

From the above lines you need to change the bolded database name as per your requirement.
To take effect the config file we need to restart the mysql service using the below command.

[root@masterdb ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

After that we need to configure slave user and grant the permission for replication using the below command through mysql promt.

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave_techuser'@'%' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

After lock the database to pevent the new changes using the below command.

mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

To know the file name and position we need to execute the below command which is required to configure slave part.

mysql> SHOW MASTER STATUS;
+------------------+----------+----------------+------------------+
| File             | Position | Binlog_Do_DB   | Binlog_Ignore_DB |
+------------------+----------+----------------+------------------+
| mysql-bin.000001 |      106 | techrideradmin |                  |
+------------------+----------+----------------+------------------+
1 row in set (0.00 sec)

After that take backup of master database using the below command.

[root@masterdb ~]# mysqldump -u root -p techrideradmin > /backup/techrideradmin.sql
Enter password:

After taking backup we need to unlock table using the below command.

mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye

Copy the backup dump to the slave server using the below command.

[root@masterdb ~]# scp /backup/techrideradmin.sql root@192.168.137.3:/slave

Configuration Master server for Master-Slave replication

Open the mysql configuration file which is located at “/etc/my.cnf” and add the below lines.
server-id=2
master-host=192.168.137.4
master-connect-retry=30
master-user=slave_techuser
master-password=password
replicate-do-db=techrideradmin
log-bin = /var/lib/mysql/mysql-bin

To take effect the config file we need to restart the mysql service using the below command.

[root@masterdb ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

Now login the MySQL server and create techrideradmin database and restore the database using previous backup file.

mysql> create database techrideradmin;
Query OK, 1 row affected (0.00 sec)

Stop the slave using the below command.

mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

Now restore the dumped file using the below command.

[root@slavedb ~]# mysql -u root -p techrideradmin < /slave/techrideradmin.sql
Enter password:

Then execute the below command to get details from the master server.

mysql> CHANGE MASTER TO MASTER_HOST='192.168.137.4', MASTER_USER='slave_techuser', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=106;
Query OK, 0 rows affected (0.11 sec)

Then execute the below command to start slave.

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

Then check the status of slave using the below command.

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.137.4
                  Master_User: slave_techuser
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 106
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: techrideradmin
How to configure MySQL Master-Slave replication setup on Centos/RHEL/Oracle server step by step guide
How to configure MySQL Master-Slave replication setup on Centos/RHEL/Oracle server step by step guide 
That’s all. Master-Slave replication has been configured successfully if this aticle is helpful please share it!!!


Please Donate To Bitcoin Address: [[address]]
Donation of [[value]] BTC Received. Thank You.
[[error]]

How to Delete value from the mysql table step by step guide for beginner


In this article we learn how to delete value from the mysql table using delete query in sql. In an organization it is required to delete the date from the mysql table .

Delete data from the mysql table using mysql promt

To delete data from the mysql table using delete query we use”DELETE FROM”command as below.

Lets take an example to delete the date which we previously created.

Before delete to view the table data using the below command.

mysql> select * from student;
+----------------+------------------+-----------------+
| studentnrollno | studentfirstname | studentlastname |
+----------------+------------------+-----------------+
|              1 | Raktim           | Ghosh           |
+----------------+------------------+-----------------+
1 row in set (0.00 sec)

Now we are going to delete the data of the student whose roll no is 1. To do this we use the below command.

mysql> DELETE FROM student WHERE studentnrollno=1;
Query OK, 1 row affected (0.00 sec)

After deleted the date we need to view the data using the below command.

mysql> select * from student;
Empty set (0.01 sec)
How to Delete value from the mysql table step by step guide for beginner
How to Delete value from the mysql table step by step guide for beginner

Delete data from mysql table using php script

To delete the data using php script we use mysql_query function as below.

To connect to mysql server we use mysql_connect function and to select the database we will use mysql_select_db function (if dbname variable not defined).All functions are important to run delete query in sql using php script. 


To delete data from mysql table we need to pass two arguments inside the mysql_query function. 

Lets take an example to delete the data using php script

<html>
   <head>
      <title>Delete value into MySQL table on linux system</title>
   </head>
   <body>
      <?php
         $mysql_host = 'localhost';
         $mysql_user = 'root';
         $mysql_pass = 'passwordofroot';
         $dbname = "techrideradmin";
         $conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass,$dbname);
        
         if(! $conn ) {
            die('Could not connect the server: ' . mysql_error());
         }
         echo 'Connection successfully established';
         $sql = “DELETE FROM student WHERE studentrollno=1;”;
         $result = mysql_query( $sql, $conn );
        
         if(! $result ) {
            die('value not deleted: ' . mysql_error());
         }
         echo "Value deleted succcessfully\n";
         mysql_close($conn);
      ?>
   </body>
</html>


That’s all.If this article is helpful please share it!!!!


How to Update value in the mysql table step by step guide for beginner


Update Query in Sql to update value in Mysql table | In this article we learn how to update value in the mysql table using update query in sql. In an organization it is required to change or modify the date frequently into the table.

Update data in the mysql table using update query in sql

To update query in sql table using mysql promt we use”UPDATE”command as below.

Lets take an example to update data in the student table which we previously created.
Before update to view the table data using the below command.

mysql> select * from student;
+----------------+------------------+-----------------+
| studentrollno | studentfirstname | studentlastname |
+----------------+------------------+-----------------+
|              1 | Raktim           | Sharma          |
+----------------+------------------+-----------------+
1 row in set (0.01 sec)

Now we are going to update the last name of the student whose roll no is 1. To do this we use the below command.

mysql> UPDATE student SET studentlastname = 'Ghosh' WHERE studentrollno = 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

After modified the date we need to view the data using the below command.

mysql> select * from student;
+----------------+------------------+-----------------+
| studentnrollno | studentfirstname | studentlastname |
+----------------+------------------+-----------------+
|              1 | Raktim           | Ghosh           |
+----------------+------------------+-----------------+
1 row in set (0.00 sec)
How to Update value in the mysql table step by step guide for beginner,update query in sql
How to Update value in the mysql table step by step guide for beginner 

Update data in mysql table using php script

To update the data using php script we have to take mysql_query function as below.


To connect to mysql server we use mysql_connect function and to select the database we will use mysql_select_db function(if we not define db name variable).All functions are important to run update query in sql using php script. 

To update data into mysql table we need to pass two arguments (connection and query)inside the mysql_query function. 

Lets take an example to update the data using php script

<html>
   <head>
      <title>Update value into MySQL table on linux system</title>
   </head>
   <body>
      <?php
         $mysql_host = 'localhost';
         $mysql_user = 'root';
         $mysql_pass = 'passwordofroot';
         $dbname = "techrideradmin";
         $conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass,$dbname);
        
         if(! $conn ) {
            die('Could not connect the server: ' . mysql_error());
         }
         echo 'Connection successfully established';
         $sql = “UPDATE student SET studentlastname = 'Ghosh' WHERE studentrollno = 1;”;
         $result = mysql_query( $sql, $conn );
        
         if(! $result ) {
            die('value not updated: ' . mysql_error());
         }
         echo "Value updated succcessfully\n";
         mysql_close($conn);
      ?>
   </body>
</html>

That’s all.If this article is helpful to know about Update Query in Sql to update value in Mysql table please share it!!!!