SlideShare a Scribd company logo
1 of 80
MYSQL
4 day training course
New Horizons
Trainer – Shueb Khan
Date : 2012 Feb 27-28 Mar 5-6
INTRODUCTION
Database
- An organized collection of data
Why Organized ?
So that it can be easily accessed,
managed and updated
Name
Age
Weight
Name
Salary
Date of Joining
Name
Birthday
Anniversary
Name
Status
Likes
Dislikes
Name
School
Technically – A database is an integrated collection of data, records, files and
other database objects.
Managing Databases
Database Management System (DBMS)
A set of software programs that can be used to create, maintain and manage
a database, allowing different user application programs to concurrently
access the same database.
DBMS
Database
Management
System
End Users
Database
Inventory
Invoices
Products
Customer
Employees
Sales
Department
Accounts
HR
DBMS TYPES
Number of Users
Single User
Multi User
Supports only one
user at a time
Supports multiple
user at the same time
Site Location
Centralized
Distributed
Database located
at a single site
Database distributed
across several different sites
Type of Use
Transactional
DBMS
Decision
Support System
(DSS)
Time-critical and accurate
transactions
Information extracted based on
historical data. Not time-critical.
INTRODUCING MYSQL
MySQL is a Relational Database Management System that
was originally developed by ‘MySQL AB’. Founded in Sweden, it is
not only the most popular Open source database,
but also the fastest growing one.
In early 2008, MySQL AB was acquired by Sun Microsystems, Inc.
In 2009, Sun Microsystems was acquired by Oracle Corporation
MySQL continues to be a strong supporter of the open source philosophy and the open source community.
Released for the first time in 1995, MySQL is now installed on every continent of the world (even Antarctica !)
MySQL runs on more than 20 platforms, among which
Windows, Linux and Solaris are the most popular operating systems.
More information on MySQL, its recent versions available, downloads,
documentations, forums etc. can be found at the official MySQL
Website: http://www.mysql.com
Brief Overview
 Free availability (Community Server)
 Low Price (Enterprise Server)
 Lowest TCO
 Open Sourced
 High Performance Characteristics
 Security Features
Why MySQL ?
Source: http://www.mysql.com/tcosavings/
Lowest TCO
MySQL default layout for rpm installation
INSTALLATION
Please follow the instructions of trainer
[mysqld]
port = 3309
socket = /tmp/mysql.sock
log-bin = /var/lib/mysql/mysql-bin
MySQL Architecture
MySQL operates in a networked environment using a Client/Server Architecture. A MySQL installation
has the following major components
• MySQL Server
• Client Programs
• Non-Client Utilities
MySQL CLIENT
MySQL SERVER
DB Files
mysqld
Client Programs
Non Client Utilities
Client/Server Architecture
OVERVIEW
UTILITIES
MySQL Utilities
MySQL
Server
MySQL
Client
Programs
MySQL
Non-Client
Utilities
The MySQL server uses Disk Space for storing various kinds of files:
Database directories – Each database corresponds to a single directory under the data directory
Format files (.frm files) – Every table and view has its own .frm file located in the appropriate
database directory, and containing its description (definition)
Log Files – Used for monitoring, recovery etc and stored in Logs and Data folder (depending on
log type)
Triggers – Stored in database directory along with affected table
System database – called mysql, containing details like user information, grants etc..
PHYSICAL STRUCTURE
MEMORY ALLOCATION
MEMORY
Instance Session
Allocated Once Dynamically Allocated
MySQL Configuration
CONFIGURATION FILE
Instance-1
--------
--------
--------
Instance-2
--------
--------
--------
Instance-3
--------
--------
--------
mysqld1
mysqld2
mysqld3
/etc/my.cnf
MySQL Server
/etc/my.cnf is the global configuration file with configuration details of each
installed instance on the server
LOGGING IN MYSQL
LOG FILES
General Query
Logs
Slow Query
Logs
Error
Logs
Binary
Logs
Relay
Logs
General Query Logs
- contain record of every SQL statement executed on the server, and client connection details, because
of which, they can grow up to enormous sizes, and should only be enabled for troubleshooting purposes
- is a GLOBAL variable
- can be enabled at server startup time by specifying parameter “general_log=1” in the conf file before
startup
- can also be enabled dynamically during runtime, without stopping a running mysqld by running the
command “SET GLOBAL general_log=ON;”
- cannot be enabled at individual session level. But once enabled at global level, it can be disabled at
individual session level by using the parameter SQL_LOG_OFF
- to check if it is currently enabled, execute the command
SHOW GLOBAL VARIABLES LIKE ‘GENERAL_LOG’;
LOGGING IN MYSQL
LOGGING IN MYSQL
Slow Query Logs
- contain the text of queries that take longer than a threshold time to complete and examined a threshold
number of rows
- the threshold time can be set by setting value of parameter “long_query_time” in seconds. The default
value, if enabled, is 10 seconds
- the threshold number of examined rows can be set by setting value of parameter
“min_examined_row_limit”. The default value, if enabled, is 0
- to also include those queries that do not use indexes into slow query logs, enable the parameter
“log_queries_not_using_indexes” either through conf file or dynamically
- is a GLOBAL variable
- can be enabled at server startup time by specifying parameter “slow_query_log=1” in the conf file before
startup
- can also be enabled dynamically during runtime, without stopping a running mysqld by running the
command “SET GLOBAL slow_query_log=ON;”
- to check if it is currently enabled, execute the command
SHOW GLOBAL VARIABLES LIKE ‘SLOW_QUERY_LOG’;
LOGGING IN MYSQL
Error Logs
- contain diagnostic messages produced by the server, that include unexpected shutdown of mysqld,
storage engine initialization failure, replication related problems etc.
- it is not enabled by default. But if mysqld_safe is used for starting mysql, it automatically starts logging
information into the error log file by creating it in default location, data directory
- To explicitly specify use of an error log file name, use the variable “log_error” in conf file before
restarting the instance.
- it is not a dynamic variable
- is a GLOBAL variable
- to check if it is currently enabled, execute the command
SHOW GLOBAL VARIABLES LIKE ‘LOG_ERROR’;
LOGGING IN MYSQL
Binary Logs
- contain a record of all DDL and DML statements executed on the server data
- it does not log statements like SELECT or SHOW, that do not modify any data
- the contents of binary logs can be viewed using a utility called mysqlbinlog
- it can be enabled using the option “log_bin” inside the conf file before restarting mysql. A file name can
be specified in this parameter along with the path.
- any extension provided to the file name specified in “log_bin” is removed by mysql, and it automatically
appends a numeric extension to the file. This number increases every time mysql creates a new file
- The server creates a new file
- each time it is started
- at the time logs are flushed
- at the time current log file’s size reaches max_binlog_size
- the log file may also be larger than the size specified by max_binlog_size if there is a long transaction. A
transaction is not split between different files.
- it is not a dynamic variable
- is a GLOBAL variable
LOGGING IN MYSQL
Binary Logs
- All binary logs can be deleted using the statement “RESET BINARY LOGS;”
- to check its current value, execute the command SHOW GLOBAL VARIABLES LIKE ‘LOG_BIN’;
- cannot be enabled at individual session level. But once enabled at global level, it can be disabled at
individual session level by using the parameter “ SQL_LOG_BIN=OFF”
- Binary logging formats
- STATEMENT : contain SQL statements that changed data, so are smaller in size. Can be
specified using “binlog_format=STATEMENT”
- ROW : writes each changed row to the binary log, so are bigger in size. It can be helpful when
non-deterministic functions are used. Can be specified using “binlog_format=ROW”
- MIXED : This will generally perform the Statement Based Binary logging, but would automatically
switch to Row Based logging whenever appropriate. Can be specified using
“binlog_format=MIXED”
Starting & Stopping MySQL
 The general script used for Starting/Stopping MySQL servers on both Linux and Solaris installations is
/etc/init.d/mysql
 Individual shutdown of particular installations can also be done using the mysqladmin utility.
For example,
/usr/local/mysql/5.5.8/bin/mysqladmin –uroot –p shutdown
 To check if mysql is running, use the command
ps –ef | grep –i mysql
which would show 2 processes running, mysqld and mysqld_safe
Please follow the instructions of trainer for practical sessions
CREATING DATABASES
CREATE DATABASE <DATABASE_NAME> ;
SHOW SCHEMAS ;
USE <DATABASE_NAME> ;
MYSQL USER
Username
Password
Hostname
MySQL User
ADDING MYSQL USER
CREATE USER ‘<USERNAME>’@’<HOSTNAME>’
IDENTIFIED BY ‘<PASSWORD>’ ;
GRANT <PRIVILEGE> ON <DB> TO ‘<USERNAME>’@’<HOSTNAME>’
IDENTIFIED BY ‘<PASSWORD>’ ;
SHOW GRANTS FOR ‘<USERNAME>’@’<HOSTNAME>’ ;
Q&A
+
LABS
DATA TYPES
DATA TYPE
Numeric Character Binary Temporal
For Numeric
values
For Text
strings
For Binary
Data strings
For Time
And Dates
NUMERIC DATA TYPES
- Integer Types ( for whole numbers)
TINYINT 1 byte -128 to 127
SMALLINT 2 bytes -32,768 to 32,767
MEDIUMINT 3 bytes -8,388,608 to 8,388,607
INT 4 bytes -----------------
BIGINT 8 bytes ------------------
- Floating Point Types ( for approximate value numbers)
FLOAT 4 bytes
DOUBLE 8 bytes
- Fixed Point Types ( for exact value decimal numbers)
DECIMAL 4 bytes
Specified as “DECIMAL(<precision>,<scale>)
For example, DECIMAL(5,2) can store from -999.99 to 999.99 values. The default precision is 10
Each multiple of 9 digits require 4 bytes for storage
CHARACTER STRING DATA TYPES
- CHAR (M)
Fixed-length string (0-255)
Always right padded with spaces to the specified length when stored
- VARCHAR (M)
Variable-length string (0-65535)
Not padded with spaces
OTHER DATA TYPES
BLOB / MEDIUMBLOB / LONGBLOB
- For storing Binary Large Objects. For eg. images, audio, multimedia files
DATE
- Supported range is '1000-01-01' to '9999-12-31‘
DATETIME
- Supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
TIME
- Supported range is '-838:59:59' to '838:59:59‘
YEAR
- In four-digit format, permissible values are 1901 to 2155
- in two-digit format, permissible values are 70 to 69, representing 1970 to 2069
NULL
An SQL keyword specified at a place where a value-expression is expected.
It is used to express “NO VALUE EXISTS”
It DOES NOT mean “ZERO”
It DOES NOT mean “string of blank spaces”
Should be used very carefully
Can be used as default value also
CREATING TABLE
Simplified syntax :
CREATE TABLE <TABLE_NAME> (
<COLUMN_NAME> <COLUMN_DEFINITION> <CONSTRAINT>,
<COLUMN_NAME> <COLUMN_DEFINITION> <CONSTRAINT>,
<COLUMN_NAME> <COLUMN_DEFINITION> <CONSTRAINT>,
) ENGINE=<ENGINE_NAME>;
Detailed syntax with all options available in MySQL docs
SQL TYPES
DDL – Data Definition Language
DML – Data Modification Language
DCL – Data Control Language
TCL – Transaction Control Language
Storage Engines
 A Storage Engine is the underlying component that an
RDBMS uses to create, retrieve, update and delete data
from a databases.
 It is a low level engine inside the database Server that
takes care of storing and retrieving data from tables.
 A client does retrieving or changing of data in tables by
sending requests in the form of SQL statements, which are
processed by server using a two-tier model.
 The SQL Tier (tier-1) is mostly free of any storage engine
dependencies.
Query Cache
Parsing
Optimization
Execution
Storage Engines
Innodb MyISAM NDB Others..
Tier-1
Tier-2
Client Request
Contd..
Storage Engines
The most common Storage Engines are
• InnoDB
• MyISAM
• Memory
Examples of Storage Engines
• MyISAM
• InnoDB
• NDB/Cluster
• Archive
• Memory
contd..
Storage Engines contd…
Storage Engines
MyISAM
- This was the default storage engine in MySQL before version 5.5
- Each MyISAM table is associated with three physical files on the disk
- a .frm file
- a .MYD file
- a .MYI file
- It has the capability to store rows in three different formats (specified by ROW_FORMAT during table
creation)
- Fixed row format
- constant row size for all rows.
- default for myisam tables
- it is used when table contains no variable length columns
- each row is stored using a fixed number of bytes
- very quick and easy to cache
- usually require more disk space than dynamic row format tables
- CHAR and VARCHAR columns are space padded to the specified column width
- Dynamic Row format
- rows take varying amounts of space
- it is used when the table contains any variable length column (VARCHAR etc.)
- Each row is preceded by a bitmap that indicates which columns contain empty string or zero
- Much less disk space is required
Storage Engines
PROS
• Low Storage Costs
• Support for BTree, Fulltext Indexes
• Very fast insert and query performance
CONS
• Table level locking
• No support for foreign key constraints
• Does not support transactions
MyISAM
- Compressed format
- tables are packed and made read-only (using myisampack).
- occupy very less disk space
- if a column has only small set of possible values, the data type is converted to ENUM
- can be used for both fixed-length and dynamic-length rows
Storage Engines
MyISAM
EXAMPLE SETUP SHOWING MYISAM RELATED FILES
Data
Logs
Table1.frm
Backups
DB-1
DB-2
/data/mysql/5.5.8/
Table2.frm
Table format files
Table1.MYD
Table2.MYD
Table1.MYI
Table2.MYI
Table Data files
Table Index files
Storage Engines
InnoDB
- developed by Innobase, which is a subsidiary of Oracle
- its design follows ACID model
- allows row-level locking
- supports foreign key referential-integrity constraints
- the InnoDB tablespace, which is a set of one or more files, is used for storing table contents
- there are InnoDB log files which contain transaction activity
- supports transactions with COMMIT, SAVEPOINT and ROLLBACK
- InnoDB also maintains a buffer pool and log buffer which help to improve performance and enable crash
recovery of tables
PROS
• ACID compliant
• Support for crash recovery
• Row level locking
• Support for foreign keys
• B+Tree and Hash Indexes support
• Advanced memory cache mechanism
CONS
• No Full Text index support
• Requires more disk and memory resources
EXAMPLE SETUP SHOWING INNODB RELATED FILES
Data
Logs
Table1.frm
Backups
DB-1
DB-2
Ibdata1
Ib_logfile0
/data/mysql/5.5.8/
Ib_logfile1
Table2.frm
Shared Tablespace file
InnoDB Log Files
Table format files
Storage Engines
InnoDB
Storage Engines
Memory
- It uses tables that are stored in memory, and have fixed-length rows, due to which they are very fast
- Both data and indexes are stored in memory, limited by the parameter max_heap_table_size (default
16MB)
- During server restarts, the table exists, but its contents are wiped out.
- cannot contain BLOB or TEXT columns
- each table has its own .frm file also
PROS
• Extremely fast read and write operations
• Support for tree and hash indexes
CONS
• Data is not persistent between server shutdowns
• No Transactional support
• No support for foreign keys
• Table level locking
• No Full Text index support
• Cannot store BLOB or TEXT
Storage Engines
CSV
- stores data in text files using comma separated values format
- every table has its own .frm file
- data inserted into the table gets stored in the text file using comma-separated values format
- every table has its own CSM file also, which contains metadata information about the table
- it does not support indexing
- it does not support partitioning
- it does not allow NULL columns
Storage Engines
ARCHIVE
- used for storing large amount of data in a compressed format
- does not support indexes
- every table has its own .frm file
- data inserted into the table gets stored in a file with extension .ARZ
- supports only the SELECT and INSERT statements
- no DELETE, REPLACE or UPDATE statements are allowed on ARCHIVE tables
- does row level locking
- rows are compressed as they are inserted
- a SELECT operation performs a complete table scan
Storage Engines
BLACKHOLE
- this engine accepts data but does not store it
- an INSERT command works on it, but a SELECT does not retrieve any data
- every table has its own .frm file. There is no other file associated with a table.
- no data is stored, but the queries are still logged in binary logs, if enabled
- INSERT triggers also work for BLACKHOLE tables, but not UPDATE and DELETE
- for the case of auto-increment columns, this engine does not automatically increment field values, and
does not retain the auto-increment field state
- possible uses
- can be used to create an intermediate slave on the Master, for filtering purpose
- to find performance bottlenecks not related to storage engine
Indexes
INTRODUCTION
BENEFITS
- Contain sorted values, allowing MySQL to find particular row values faster
- The server can reach a particular row value directly, so lesser I/O
TYPES OF INDEXES
Types of indexes supported by MySQL
- PRIMARY KEY INDEX  Unique + NOT NULL
- UNIQUE INDEX  Unique + NULLs allowed
- NON UNIQUE INDEX  Key values may occur multiple times
- FULLTEXT  For text searching
For string data types, it is also possible to index on a column prefix rather than the
entire width of it, which means that it is possible to create an index on a specified
width of a column.
For example, if a name column is of 255 characters, and using a query, we find
that the first 10 characters of each row are sufficient to obtain distinct values for
most of them, then an index can be created using only the first 10 characters of
each row.
This will not only allow more values to be cached in memory due to its small size,
but also can improve index performance dramatically.
INDEX COLUMN PREFIXES
LEFTMOST INDEX PREFIXES
In the case of composite indexes, MySQL can use leftmost index prefixes of
that index.
A leftmost index prefix of a composite index consists of one or more of the initial
columns of the index.
MySQL’s capability to use leftmost index prefixes enables you to avoid creating
unnecessary indexes.
TRIGGERS
WHAT IS A TRIGGER ?
A named database object that is maintained within a database, and gets activated
when data within a table is modified.
It is defined to activate when a particular kind of event occurs for a given table.
The events can be defined as INSERT | DELETE | UPDATE.
They can be defined to get activate either BEFORE or AFTER the event.
WHEN TO USE TRIGGER ?
- A Trigger can examine row values to be inserted or updated, and can determine
what values were deleted or what they were updated to
- It can change values before they are inserted into a table.
- You can modify how INSERT, DELETE or UPDATE works
TRIGGER Usage Example
1. Test Database and Test table Created
mysql> create database d1;
Query OK, 1 row affected (0.01 sec)
mysql> use d1;
Database changed
mysql>
mysql> create table tab1 (id int, num int);
Query OK, 0 rows affected (0.01 sec)
2. Test table inserted with dummy values
mysql> insert into tab1 values (1, 10);
Query OK, 1 row affected (0.01 sec)
mysql> insert into tab1 values (2, 20);
Query OK, 1 row affected (0.01 sec)
mysql> insert into tab1 values (3, 30);
Query OK, 1 row affected (0.01 sec)
mysql> insert into tab1 values (4, 40);
Query OK, 1 row affected (0.01 sec)
mysql> insert into tab1 values (5, 50);
Query OK, 1 row affected (0.01 sec)
mysql> select * from tab1;
+------+------+
| id | num |
+------+------+
| 1 | 10 |
| 2 | 20 |
| 3 | 30 |
| 4 | 40 |
| 5 | 50 |
+------+------+
5 rows in set (0.00 sec)
3. Checking if any previous trigger exists
mysql> show triggers;
Empty set (0.00 sec)
4. Creating another test table
mysql> create table delnumber (id int auto_increment primary key, num int);
Query OK, 0 rows affected (0.01 sec)
5. Creating Trigger
mysql> create trigger tr1
-> after delete on tab1
-> for each row
-> insert into delnumber (id, num) values
-> (old.id, old.num);
Query OK, 0 rows affected (0.01 sec)
6. Checking that the newly created trigger now exists
mysql> show triggersG
*************************** 1. row ***************************
Trigger: tr1
Event: DELETE
Table: tab1
Statement: insert into delnumber (id, num) values
(old.id, old.num)
Timing: AFTER
Created: NULL
sql_mode:
Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)
7. Checking the trigger usage
mysql> delete from tab1 where num=20;
Query OK, 1 row affected (0.00 sec)
mysql> select * from tab1;
+------+------+
| id | num |
+------+------+
| 1 | 10 |
| 3 | 30 |
| 4 | 40 |
| 5 | 50 |
+------+------+
4 rows in set (0.00 sec)
mysql> select * from delnumber;
+----+------+
| id | num |
+----+------+
| 2 | 20 |
+----+------+
1 row in set (0.00 sec)
GRANTS & PRIVILEGES
User security consists of 5 access levels that incrementally
refine the privileges on user account.
• user  level 1
• db  level 2
• tables  level 3
• columns  level 4
• procs  level 5
Privileges can be granted at these levels to control the
level of access to databases for users.
Examples:
GRANT SELECT, INSERT ON *.* TO 'someuser'@'somehost';
GRANT SELECT, INSERT ON mydb.* TO 'someuser'@'somehost';
GRANT SELECT, INSERT ON mydb.mytbl TO 'someuser'@'somehost';
GRANT SELECT (col1), INSERT (col1,col2) ON mydb.mytbl TO 'someuser'@'somehost';
GRANT EXECUTE ON PROCEDURE mydb.myproc TO 'someuser'@'somehost';
user
db
tables
columns
routine
Global access
Database specific
access
Table specific
access
Column specific
access
Specific Stored procedures
and functions access
User Security
Replication
Replication Overview
MySQL Replication can be used to keep a server’s data synchronized with different server/servers so as
to have a copy of the Production databases that can be used for various purposes, including performance
improvement, reporting purposes, backups, load distribution and long distance data distribution.
Depending on the configuration, it is possible to replicate all databases, selected databases, or selected
tables from a database.
Replication can be of various types, including:
• Basic Replication
• Single Master with multiple slaves
• Master-Master in Active-Active mode
• Master-Master in Active-Passive mode
• Ring
• etc…
A basic replication involving two servers can be represented as in the below figure.
The Master server contains the databases and tables that get replicated onto the Slave server.
MASTER SERVER SLAVE SERVER
Databases
Tables
Replicated
Databases
Tables
How Replication works
MySQL Replication works on the basis of binary logs, which contain all changes performed on the tables.
The binary logs that are generated from a Master server are transported to the Slave server, where all
the recorder events from logs are played on Slave server, to bring it in sync with the Master.
At a high level, replication is a simple three-part process:
1. The master records changes to its data in its binary log. (the records are called events also)
2. The Slave copies the master’s binary log events to its relay log
3. The slave replays the events in the relay log, applying the changes to its own data.
For the points (2) and (3) above, there are two threads running on Slave server which take care of these
operations:
I/O Slave Thread – This is responsible for reading events from the master’s binary logs, and copying them
to the slave’s relay log.
SQL Slave Thread – This is responsible for reading events from the relay log and replaying them on the
slave data, thus bringing it in sync with the Master.
MASTER SLAVE
Node-1 Node-2
Data Changes
Binary Log
I/O Thread
Read
Write
Relay Log
SQL Thread
Read
Replay
Server Server
Setting Up Replication
How to setup Replication
From the previous slide, it is clear that the basic requirements for a Replication configuration are :
• Binary Logging should be enabled on Master server
• Both threads (I/O thread and SQL thread) should be running on the Slave server
One of the primary requirements for setting up Replication is to enable Binary Logging at Master Server.
All updates that happen to the data on Master server are recorded in the binary logs in the form of events.
These events can be replayed at Slave side to bring it in sync with the Master.
Every server involved in a Replication setup should have a unique server-id that can identify it, so ensure
that a different server-id is allotted to each server.
On Master, record the bin log file number and the position number of log in it, so that the slave can be
instructed to start Replication from that file and that pointer number onwards
The Slave gets its updates from the Master by connecting through a user that has to be specially created
on the Master for this purpose, having just the privilege of REPLICATION SLAVE
It is advisable to set the Slave in read-only mode of operation, so that there would not be any
undesirable updates on Slave’s data which can result in data corruption or other issues.
Setting up Replication
The following steps assume the availability of two servers,
server-A (Master)  172.30.0.1
server-B (Slave)  172.30.0.2
with a fresh installation of MySQL v5.5 on both.
1. Ping bothways on servers A and B, to check their network connectivity.
2. On the Master server (A):
a. Login as root user and create a user (rep) that would be used by Slave server for replication purposes.
mysql> GRANT REPLICATION SLAVE ON *.* to 'rep'@'172.30.0.2' identified by 'rep';
 Run on server-A
mysql> GRANT REPLICATION CLIENT ON *.* to 'rep'@'localhost' identified by 'rep';
 Run on server-A
b. Stop the instance, and edit the file /etc/my.cnf. Add/edit these parameters in the [mysqld]
# /etc/init.d/mysql.server stop
[mysqld]
log-bin= /var/lib/mysql/mysql-bin
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1
c. Start the instance on server A
# /etc/init.d/mysql.server start
Setting up Replication
3. On the Slave server (B):
a. Stop the server and edit the file /etc/my.cnf. Add/edit these parameters in the [mysqld] section
# /etc/init.d/mysql.server stop
[mysqld]
server-id=2
log-bin= /var/lib/mysql/mysql-bin
master-info-file= /var/lib/mysql/replication/master.info
relay-log-info-file= /var/lib/mysql/replication/relay-log.info
slave_load_tmpdir= /var/lib/mysql/replication
read-only =1
b. Create the directory “replication” inside data directory.
The owner of this directory should be the same as the owner of mysql installation.
mkdir –p /var/lib/mysql/replication
chown –R mysql:mysql /var/lib/mysql/replication
c. Startup the server
# /etc/init.d/mysql.server start
Setting up Replication
4. On the Master server (A) :
Login as root user and check Master Log position, using commands as below
mysql> FLUSH TABLES WITH READ LOCK;
mysql> SHOW MASTER STATUS;
mysql> UNLOCK TABLES;
Note down the values of ‘File’ and ‘Position’ columns from above commands’ output for the Master server. An
example output of above commands is as below:
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 417 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
5. On the Slave server (B) :
a. Configure the Slave server settings and start the slave threads using command as below.
Write the IP address of master server in below command at MASTER_HOST:
mysql> CHANGE MASTER TO
-> MASTER_HOST='172.30.0.1',
-> MASTER_USER='rep',
-> MASTER_PASSWORD='rep',
-> MASTER_LOG_FILE= '<value_of_’File’_obtained_in_previous_step >',
-> MASTER_LOG_POS= <value_of_’Position’_obtained_in_previous_step >;
mysql> START SLAVE;
6. Replication setup is complete now. Execute the command SHOW SLAVE STATUS on server-B to check for its
status.
Setting up Replication
Checking Replication Status (at Slave)
There are various ways of checking Replication
At the Slave side, a command to check the Replication status is SHOW SLAVE STATUS G
An example output is shown below
The Address of Master
Binary Log file Number
currently reading from Master
Position of reading from Master Binary Log
Status of the Slave threads – should be both running
for replication to continue
Position of Log application – should be ideally
always equal to Read_Master_Log_Pos
Shows error of Replication (if any)
Contd..
Checking Replication Status (at Slave) contd..
The State of different threads running on the Slave server can also be checked using the command
SHOW FULL PROCESSLIST G
An example output has been shown below:
State for IO thread
State for SQL thread
Checking Replication Status (at Master)
At the Master side, these commands can be used to check the Replication status
 SHOW MASTER STATUS G
 SHOW FULL PROCESSLIST;
An example output is shown below
The State column shows the current state of Master.
A message like this one indicates Master has sent all logs to Slave, and waiting for any update
Replication Error Handling
Errors in Replication
The errors encountered in a Replication setup can be seen
 By checking value of “Last_Error” in the output of SHOW SLAVE SATUS command.
 By checking the error log file (existing, by default, in the data directory)
If an error relates to the inability of a command that cannot be executed on Slave server due to inconsistent
data in it, try to resolve the error by finding out the cause of that inconsistency.
If due to some reasons, the command cannot be executed on Slave server, and the only option is to skip those
particular commands from being executed, then check if the skipping of those commands’ execution would
not make it inconsistent or ‘not-in-sync’ with Master. Use the command to skip a number of commands from
being executed
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = N ;
START SLAVE ;
where N is the number of statements that should be skipped from Master’s bin log. It is better to skip only
one statement at a time and check the next statement in error log every time before skipping it.
Stopping Replication
The process of Replication can be stopped or paused temporarily at the Slave side. This can be done to
perform maintenance activities or for any other purposes. There are options to stop individual Slave threads,
or to completely stop the Replication process at the Slave side.
For example,
(i) for the purpose of backing up the Slave, updates on the Slave can be paused temporarily
by stopping the SQL thread using this command
STOP SLAVE SQL_THREAD ;
To restart it again, use
START SLAVE SQL_THREAD ;
(ii) Similarly, to Stop the I/O thread from reading Master bin logs, use the command
STOP SLAVE IO_THREAD ;
(iii) To completely Stop the Replication process on the slave, use
STOP SLAVE ;
To restart it again, use
START SLAVE ;
Master-Master Replication
Setting up Master-Master Replication
The following steps assume the availability of two servers,
server-A (Master)  172.30.0.1
server-B (Slave)  172.30.0.2
with a fresh installation of MySQL v5.5 on both.
1. Ping bothways on servers A and B, to check their network connectivity.
2. Execute the below mentioned steps on both the servers:
a. Login as root user and create a user (rep) that would be used by Slave server for replication purposes.
mysql> GRANT REPLICATION SLAVE ON *.* to 'rep1'@'172.30.0.2' identified by 'rep1';
 Run on server-A
mysql> GRANT REPLICATION SLAVE ON *.* to 'rep2'@'172.30.0.2' identified by 'rep2';
 Run on server-B
b. Stop the instance, and edit the file /etc/my.cnf. Add/edit these parameters in the [mysqld]
# /etc/init.d/mysql.server stop
[mysqld]
log-bin= /var/lib/mysql/mysql-bin
server-id=1  write in server-A’s my.cnf file
server-id=2  write in server-B’s my.cnf file
innodb_flush_log_at_trx_commit=1
sync_binlog=1
log_slave_updates=1
master-info-file= /var/lib/mysql/replication/master.info
relay-log-info-file= /var/lib/mysql/replication/relay-log.info
read-only =1  write only on server-B’s my.cnf file
Setting up Master-Master Replication
c. Create the directory “replication” inside data directory.
The owner of this directory should be the same as the owner of mysql installation.
mkdir –p /var/lib/mysql/replication
chown –R mysql:mysql /var/lib/mysql/replication
d. Startup both the servers
# /etc/init.d/mysql.server start
e. Login as root user and check Master Log position, using commands as below
mysql> FLUSH TABLES WITH READ LOCK;
mysql> SHOW MASTER STATUS;
mysql> UNLOCK TABLES;
Note down the values of ‘File’ and ‘Position’ columns from above commands’ output . An example output of above
commands is as below:
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 417 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
Setting up Master-Master Replication
3. Configure the Slave server settings on Server-A and start the slave threads using command as below.
Write the IP address of master server in below command at MASTER_HOST:
mysql> CHANGE MASTER TO
-> MASTER_HOST='172.30.0.2',
-> MASTER_USER='rep2',
-> MASTER_PASSWORD='rep2',
-> MASTER_LOG_FILE= '<value_of_’File’_obtained_in_server-B_execution >',
-> MASTER_LOG_POS= <value_of_’Position’_obtained_in_server-B_execution >;
mysql> START SLAVE;
4. Configure the Slave server settings on Server-B and start the slave threads using command as below.
Write the IP address of master server in below command at MASTER_HOST:
mysql> CHANGE MASTER TO
-> MASTER_HOST='172.30.0.1',
-> MASTER_USER='rep1',
-> MASTER_PASSWORD='rep1',
-> MASTER_LOG_FILE= '<value_of_’File’_obtained_in_server-A_execution >',
-> MASTER_LOG_POS= <value_of_’Position’_obtained_in_server-A_execution >;
mysql> START SLAVE;

More Related Content

Similar to C_mysql-1.ppt

Collaborate 2012 - Administering MySQL for Oracle DBAs
Collaborate 2012 - Administering MySQL for Oracle DBAsCollaborate 2012 - Administering MySQL for Oracle DBAs
Collaborate 2012 - Administering MySQL for Oracle DBAsNelson Calero
 
Getting started with my sql
Getting started with my sqlGetting started with my sql
Getting started with my sqlWeb Sky
 
E business suite r12.2 changes for database administrators
E business suite r12.2 changes for database administratorsE business suite r12.2 changes for database administrators
E business suite r12.2 changes for database administratorsSrinivasa Pavan Marti
 
E business suite r12.2 changes for database administrators
E business suite r12.2 changes for database administratorsE business suite r12.2 changes for database administrators
E business suite r12.2 changes for database administratorsSrinivasa Pavan Marti
 
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)M Malai
 
Ansible is Our Wishbone
Ansible is Our WishboneAnsible is Our Wishbone
Ansible is Our WishboneMydbops
 
BITS: Introduction to MySQL - Introduction and Installation
BITS: Introduction to MySQL - Introduction and InstallationBITS: Introduction to MySQL - Introduction and Installation
BITS: Introduction to MySQL - Introduction and InstallationBITS
 
Mysql database basic user guide
Mysql database basic user guideMysql database basic user guide
Mysql database basic user guidePoguttuezhiniVP
 
MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017Dave Stokes
 
Snowflake_Cheat_Sheet_Snowflake_Cheat_Sheet
Snowflake_Cheat_Sheet_Snowflake_Cheat_SheetSnowflake_Cheat_Sheet_Snowflake_Cheat_Sheet
Snowflake_Cheat_Sheet_Snowflake_Cheat_Sheetharipra2
 
MySQL Utilities -- PyTexas 2015
MySQL Utilities -- PyTexas 2015MySQL Utilities -- PyTexas 2015
MySQL Utilities -- PyTexas 2015Dave Stokes
 
My sql introduction for Bestcom
My sql introduction for BestcomMy sql introduction for Bestcom
My sql introduction for BestcomIvan Tu
 
My S Q L Introduction for 1 day training
My S Q L  Introduction for 1 day trainingMy S Q L  Introduction for 1 day training
My S Q L Introduction for 1 day trainingIvan Tu
 
Configuring sql server - SQL Saturday, Athens Oct 2014
Configuring sql server - SQL Saturday, Athens Oct 2014Configuring sql server - SQL Saturday, Athens Oct 2014
Configuring sql server - SQL Saturday, Athens Oct 2014Antonios Chatzipavlis
 

Similar to C_mysql-1.ppt (20)

Collaborate 2012 - Administering MySQL for Oracle DBAs
Collaborate 2012 - Administering MySQL for Oracle DBAsCollaborate 2012 - Administering MySQL for Oracle DBAs
Collaborate 2012 - Administering MySQL for Oracle DBAs
 
Slides
SlidesSlides
Slides
 
Getting started with my sql
Getting started with my sqlGetting started with my sql
Getting started with my sql
 
Mysql tutorial 5257
Mysql tutorial 5257Mysql tutorial 5257
Mysql tutorial 5257
 
My sql basic
My sql basicMy sql basic
My sql basic
 
Mysql all
Mysql allMysql all
Mysql all
 
E business suite r12.2 changes for database administrators
E business suite r12.2 changes for database administratorsE business suite r12.2 changes for database administrators
E business suite r12.2 changes for database administrators
 
E business suite r12.2 changes for database administrators
E business suite r12.2 changes for database administratorsE business suite r12.2 changes for database administrators
E business suite r12.2 changes for database administrators
 
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
 
Ansible is Our Wishbone
Ansible is Our WishboneAnsible is Our Wishbone
Ansible is Our Wishbone
 
Mysql
MysqlMysql
Mysql
 
BITS: Introduction to MySQL - Introduction and Installation
BITS: Introduction to MySQL - Introduction and InstallationBITS: Introduction to MySQL - Introduction and Installation
BITS: Introduction to MySQL - Introduction and Installation
 
Mysql database basic user guide
Mysql database basic user guideMysql database basic user guide
Mysql database basic user guide
 
MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017
 
Snowflake_Cheat_Sheet_Snowflake_Cheat_Sheet
Snowflake_Cheat_Sheet_Snowflake_Cheat_SheetSnowflake_Cheat_Sheet_Snowflake_Cheat_Sheet
Snowflake_Cheat_Sheet_Snowflake_Cheat_Sheet
 
Mysql all
Mysql allMysql all
Mysql all
 
MySQL Utilities -- PyTexas 2015
MySQL Utilities -- PyTexas 2015MySQL Utilities -- PyTexas 2015
MySQL Utilities -- PyTexas 2015
 
My sql introduction for Bestcom
My sql introduction for BestcomMy sql introduction for Bestcom
My sql introduction for Bestcom
 
My S Q L Introduction for 1 day training
My S Q L  Introduction for 1 day trainingMy S Q L  Introduction for 1 day training
My S Q L Introduction for 1 day training
 
Configuring sql server - SQL Saturday, Athens Oct 2014
Configuring sql server - SQL Saturday, Athens Oct 2014Configuring sql server - SQL Saturday, Athens Oct 2014
Configuring sql server - SQL Saturday, Athens Oct 2014
 

Recently uploaded

Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 

Recently uploaded (20)

Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 

C_mysql-1.ppt

  • 1. MYSQL 4 day training course New Horizons Trainer – Shueb Khan Date : 2012 Feb 27-28 Mar 5-6
  • 3. Database - An organized collection of data Why Organized ? So that it can be easily accessed, managed and updated Name Age Weight Name Salary Date of Joining Name Birthday Anniversary Name Status Likes Dislikes Name School Technically – A database is an integrated collection of data, records, files and other database objects.
  • 4. Managing Databases Database Management System (DBMS) A set of software programs that can be used to create, maintain and manage a database, allowing different user application programs to concurrently access the same database. DBMS Database Management System End Users Database Inventory Invoices Products Customer Employees Sales Department Accounts HR
  • 5. DBMS TYPES Number of Users Single User Multi User Supports only one user at a time Supports multiple user at the same time Site Location Centralized Distributed Database located at a single site Database distributed across several different sites Type of Use Transactional DBMS Decision Support System (DSS) Time-critical and accurate transactions Information extracted based on historical data. Not time-critical.
  • 7. MySQL is a Relational Database Management System that was originally developed by ‘MySQL AB’. Founded in Sweden, it is not only the most popular Open source database, but also the fastest growing one. In early 2008, MySQL AB was acquired by Sun Microsystems, Inc. In 2009, Sun Microsystems was acquired by Oracle Corporation MySQL continues to be a strong supporter of the open source philosophy and the open source community. Released for the first time in 1995, MySQL is now installed on every continent of the world (even Antarctica !) MySQL runs on more than 20 platforms, among which Windows, Linux and Solaris are the most popular operating systems. More information on MySQL, its recent versions available, downloads, documentations, forums etc. can be found at the official MySQL Website: http://www.mysql.com Brief Overview
  • 8.  Free availability (Community Server)  Low Price (Enterprise Server)  Lowest TCO  Open Sourced  High Performance Characteristics  Security Features Why MySQL ?
  • 10. MySQL default layout for rpm installation
  • 11. INSTALLATION Please follow the instructions of trainer [mysqld] port = 3309 socket = /tmp/mysql.sock log-bin = /var/lib/mysql/mysql-bin
  • 13. MySQL operates in a networked environment using a Client/Server Architecture. A MySQL installation has the following major components • MySQL Server • Client Programs • Non-Client Utilities MySQL CLIENT MySQL SERVER DB Files mysqld Client Programs Non Client Utilities Client/Server Architecture OVERVIEW
  • 15. The MySQL server uses Disk Space for storing various kinds of files: Database directories – Each database corresponds to a single directory under the data directory Format files (.frm files) – Every table and view has its own .frm file located in the appropriate database directory, and containing its description (definition) Log Files – Used for monitoring, recovery etc and stored in Logs and Data folder (depending on log type) Triggers – Stored in database directory along with affected table System database – called mysql, containing details like user information, grants etc.. PHYSICAL STRUCTURE
  • 19. LOGGING IN MYSQL LOG FILES General Query Logs Slow Query Logs Error Logs Binary Logs Relay Logs
  • 20. General Query Logs - contain record of every SQL statement executed on the server, and client connection details, because of which, they can grow up to enormous sizes, and should only be enabled for troubleshooting purposes - is a GLOBAL variable - can be enabled at server startup time by specifying parameter “general_log=1” in the conf file before startup - can also be enabled dynamically during runtime, without stopping a running mysqld by running the command “SET GLOBAL general_log=ON;” - cannot be enabled at individual session level. But once enabled at global level, it can be disabled at individual session level by using the parameter SQL_LOG_OFF - to check if it is currently enabled, execute the command SHOW GLOBAL VARIABLES LIKE ‘GENERAL_LOG’; LOGGING IN MYSQL
  • 21. LOGGING IN MYSQL Slow Query Logs - contain the text of queries that take longer than a threshold time to complete and examined a threshold number of rows - the threshold time can be set by setting value of parameter “long_query_time” in seconds. The default value, if enabled, is 10 seconds - the threshold number of examined rows can be set by setting value of parameter “min_examined_row_limit”. The default value, if enabled, is 0 - to also include those queries that do not use indexes into slow query logs, enable the parameter “log_queries_not_using_indexes” either through conf file or dynamically - is a GLOBAL variable - can be enabled at server startup time by specifying parameter “slow_query_log=1” in the conf file before startup - can also be enabled dynamically during runtime, without stopping a running mysqld by running the command “SET GLOBAL slow_query_log=ON;” - to check if it is currently enabled, execute the command SHOW GLOBAL VARIABLES LIKE ‘SLOW_QUERY_LOG’;
  • 22. LOGGING IN MYSQL Error Logs - contain diagnostic messages produced by the server, that include unexpected shutdown of mysqld, storage engine initialization failure, replication related problems etc. - it is not enabled by default. But if mysqld_safe is used for starting mysql, it automatically starts logging information into the error log file by creating it in default location, data directory - To explicitly specify use of an error log file name, use the variable “log_error” in conf file before restarting the instance. - it is not a dynamic variable - is a GLOBAL variable - to check if it is currently enabled, execute the command SHOW GLOBAL VARIABLES LIKE ‘LOG_ERROR’;
  • 23. LOGGING IN MYSQL Binary Logs - contain a record of all DDL and DML statements executed on the server data - it does not log statements like SELECT or SHOW, that do not modify any data - the contents of binary logs can be viewed using a utility called mysqlbinlog - it can be enabled using the option “log_bin” inside the conf file before restarting mysql. A file name can be specified in this parameter along with the path. - any extension provided to the file name specified in “log_bin” is removed by mysql, and it automatically appends a numeric extension to the file. This number increases every time mysql creates a new file - The server creates a new file - each time it is started - at the time logs are flushed - at the time current log file’s size reaches max_binlog_size - the log file may also be larger than the size specified by max_binlog_size if there is a long transaction. A transaction is not split between different files. - it is not a dynamic variable - is a GLOBAL variable
  • 24. LOGGING IN MYSQL Binary Logs - All binary logs can be deleted using the statement “RESET BINARY LOGS;” - to check its current value, execute the command SHOW GLOBAL VARIABLES LIKE ‘LOG_BIN’; - cannot be enabled at individual session level. But once enabled at global level, it can be disabled at individual session level by using the parameter “ SQL_LOG_BIN=OFF” - Binary logging formats - STATEMENT : contain SQL statements that changed data, so are smaller in size. Can be specified using “binlog_format=STATEMENT” - ROW : writes each changed row to the binary log, so are bigger in size. It can be helpful when non-deterministic functions are used. Can be specified using “binlog_format=ROW” - MIXED : This will generally perform the Statement Based Binary logging, but would automatically switch to Row Based logging whenever appropriate. Can be specified using “binlog_format=MIXED”
  • 25. Starting & Stopping MySQL  The general script used for Starting/Stopping MySQL servers on both Linux and Solaris installations is /etc/init.d/mysql  Individual shutdown of particular installations can also be done using the mysqladmin utility. For example, /usr/local/mysql/5.5.8/bin/mysqladmin –uroot –p shutdown  To check if mysql is running, use the command ps –ef | grep –i mysql which would show 2 processes running, mysqld and mysqld_safe Please follow the instructions of trainer for practical sessions
  • 26. CREATING DATABASES CREATE DATABASE <DATABASE_NAME> ; SHOW SCHEMAS ; USE <DATABASE_NAME> ;
  • 28. ADDING MYSQL USER CREATE USER ‘<USERNAME>’@’<HOSTNAME>’ IDENTIFIED BY ‘<PASSWORD>’ ; GRANT <PRIVILEGE> ON <DB> TO ‘<USERNAME>’@’<HOSTNAME>’ IDENTIFIED BY ‘<PASSWORD>’ ; SHOW GRANTS FOR ‘<USERNAME>’@’<HOSTNAME>’ ;
  • 30. DATA TYPES DATA TYPE Numeric Character Binary Temporal For Numeric values For Text strings For Binary Data strings For Time And Dates
  • 31. NUMERIC DATA TYPES - Integer Types ( for whole numbers) TINYINT 1 byte -128 to 127 SMALLINT 2 bytes -32,768 to 32,767 MEDIUMINT 3 bytes -8,388,608 to 8,388,607 INT 4 bytes ----------------- BIGINT 8 bytes ------------------ - Floating Point Types ( for approximate value numbers) FLOAT 4 bytes DOUBLE 8 bytes - Fixed Point Types ( for exact value decimal numbers) DECIMAL 4 bytes Specified as “DECIMAL(<precision>,<scale>) For example, DECIMAL(5,2) can store from -999.99 to 999.99 values. The default precision is 10 Each multiple of 9 digits require 4 bytes for storage
  • 32. CHARACTER STRING DATA TYPES - CHAR (M) Fixed-length string (0-255) Always right padded with spaces to the specified length when stored - VARCHAR (M) Variable-length string (0-65535) Not padded with spaces
  • 33. OTHER DATA TYPES BLOB / MEDIUMBLOB / LONGBLOB - For storing Binary Large Objects. For eg. images, audio, multimedia files DATE - Supported range is '1000-01-01' to '9999-12-31‘ DATETIME - Supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. TIME - Supported range is '-838:59:59' to '838:59:59‘ YEAR - In four-digit format, permissible values are 1901 to 2155 - in two-digit format, permissible values are 70 to 69, representing 1970 to 2069
  • 34. NULL An SQL keyword specified at a place where a value-expression is expected. It is used to express “NO VALUE EXISTS” It DOES NOT mean “ZERO” It DOES NOT mean “string of blank spaces” Should be used very carefully Can be used as default value also
  • 35. CREATING TABLE Simplified syntax : CREATE TABLE <TABLE_NAME> ( <COLUMN_NAME> <COLUMN_DEFINITION> <CONSTRAINT>, <COLUMN_NAME> <COLUMN_DEFINITION> <CONSTRAINT>, <COLUMN_NAME> <COLUMN_DEFINITION> <CONSTRAINT>, ) ENGINE=<ENGINE_NAME>; Detailed syntax with all options available in MySQL docs
  • 36. SQL TYPES DDL – Data Definition Language DML – Data Modification Language DCL – Data Control Language TCL – Transaction Control Language
  • 38.  A Storage Engine is the underlying component that an RDBMS uses to create, retrieve, update and delete data from a databases.  It is a low level engine inside the database Server that takes care of storing and retrieving data from tables.  A client does retrieving or changing of data in tables by sending requests in the form of SQL statements, which are processed by server using a two-tier model.  The SQL Tier (tier-1) is mostly free of any storage engine dependencies. Query Cache Parsing Optimization Execution Storage Engines Innodb MyISAM NDB Others.. Tier-1 Tier-2 Client Request Contd.. Storage Engines
  • 39. The most common Storage Engines are • InnoDB • MyISAM • Memory Examples of Storage Engines • MyISAM • InnoDB • NDB/Cluster • Archive • Memory contd.. Storage Engines contd…
  • 40. Storage Engines MyISAM - This was the default storage engine in MySQL before version 5.5 - Each MyISAM table is associated with three physical files on the disk - a .frm file - a .MYD file - a .MYI file - It has the capability to store rows in three different formats (specified by ROW_FORMAT during table creation) - Fixed row format - constant row size for all rows. - default for myisam tables - it is used when table contains no variable length columns - each row is stored using a fixed number of bytes - very quick and easy to cache - usually require more disk space than dynamic row format tables - CHAR and VARCHAR columns are space padded to the specified column width - Dynamic Row format - rows take varying amounts of space - it is used when the table contains any variable length column (VARCHAR etc.) - Each row is preceded by a bitmap that indicates which columns contain empty string or zero - Much less disk space is required
  • 41. Storage Engines PROS • Low Storage Costs • Support for BTree, Fulltext Indexes • Very fast insert and query performance CONS • Table level locking • No support for foreign key constraints • Does not support transactions MyISAM - Compressed format - tables are packed and made read-only (using myisampack). - occupy very less disk space - if a column has only small set of possible values, the data type is converted to ENUM - can be used for both fixed-length and dynamic-length rows
  • 42. Storage Engines MyISAM EXAMPLE SETUP SHOWING MYISAM RELATED FILES Data Logs Table1.frm Backups DB-1 DB-2 /data/mysql/5.5.8/ Table2.frm Table format files Table1.MYD Table2.MYD Table1.MYI Table2.MYI Table Data files Table Index files
  • 43. Storage Engines InnoDB - developed by Innobase, which is a subsidiary of Oracle - its design follows ACID model - allows row-level locking - supports foreign key referential-integrity constraints - the InnoDB tablespace, which is a set of one or more files, is used for storing table contents - there are InnoDB log files which contain transaction activity - supports transactions with COMMIT, SAVEPOINT and ROLLBACK - InnoDB also maintains a buffer pool and log buffer which help to improve performance and enable crash recovery of tables PROS • ACID compliant • Support for crash recovery • Row level locking • Support for foreign keys • B+Tree and Hash Indexes support • Advanced memory cache mechanism CONS • No Full Text index support • Requires more disk and memory resources
  • 44. EXAMPLE SETUP SHOWING INNODB RELATED FILES Data Logs Table1.frm Backups DB-1 DB-2 Ibdata1 Ib_logfile0 /data/mysql/5.5.8/ Ib_logfile1 Table2.frm Shared Tablespace file InnoDB Log Files Table format files Storage Engines InnoDB
  • 45. Storage Engines Memory - It uses tables that are stored in memory, and have fixed-length rows, due to which they are very fast - Both data and indexes are stored in memory, limited by the parameter max_heap_table_size (default 16MB) - During server restarts, the table exists, but its contents are wiped out. - cannot contain BLOB or TEXT columns - each table has its own .frm file also PROS • Extremely fast read and write operations • Support for tree and hash indexes CONS • Data is not persistent between server shutdowns • No Transactional support • No support for foreign keys • Table level locking • No Full Text index support • Cannot store BLOB or TEXT
  • 46. Storage Engines CSV - stores data in text files using comma separated values format - every table has its own .frm file - data inserted into the table gets stored in the text file using comma-separated values format - every table has its own CSM file also, which contains metadata information about the table - it does not support indexing - it does not support partitioning - it does not allow NULL columns
  • 47. Storage Engines ARCHIVE - used for storing large amount of data in a compressed format - does not support indexes - every table has its own .frm file - data inserted into the table gets stored in a file with extension .ARZ - supports only the SELECT and INSERT statements - no DELETE, REPLACE or UPDATE statements are allowed on ARCHIVE tables - does row level locking - rows are compressed as they are inserted - a SELECT operation performs a complete table scan
  • 48. Storage Engines BLACKHOLE - this engine accepts data but does not store it - an INSERT command works on it, but a SELECT does not retrieve any data - every table has its own .frm file. There is no other file associated with a table. - no data is stored, but the queries are still logged in binary logs, if enabled - INSERT triggers also work for BLACKHOLE tables, but not UPDATE and DELETE - for the case of auto-increment columns, this engine does not automatically increment field values, and does not retain the auto-increment field state - possible uses - can be used to create an intermediate slave on the Master, for filtering purpose - to find performance bottlenecks not related to storage engine
  • 50. INTRODUCTION BENEFITS - Contain sorted values, allowing MySQL to find particular row values faster - The server can reach a particular row value directly, so lesser I/O
  • 51. TYPES OF INDEXES Types of indexes supported by MySQL - PRIMARY KEY INDEX  Unique + NOT NULL - UNIQUE INDEX  Unique + NULLs allowed - NON UNIQUE INDEX  Key values may occur multiple times - FULLTEXT  For text searching
  • 52. For string data types, it is also possible to index on a column prefix rather than the entire width of it, which means that it is possible to create an index on a specified width of a column. For example, if a name column is of 255 characters, and using a query, we find that the first 10 characters of each row are sufficient to obtain distinct values for most of them, then an index can be created using only the first 10 characters of each row. This will not only allow more values to be cached in memory due to its small size, but also can improve index performance dramatically. INDEX COLUMN PREFIXES
  • 53. LEFTMOST INDEX PREFIXES In the case of composite indexes, MySQL can use leftmost index prefixes of that index. A leftmost index prefix of a composite index consists of one or more of the initial columns of the index. MySQL’s capability to use leftmost index prefixes enables you to avoid creating unnecessary indexes.
  • 55. WHAT IS A TRIGGER ? A named database object that is maintained within a database, and gets activated when data within a table is modified. It is defined to activate when a particular kind of event occurs for a given table. The events can be defined as INSERT | DELETE | UPDATE. They can be defined to get activate either BEFORE or AFTER the event.
  • 56. WHEN TO USE TRIGGER ? - A Trigger can examine row values to be inserted or updated, and can determine what values were deleted or what they were updated to - It can change values before they are inserted into a table. - You can modify how INSERT, DELETE or UPDATE works
  • 57. TRIGGER Usage Example 1. Test Database and Test table Created mysql> create database d1; Query OK, 1 row affected (0.01 sec) mysql> use d1; Database changed mysql> mysql> create table tab1 (id int, num int); Query OK, 0 rows affected (0.01 sec) 2. Test table inserted with dummy values mysql> insert into tab1 values (1, 10); Query OK, 1 row affected (0.01 sec) mysql> insert into tab1 values (2, 20); Query OK, 1 row affected (0.01 sec) mysql> insert into tab1 values (3, 30); Query OK, 1 row affected (0.01 sec) mysql> insert into tab1 values (4, 40); Query OK, 1 row affected (0.01 sec) mysql> insert into tab1 values (5, 50); Query OK, 1 row affected (0.01 sec) mysql> select * from tab1; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 20 | | 3 | 30 | | 4 | 40 | | 5 | 50 | +------+------+ 5 rows in set (0.00 sec)
  • 58. 3. Checking if any previous trigger exists mysql> show triggers; Empty set (0.00 sec) 4. Creating another test table mysql> create table delnumber (id int auto_increment primary key, num int); Query OK, 0 rows affected (0.01 sec) 5. Creating Trigger mysql> create trigger tr1 -> after delete on tab1 -> for each row -> insert into delnumber (id, num) values -> (old.id, old.num); Query OK, 0 rows affected (0.01 sec) 6. Checking that the newly created trigger now exists mysql> show triggersG *************************** 1. row *************************** Trigger: tr1 Event: DELETE Table: tab1 Statement: insert into delnumber (id, num) values (old.id, old.num) Timing: AFTER Created: NULL sql_mode: Definer: root@localhost character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: latin1_swedish_ci 1 row in set (0.00 sec)
  • 59. 7. Checking the trigger usage mysql> delete from tab1 where num=20; Query OK, 1 row affected (0.00 sec) mysql> select * from tab1; +------+------+ | id | num | +------+------+ | 1 | 10 | | 3 | 30 | | 4 | 40 | | 5 | 50 | +------+------+ 4 rows in set (0.00 sec) mysql> select * from delnumber; +----+------+ | id | num | +----+------+ | 2 | 20 | +----+------+ 1 row in set (0.00 sec)
  • 61. User security consists of 5 access levels that incrementally refine the privileges on user account. • user  level 1 • db  level 2 • tables  level 3 • columns  level 4 • procs  level 5 Privileges can be granted at these levels to control the level of access to databases for users. Examples: GRANT SELECT, INSERT ON *.* TO 'someuser'@'somehost'; GRANT SELECT, INSERT ON mydb.* TO 'someuser'@'somehost'; GRANT SELECT, INSERT ON mydb.mytbl TO 'someuser'@'somehost'; GRANT SELECT (col1), INSERT (col1,col2) ON mydb.mytbl TO 'someuser'@'somehost'; GRANT EXECUTE ON PROCEDURE mydb.myproc TO 'someuser'@'somehost'; user db tables columns routine Global access Database specific access Table specific access Column specific access Specific Stored procedures and functions access User Security
  • 63. Replication Overview MySQL Replication can be used to keep a server’s data synchronized with different server/servers so as to have a copy of the Production databases that can be used for various purposes, including performance improvement, reporting purposes, backups, load distribution and long distance data distribution. Depending on the configuration, it is possible to replicate all databases, selected databases, or selected tables from a database. Replication can be of various types, including: • Basic Replication • Single Master with multiple slaves • Master-Master in Active-Active mode • Master-Master in Active-Passive mode • Ring • etc… A basic replication involving two servers can be represented as in the below figure. The Master server contains the databases and tables that get replicated onto the Slave server. MASTER SERVER SLAVE SERVER Databases Tables Replicated Databases Tables
  • 64. How Replication works MySQL Replication works on the basis of binary logs, which contain all changes performed on the tables. The binary logs that are generated from a Master server are transported to the Slave server, where all the recorder events from logs are played on Slave server, to bring it in sync with the Master. At a high level, replication is a simple three-part process: 1. The master records changes to its data in its binary log. (the records are called events also) 2. The Slave copies the master’s binary log events to its relay log 3. The slave replays the events in the relay log, applying the changes to its own data. For the points (2) and (3) above, there are two threads running on Slave server which take care of these operations: I/O Slave Thread – This is responsible for reading events from the master’s binary logs, and copying them to the slave’s relay log. SQL Slave Thread – This is responsible for reading events from the relay log and replaying them on the slave data, thus bringing it in sync with the Master. MASTER SLAVE Node-1 Node-2 Data Changes Binary Log I/O Thread Read Write Relay Log SQL Thread Read Replay Server Server
  • 66. How to setup Replication From the previous slide, it is clear that the basic requirements for a Replication configuration are : • Binary Logging should be enabled on Master server • Both threads (I/O thread and SQL thread) should be running on the Slave server One of the primary requirements for setting up Replication is to enable Binary Logging at Master Server. All updates that happen to the data on Master server are recorded in the binary logs in the form of events. These events can be replayed at Slave side to bring it in sync with the Master. Every server involved in a Replication setup should have a unique server-id that can identify it, so ensure that a different server-id is allotted to each server. On Master, record the bin log file number and the position number of log in it, so that the slave can be instructed to start Replication from that file and that pointer number onwards The Slave gets its updates from the Master by connecting through a user that has to be specially created on the Master for this purpose, having just the privilege of REPLICATION SLAVE It is advisable to set the Slave in read-only mode of operation, so that there would not be any undesirable updates on Slave’s data which can result in data corruption or other issues.
  • 67. Setting up Replication The following steps assume the availability of two servers, server-A (Master)  172.30.0.1 server-B (Slave)  172.30.0.2 with a fresh installation of MySQL v5.5 on both. 1. Ping bothways on servers A and B, to check their network connectivity. 2. On the Master server (A): a. Login as root user and create a user (rep) that would be used by Slave server for replication purposes. mysql> GRANT REPLICATION SLAVE ON *.* to 'rep'@'172.30.0.2' identified by 'rep';  Run on server-A mysql> GRANT REPLICATION CLIENT ON *.* to 'rep'@'localhost' identified by 'rep';  Run on server-A b. Stop the instance, and edit the file /etc/my.cnf. Add/edit these parameters in the [mysqld] # /etc/init.d/mysql.server stop [mysqld] log-bin= /var/lib/mysql/mysql-bin server-id=1 innodb_flush_log_at_trx_commit=1 sync_binlog=1 c. Start the instance on server A # /etc/init.d/mysql.server start
  • 68. Setting up Replication 3. On the Slave server (B): a. Stop the server and edit the file /etc/my.cnf. Add/edit these parameters in the [mysqld] section # /etc/init.d/mysql.server stop [mysqld] server-id=2 log-bin= /var/lib/mysql/mysql-bin master-info-file= /var/lib/mysql/replication/master.info relay-log-info-file= /var/lib/mysql/replication/relay-log.info slave_load_tmpdir= /var/lib/mysql/replication read-only =1 b. Create the directory “replication” inside data directory. The owner of this directory should be the same as the owner of mysql installation. mkdir –p /var/lib/mysql/replication chown –R mysql:mysql /var/lib/mysql/replication c. Startup the server # /etc/init.d/mysql.server start
  • 69. Setting up Replication 4. On the Master server (A) : Login as root user and check Master Log position, using commands as below mysql> FLUSH TABLES WITH READ LOCK; mysql> SHOW MASTER STATUS; mysql> UNLOCK TABLES; Note down the values of ‘File’ and ‘Position’ columns from above commands’ output for the Master server. An example output of above commands is as below: mysql> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (0.00 sec) mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 417 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql> UNLOCK TABLES; Query OK, 0 rows affected (0.00 sec)
  • 70. 5. On the Slave server (B) : a. Configure the Slave server settings and start the slave threads using command as below. Write the IP address of master server in below command at MASTER_HOST: mysql> CHANGE MASTER TO -> MASTER_HOST='172.30.0.1', -> MASTER_USER='rep', -> MASTER_PASSWORD='rep', -> MASTER_LOG_FILE= '<value_of_’File’_obtained_in_previous_step >', -> MASTER_LOG_POS= <value_of_’Position’_obtained_in_previous_step >; mysql> START SLAVE; 6. Replication setup is complete now. Execute the command SHOW SLAVE STATUS on server-B to check for its status. Setting up Replication
  • 71. Checking Replication Status (at Slave) There are various ways of checking Replication At the Slave side, a command to check the Replication status is SHOW SLAVE STATUS G An example output is shown below The Address of Master Binary Log file Number currently reading from Master Position of reading from Master Binary Log Status of the Slave threads – should be both running for replication to continue Position of Log application – should be ideally always equal to Read_Master_Log_Pos Shows error of Replication (if any) Contd..
  • 72. Checking Replication Status (at Slave) contd.. The State of different threads running on the Slave server can also be checked using the command SHOW FULL PROCESSLIST G An example output has been shown below: State for IO thread State for SQL thread
  • 73. Checking Replication Status (at Master) At the Master side, these commands can be used to check the Replication status  SHOW MASTER STATUS G  SHOW FULL PROCESSLIST; An example output is shown below The State column shows the current state of Master. A message like this one indicates Master has sent all logs to Slave, and waiting for any update
  • 75. Errors in Replication The errors encountered in a Replication setup can be seen  By checking value of “Last_Error” in the output of SHOW SLAVE SATUS command.  By checking the error log file (existing, by default, in the data directory) If an error relates to the inability of a command that cannot be executed on Slave server due to inconsistent data in it, try to resolve the error by finding out the cause of that inconsistency. If due to some reasons, the command cannot be executed on Slave server, and the only option is to skip those particular commands from being executed, then check if the skipping of those commands’ execution would not make it inconsistent or ‘not-in-sync’ with Master. Use the command to skip a number of commands from being executed SET GLOBAL SQL_SLAVE_SKIP_COUNTER = N ; START SLAVE ; where N is the number of statements that should be skipped from Master’s bin log. It is better to skip only one statement at a time and check the next statement in error log every time before skipping it.
  • 76. Stopping Replication The process of Replication can be stopped or paused temporarily at the Slave side. This can be done to perform maintenance activities or for any other purposes. There are options to stop individual Slave threads, or to completely stop the Replication process at the Slave side. For example, (i) for the purpose of backing up the Slave, updates on the Slave can be paused temporarily by stopping the SQL thread using this command STOP SLAVE SQL_THREAD ; To restart it again, use START SLAVE SQL_THREAD ; (ii) Similarly, to Stop the I/O thread from reading Master bin logs, use the command STOP SLAVE IO_THREAD ; (iii) To completely Stop the Replication process on the slave, use STOP SLAVE ; To restart it again, use START SLAVE ;
  • 78. Setting up Master-Master Replication The following steps assume the availability of two servers, server-A (Master)  172.30.0.1 server-B (Slave)  172.30.0.2 with a fresh installation of MySQL v5.5 on both. 1. Ping bothways on servers A and B, to check their network connectivity. 2. Execute the below mentioned steps on both the servers: a. Login as root user and create a user (rep) that would be used by Slave server for replication purposes. mysql> GRANT REPLICATION SLAVE ON *.* to 'rep1'@'172.30.0.2' identified by 'rep1';  Run on server-A mysql> GRANT REPLICATION SLAVE ON *.* to 'rep2'@'172.30.0.2' identified by 'rep2';  Run on server-B b. Stop the instance, and edit the file /etc/my.cnf. Add/edit these parameters in the [mysqld] # /etc/init.d/mysql.server stop [mysqld] log-bin= /var/lib/mysql/mysql-bin server-id=1  write in server-A’s my.cnf file server-id=2  write in server-B’s my.cnf file innodb_flush_log_at_trx_commit=1 sync_binlog=1 log_slave_updates=1 master-info-file= /var/lib/mysql/replication/master.info relay-log-info-file= /var/lib/mysql/replication/relay-log.info read-only =1  write only on server-B’s my.cnf file
  • 79. Setting up Master-Master Replication c. Create the directory “replication” inside data directory. The owner of this directory should be the same as the owner of mysql installation. mkdir –p /var/lib/mysql/replication chown –R mysql:mysql /var/lib/mysql/replication d. Startup both the servers # /etc/init.d/mysql.server start e. Login as root user and check Master Log position, using commands as below mysql> FLUSH TABLES WITH READ LOCK; mysql> SHOW MASTER STATUS; mysql> UNLOCK TABLES; Note down the values of ‘File’ and ‘Position’ columns from above commands’ output . An example output of above commands is as below: mysql> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (0.00 sec) mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 417 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql> UNLOCK TABLES; Query OK, 0 rows affected (0.00 sec)
  • 80. Setting up Master-Master Replication 3. Configure the Slave server settings on Server-A and start the slave threads using command as below. Write the IP address of master server in below command at MASTER_HOST: mysql> CHANGE MASTER TO -> MASTER_HOST='172.30.0.2', -> MASTER_USER='rep2', -> MASTER_PASSWORD='rep2', -> MASTER_LOG_FILE= '<value_of_’File’_obtained_in_server-B_execution >', -> MASTER_LOG_POS= <value_of_’Position’_obtained_in_server-B_execution >; mysql> START SLAVE; 4. Configure the Slave server settings on Server-B and start the slave threads using command as below. Write the IP address of master server in below command at MASTER_HOST: mysql> CHANGE MASTER TO -> MASTER_HOST='172.30.0.1', -> MASTER_USER='rep1', -> MASTER_PASSWORD='rep1', -> MASTER_LOG_FILE= '<value_of_’File’_obtained_in_server-A_execution >', -> MASTER_LOG_POS= <value_of_’Position’_obtained_in_server-A_execution >; mysql> START SLAVE;