Practical Book For Mysql
Practical Book For Mysql
1. Community
2. Enterprise
under each distribution there are different type of installation files are available. for example
1. tar ball installation also called the binary distribution or no-install installation
2. RPM installation
Binary Installation
About MySQL Binary Installation
MySQL binary installation is the easiest way of installing MySQL. If you have very little
knowledge of linux you will be able to do this installation comfortably.
Download the suitable binary package from mysql.com (as per the OS requirement)
./mysql_install_db --user=mysql
With this mysql installation is complete. Above mentioned steps are mandatory for mysql
installation and following steps are optional.
1. cp mysql/support-files/my.cnf /etc/my.cnf
(my.cnf is the configuration file through with you can customize the mysql environment)
2. bin/mysqld_safe --user=mysql &
(this will start mysql server)
3. cp support-files/mysql.server /etc/init.d/mysql.server
1. download MySQL-5.6.27-1.el6.i686.rpm-bundle.tar
1. MySQL-client-5.6.27-1.el6.i686.rpm
2. MySQL-devel-5.6.27-1.el6.i686.rpm
3. MySQL-embedded-5.6.27-1.el6.i686.rpm
4. MySQL-server-5.6.27-1.el6.i686.rpm
5. MySQL-shared-5.6.27-1.el6.i686.rpm
6. MySQL-shared-compat-5.6.14-1.el6.x86_64.rpm
7. MySQL-shared-compat-5.6.27-1.el6.i686.rpm
8. MySQL-test-5.6.27-1.el6.i686.rpm
Note : while executing step #4 you may encounter few errors like
this is because your current rpm package is conflicting with any existing package. to find out
that execute following command
rpm -qa | grep -i mysql
this will list the existing mysql packages installed on your operating system. Although you
have not manually installed these but they might have come bundled with operating system.
the output of previous command will be as follows
MySQL-devel-5.6.27-1.el6.i686
MySQL-shared-compat-5.6.27-1.el6.i686
Mysql-libs-5.1.66-2.el6_3.i686
MySQL-embedded-5.6.27-1.el6.i686
highlighted one is the package which is conflicting. we will need to remove it for successful
installation.
The following command display the status information of the server's storage
engines.
mysql> SHOW ENGINES;
+--------------------+---------+--------------------------------------------------------------
--+--------------+------+------------+
+--------------------+---------+--------------------------------------------------------------
--+--------------+------+------------+
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables
| NO | NO | NO |
+--------------------+---------+--------------------------------------------------------------
--+--------------+------+------------+
In MySQL 5.6, the default engine is InnoDB. The default storage engine is used if you do not
mention the other engine name in ENGINE option. You can specify the default engine by
using the --default-storage-engine server startup option (Command-Line Format), or by
setting the default-storage-engine option in the my.cnf configuration file.
You can set the default storage engine for the current session by setting the
default_storage_engine variable using set command.
SET default_storage_engine=ARCHIVE;
If you want to convert a table form one storage engine to another, use an ALTER TABLE
statement. See the following statement :
ALTER TABLE table1 ENGINE = InnoDB;
To store the table and column definitions for a new table, MySQL always creates an .frm file.
Depending on the storage engine the table's index and data may be stored in one or more
other files. The server creates the .frm file above the storage engine level.
InnoDB is a storage engine for MySQL that balances high reliability and high
performance. As of MySQL 5.5 and later, it is the default storage engine.
MVCC (Multiversion concurrency control) Yes Geospatial data type support Yes Geospatial indexing support
Backup / point-in-time recovery Yes Query cache support Yes Update statistics for data dictionary
Its DML operations (add, update and delete data) is ACID (atomic, consistent, isolated
and durable) model compatible, with transactions featuring commit, rollback, and crash-
recovery capabilities to protect user data.
Row-level locking (locks are placed on single records (rows)) system increase multi-user
concurrency and performance. All InnoDB locks held by a transaction are released when
the transaction is committed or aborted.
InnoDB tables arrange your data on disk to optimize queries based on primary keys.
InnoDB supports FOREIGN KEY constraints to maintain data integrity. Therefore inserts,
updates, and deletes are all checked to ensure they do not result in inconsistencies
across different tables.
It is possible to mix InnoDB tables with tables from other MySQL storage engines within
the same statement. For example, you can use a join operation to combine data from
InnoDB and MEMORY tables in a single query.
+-------+----------+------+-----+---------+-------+
+-------+----------+------+-----+---------+-------+
The following SHOW TABLE STATUS statement shows the properties of the tables (belongs
to 'tutorial' database).
mysql> SHOW TABLE STATUS FROM tutorial;
+--------+--------+---------+------------+------+----------------+-------------+---
--------------+--------------+-----------+----------------+---------------------+--
-----------+------------+-----------------+----------+----------------+---------+
+--------+--------+---------+------------+------+----------------+-------------+---
--------------+--------------+-----------+----------------+---------------------+--
-----------+------------+-----------------+----------+----------------+---------+
InnoDB provides a method that improves scalability and performance of SQL statements
that insert rows into tables with AUTO_INCREMENT columns. To use the
AUTO_INCREMENT mechanism with an InnoDB table, an AUTO_INCREMENT column
(col1 in the example) must be defined as part of an index. See the following example :
mysql> CREATE TABLE table1 (col1 INT(10) NOT NULL AUTO_INCREMENT,
MySQL supports foreign keys, which let you cross-reference related data across tables, and
foreign key constraints, which help keep this spread-out data consistent. Foreign key
definitions for InnoDB tables are subject to the following conditions :
InnoDB permits a foreign key to reference any index column or group of columns. However,
in the referenced table, there must be an index where the referenced columns are listed as
the first columns in the same order.
InnoDB does not currently support foreign keys for tables with user-defined partitioning. This
means that no user-partitioned InnoDB table may contain foreign key references or columns
referenced by foreign keys.
InnoDB allows a foreign key constraint to reference a non-unique key. This is an InnoDB
extension to standard SQL.
By default, an index key for a single-column index can be up to 767 bytes. The same length
limit applies to any index key prefix.
The InnoDB internal maximum key length is 3500 bytes, but MySQL itself restricts this to
3072 bytes (combined index key in a multi-column index).
The maximum row length except for variable-length columns (VARBINARY, VARCHAR,
BLOB and TEXT), is about 8000 bytes for the default page size of 16KB.
Internally InnoDB supports row sizes larger than 65,535 bytes, but MySQL itself imposes a
row-size limit of 65,535 for the combined size of all columns.
The maximum table space size is four billion database pages (64TB) and the minimum table
space size is slightly larger than 10MB.
MVCC (Multiversion concurrency control) No Geospatial data type support Yes Geospatial indexing support
Backup / point-in-time recovery Yes Query cache support Yes Update statistics for data dictionary
You can put the data file and index file in different directories on different physical devices to
get more speed with the DATA DIRECTORY and INDEX DIRECTORY table options to
CREATE TABLE
NULL values are permitted in indexed columns. This takes 0 to 1 bytes per key.
Support for a true VARCHAR type; a VARCHAR column starts with a length stored in one or
two bytes.
Tables with VARCHAR columns may have fixed or dynamic row length.
The sum of the lengths of the VARCHAR and CHAR columns in a table may be up to 64KB.
The mysqld (Known as MySQL Server) process is killed in the middle of a write.
Hardware failures.
Backup / point-in-time recover Yes Query cache support Yes Update statistics for data dictionary Yes
Creating MEMORY tables :
Use CREATE TABLE statement to create am MEMORY table with ENGINE
clause. As of MySQL 5.6, it is necessary to use ENGINE clause to specify the
MEMORY storage engine because InnoDB is the default engine. The
following example shows how to create and use a MEMORY table :
mysql> SELECT * FROM hr.departments;
+---------------+----------------------+------------+-------------+
+---------------+----------------------+------------+-------------+
| 60 | IT | 103 | 1400 |
|- - - - - - - -|- - - - - - - - - - - |- - - - - - |- - - - - - -|
|- - - - - - - -|- - - - - - - - - - - |- - - - - - |- - - - - - -|
+---------------+----------------------+------------+-------------+
+---------------+------------------+------------+-------------+
+---------------+------------------+------------+-------------+
+---------------+------------------+------------+-------------+
The following SHOW TABLE STATUS statement shows the properties of the
tables (belongs to 'tutorial' database).
mysql> SHOW TABLE STATUS FROM tutorial;
+--------+--------+---------+------------+------+----------------+-------------+---
----------------+--------------+-----------+----------------+---------------------+
---------------------+------------+-----------------+----------+----------------+--
-------+
+--------+--------+---------+------------+------+----------------+-------------+---
----------------+--------------+-----------+----------------+---------------------+
---------------------+------------+-----------------+----------+----------------+--
-------+
| table1 | InnoDB | 10 | Compact | 0 | 0 | 16384 |
0 | 0 | 0 | 1 | 2014-02-14 13:16:16 |
NULL | NULL | utf8_general_ci | NULL | |
|
ENGINE = MEMORY;
ENGINE = MEMORY;
In-memory storage for fast access and low latency. Data volume can fit entirely in memory
without causing the operating system to swap out virtual memory pages.
By default, an index key for a single-column index can be up to 767 bytes. The same length
limit applies to any index key prefix.
The InnoDB internal maximum key length is 3500 bytes, but MySQL itself restricts this to
3072 bytes (combined index key in a multi-column index).
The maximum row length except for variable-length columns (VARBINARY, VARCHAR,
BLOB and TEXT), is about 8000 bytes for the default page size of 16KB.
Internally InnoDB supports row sizes larger than 65,535 bytes, but MySQL itself imposes a
row-size limit of 65,535 for the combined size of all columns.
The maximum tablespace size is four billion database pages (64TB) and the minimum
tablespace size is slightly larger than 10MB.
mysql> CREATE TABLE tabl2 (rollno INT NOT NULL AUTO_INCREMENT PRIMARY KEY, class CH
AR(5), student_name CHAR(40)) ENGINE = MyISAM;
mysql> CREATE TABLE tabl3 (rollno INT NOT NULL AUTO_INCREMENT PRIMARY KEY, class CH
AR(5), student_name CHAR(40)) ENGINE = MyISAM;
Query OK, 0 rows affected (0.09 sec)
mysql> INSERT INTO tabl1 (class, student_name) VALUES ('V','Steven'), ('V', 'Neena'
);
mysql> INSERT INTO tabl2 (class, student_name) VALUES ('VI','Lex'), ('VI', 'Alexand
er');
mysql> INSERT INTO tabl3 (class, student_name) VALUES ('VII','Bruce'), ('VII', 'Dav
id');
mysql> CREATE TABLE allclass (rollno INT NOT NULL, class CHAR(5), student_name CHAR
(40)) ENGINE = MERGE UNION = (tabl1, tabl2, tabl3) INSERT_METHOD = LAST;
+--------+-------+--------------+
+--------+-------+--------------+
| 1 | V | Steven |
| 2 | V | Neena |
| 1 | VI | Lex |
| 2 | VI | Alexander |
| 1 | VII | Bruce |
| 2 | VII | David |
+--------+-------+--------------+
The following SHOW TABLE STATUS statement shows the properties of the
tables (belongs to 'tutorial' database).
mysql> SHOW TABLE STATUS FROM tutorial;
+----------+------------+---------+------------+------+----------------+-----------
--+-------------------+--------------+-----------+----------------+----------------
-----+---------------------+------------+-----------------+----------+-------------
---+---------+
+----------+------------+---------+------------+------+----------------+-----------
--+-------------------+--------------+-----------+----------------+----------------
-----+---------------------+------------+-----------------+----------+-------------
---+---------+
+------+------------+--------+
+------+------------+--------+
| 1 | IndianRed | CD5C5C |
| 2 | LightCoral | F08080 |
| 3 | Salmon | FA8072 |
+------+------------+--------+
You can can read, modify the 'color.CSV' file by spreadsheet applications
such as Microsoft Excel or StarOffice Calc.
CSV Limitations :
Does not support indexing.
All columns must have the NOT NULL attribute in a CSV table.
The following SHOW TABLE STATUS statement shows the properties of the
tables (belongs to 'tutorial' database).
mysql> SHOW TABLE STATUS FROM tutorial;
+----------+------------+---------+------------+------+----------------+-----------
--+-------------------+--------------+-----------+----------------+----------------
-----+---------------------+------------+-----------------+----------+-------------
---+---------+
+----------+------------+---------+------------+------+----------------+-----------
--+-------------------+--------------+-----------+----------------+----------------
-----+---------------------+------------+-----------------+----------+-------------
---+---------+
Backup / point-in-time recovery Yes Query cache support Yes Update statistics for data dictionary Yes
ORDER BY operations
BLOB columns
The server creates a table format file (.frm extension) in the database directory.
To enable the EXAMPLE storage engine if you build MySQL from source,
invoke CMake with the -DWITH_EXAMPLE_STORAGE_ENGINE option.
The following SHOW TABLE STATUS statement shows the properties of the
tables (belongs to 'tutorial' database).
mysql> SHOW TABLE STATUS FROM tutorial;
+----------+------------+---------+------------+------+----------------+-----------
--+-------------------+--------------+-----------+----------------+----------------
-----+---------------------+------------+-----------------+----------+-------------
---+---------+
+----------+------------+---------+------------+------+----------------+-----------
--+-------------------+--------------+-----------+----------------+----------------
-----+---------------------+------------+-----------------+----------+-------------
---+---------+
Using CONNECTION
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://feduser@remote_host:9306/federated/test10_table';
Where :
scheme : A recognized connection protocol. Only mysql is supported as the scheme value at
this point.
The user name for the connection, must have been created on the remote server, and have
suitable privileges to perform the required actions like SELECT, INSERT, UPDATE, and so
forth on the remote table.
The password for user_name. (Optional)
port_num: The port number (default : 3306) for the remote server. (Optional)
Using CREATE SERVER : To use this method, you must specify the
CONNECTION string after the engine type in a CREATE TABLE statement.
See the following example :
CREATE SERVER
server_name
Transactions Yes No
MVCC Yes No
T-tree indexes No No
Hash indexes No No
Feature InnoDB MyISAM
Transactions Yes No
MVCC Yes No
T-tree indexes No No
Hash indexes No No
8. To drop a database :
Mysql>Drop database <database name>
16. To see the location where the physical files are stored.
Mysql>show variables like ‘%data%’;
USER MANAGEMENT
LOGICAL BACKUPS:
We use MYSQL DUMP and MYSQL IMPORT utilities for taking logical
backups.in logical backups we can take backup of all databases , a single database
and even a single table backup . MYSQL DUMP is similar to EXPORT in oracle and
MYSQL IMPORT is similar to IMPORT in oracle.
$MYSQLDUMP –u root –p –all-databases > alldb.sql {full database backup}
$MYSQLDUMP –u root –p <database name> outfile.sql { single database
backup}
$MYSQLDUMP –u root –p <database name> <table name> > outfile.sql
{backing up a single table}
If we want a backup of table into a text file then use the following:
Connect to any database
Mysql>select * from <table name> into outfile ‘outfile.txt’;
When taking the backup into TEXT file from sql prompt the particular file will
be stored in DATADIR location. When taking backup into .SQL file the file is stored in
OS location from where we issue the MYSQLDUMP command.
Replication is the most trusted and tested way of making your data redundant. using
replication you can overcome the problem of SPOF (single point of failure). A slave
can be used in multiple way
1. enable binary logging : put the parameter log-bin in the my.cnf and restart the
instance
if the replication is successfully started you will see the following output
*************************** 1. row ***************************
2. slave reads the binary logs and puts the changes into relay log with IO thread
IO Thread : responsible for making coinnection with the master and reading binary
logs and writing into the relay log. it fails when there is a problem connecting to the
master either because of network issues like physical network break or the firewall or
if the master server is down because of any reason.
SQL thread : sql thread executes the contents of relay log. if there is problem at the
data level at the slave thew sql thread will fail to update or carry on the replication.
1. object does not exist : database / table
2. key not found : if the primary is there in the table and if somone has by mistake or
intentionally removed the row from the slave and the statment on the same key
comes from the master. if slave already has a primary key which is not there at the
master yet and later the same key value comes from the master.
1. master - slave
2. master
slave slave
Edit the /etc/mysql/my.cnf file on each of the Linodes. Add or modify the following
values:
Server 1:
/etc/mysql/my.cnf
1server_id =1
2log_bin = /var/log/mysql/mysql-bin.log
3log_bin_index = /var/log/mysql/mysql-bin.log.index
4relay_log = /var/log/mysql/mysql-relay-bin
5relay_log_index = /var/log/mysql/mysql-relay-bin.index
6expire_logs_days = 10
7max_binlog_size = 100M
8log_slave_updates = 1
9auto-increment-increment = 2
10auto-increment-offset = 1
Server 2:
/etc/mysql/my.cnf
1server_id =2
2log_bin = /var/log/mysql/mysql-bin.log
3log_bin_index = /var/log/mysql/mysql-bin.log.index
4relay_log = /var/log/mysql/mysql-relay-bin
5relay_log_index = /var/log/mysql/mysql-relay-bin.index
6expire_logs_days = 10
7max_binlog_size = 100M
8log_slave_updates = 1
9auto-increment-increment = 2
10auto-increment-offset = 2