3. Configurationfiles:
auto.cnf:Contains server_uuid
my.cnf:MySQLConfigurationfile.
MySQL Log files:There fewimportantlogfilesprovidedbyMySQLto trace andstore varioustypesof database relatedoperationsandactivities.
Error log: Alsocalledas‘MySQLServerLogs’.By Defaultenableanddefaultlocationisdatadirectory.MySQLserver/ErrorLogfile for loggingserveractivitieslike startup-shutdown
events,crash-recoveryevents,connectionerrors etc.MySQLserverlogfiletorecord mysqldserveractivitiesasNOTE,WARNING,ERRORon the pre-definedseveritybasis.
Variable:log_error
log-error= mysqld.log
General Log:By Defaultthislogisdisable,we canenable anddisable itdynamicallywithout downtime.To logclientqueries.Enableditwhenyouwanttotrouble shootqueries
executedfromclienttosee whatclientsendingto mysqldserver.
Variables:
general_log
general_log_file
Slow Querylog:By Defaultthislogisdisable,we canenable anddisableitdynamicallywithoutdowntime.
The slowquerylogcontainSQL querywithadditional infothattookmore thanspecified long_query_time(inseconds) toexecute.Thresholdtime insecondsforaqueryexecution,to
mark thatas slowquery.
Variables:
slow_query_log
slow_query_log_file
Binary log:Alsocalledas‘binlog’,itcontainseventsfordatabase relatedchangeslike tablecreationanddatachanges.
By Defaultthislogisdisable,we cannotenableanddisableitdynamicallyandrequiresdowntime.
Thislogis veryuseful forPointinTime recoveryaswell asforReplication,itismandatoryto binlogenabledonMySQLserverif youwant to useMySQLReplication.
Binaryfile hasbinaryformat,tosee the contentsinthisfile we can usemysqlbinlogutility whichwillconvertthisfile intextformatandsame canbe usedfor Pointintime restore to
applyincrementalchanges.
4. Miscellaneous files:basedir=dir_nameDirectory locationto the MySQL binaries
installation.
datadir=dir_name
Directory location to the MySQLdatabase data, status and log files.
pid-file=file_name
Filenamein which the mysqld server should write its process IDinformation.
socket=file_name, -S file_name
On Unix, the name of the Unix socket file to use, for connections made using a
named pipe to a local server
8. MyISAM
MyISAM extends the former ISAM storage engine. The MyISAM tables are optimized for compression and speed. MyISAM tables are also portable between platforms and
operating systems.
The size of the MyISAM table can be up to 256TB, which is huge. In addition, MyISAM tables can be compressed into read-only tables to save spaces. At startup, MySQL checks
MyISAM tables for corruption and even repairs them in a case of errors. The MyISAM tables are not transaction-safe.
Before MySQL version 5.5, MyISAM is the default storage engine when you create a table without specifying the storage engine explicitly. From version 5.5, MySQL uses InnoDB
as the default storage engine.
InnoDB
The InnoDB tables fully support ACID-compliant and transactions. They are also optimal for performance. InnoDB table supports foreign keys, commit, rollback, roll-forward
operations. The size of an InnoDB table can be up to 64TB.
Like MyISAM, the InnoDB tables are portable between different platforms and operating systems. MySQL also checks and repairs InnoDB tables
MERGE
A MERGE table is a virtual table that combines multiple MyISAM tables that have a similar structure to one table. The MERGE storage engine is also known as the MRG_MyISAM
engine. The MERGE table does not have its own indexes; it uses indexes of the component tables instead.
Using MERGE table, you can speed up performance when joining multiple tables. MySQL only allows you to perform SELECT, DELETE, UPDATE and INSERT operations on the
MERGE tables. If you use DROP TABLE statement on a MERGE table, only MERGE specification is removed. The underlying tables will not be affected.
Memory
The memory tables are stored in memory and use hash indexes so that they are faster than MyISAM tables. The lifetime of the data of the memory tables depends on the
uptime of the database server. The memory storage engine is formerly known as HEAP.
Archive
The archive storage engine allows you to store a large number of records, which for archiving purpose, into a compressed format to save disk space. The archive storage engine
compresses a record when it is inserted and decompress it using the zlib library as it is read.
The archive tables only allow INSERT and SELECT statements. The ARCHIVE tables do not support indexes, so it is required a full table scanning for reading rows.
, if necessary, at startup.
9. MERGE
A MERGE table is a virtual table that combines multiple MyISAM tables that have a similar structure
to one table. The MERGE storage engine is also known as the MRG_MyISAM engine. The MERGE
table does not have its own indexes; it uses indexes of the component tables instead.
Using MERGE table, you can speed up performance when joining multiple tables. MySQL only allows
you to perform SELECT, DELETE, UPDATEand INSERT operations on the MERGE tables. If you use
DROPTABLE statementon a MERGE table, only MERGE specification is removed. The underlying
tables will not be affected.
Memory
The memory tables are stored in memory and use hash indexes so that they are faster thanMyISAM
tables. The lifetime of the data of the memory tables depends on the uptime of the database server.
The memory storage engine is formerly known as HEAP.
Archive
The archive storage engine allows you to store a large number of records, which for archiving
purpose, into a compressed format to save disk space. The archive storage engine compresses a
record when it is inserted and decompress it using the zlib library as it is read.
The archive tables only allow INSERT and SELECT statements. The ARCHIVEtables do not support
indexes, so it is required a full table scanning for reading rows.
10. CSV
The CSV storage engine stores data in comma-separated values (CSV)
file format. A CSV table brings a convenient way to migrate data into
non-SQL applications such as spreadsheet software.
CSV table does not support NULL data type. In addition, the read
operation requires a full table scan.
FEDERATED
The FEDERATED storage engine allows you to manage data from a
remote MySQL server without using the cluster or replication
technology. The local federated table stores no data. When you query
data from a local federated table, the data is pulled automatically from
the remote federated tables.
11. • Storage engine:
MySQL componentthat manages physicaldata (file management) and locations. Storageengine responsiblefor SQL statementexecution
and fetching data fromdata files. Useas a plugin and can load/unload fromrunning MySQL server. Few of them as following,
• InnoDB :
• Fully transactionalACID.
• Offers REDO and UNDO for transactions.
• Data storagein tablespace:
• – Multiple data files
– Logical objectstructureusing InnoDBdata and log buffer
• Row-levellocking.
• NDB (For MySQL Cluster):
• Fully Transactionaland ACID Storageengine.
• Distribution execution of data and using multiple mysqld.
• NDB uselogical data with own buffer for each NDB engine.
• Offers REDO and UNDO for transactions.
• Row-levellocking.
• MyISAM:
• Non-transactionalstorageengine
• Speed for read
• Data storagein files and use key, metadata and query cache
12. • – FRM for table structure
– MYI for table index
– MYD for table data
• Table-level locking.
• MEMORY:
• Non-transactionalstorageengine
• All data stored in memory other than table metadata and structure.
• Table-level locking.
• ARCHIVE:
• Non-transactionalstorageengine,
• Storelarge amounts of compressed and unindexed data.
• Allow INSERT, REPLACE, and SELECT, but not DELETE or UPDATE sql operations.
• Table-level locking.
• CSV:
• Stores data in flat files using comma-separated values format.
• Table structureneed be created within MySQL server (.frm)
•
13. mysql>select*from information_schema.engines;
OR
mysql>showengines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support|Comment | Transactions|XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MEMORY | YES | Hash based,storedinmemory,usefulfortemporarytables | NO | NO | NO |
| MRG_MYISAM | YES | Collectionof identical MyISAMtables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| FEDERATED | NO | FederatedMySQLstorage engine | NULL | NULL | NULL |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MyISAM | YES | MyISAMstorage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supportstransactions,row-level locking,andforeignkeys | YES | YES | YES |
| BLACKHOLE | YES | /dev/null storage engine(anythingyouwrite toitdisappears) |NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------------------------------------------
Change defaultsetting ofEngine inini file parameter
[mysqld]
# The defaultstorage engine thatwill be usedwhencreate newtableswhen
default-storage-engine=INNODB
Or
Command line duringstarting:
--default-storage-engine=InnoDB
14. mysql> select * from engines;
+------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ENGINE | SUPPORT | COMMENT | TRANSACTIONS| XA | SAVEPOINTS|
+------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | YES | Supports transactions,row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM |YES | Collection of identical MyISAM tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storageengine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storageengine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | NO | FederatedMySQL storage engine | NULL | NULL | NULL |
| ARCHIVE | YES | Archive storageengine | NO | NO | NO |
| MyISAM | DEFAULT | Default engine as of MySQL 3.23with great performance | NO | NO | NO |
+------------+---------+----------------------------------------------------------------+--------------+------+------------+
8 rows in set (0.00 sec)
16. Start MySQL Server
Start MySQL Server on Linux
On Linux, you can start the server with the following commands using service, init.d, and systemd.
Start MySQL Server using service
sudo service mysql start
Start MySQL Server using using init.d
sudo /etc/init.d/mysql start
Start MySQL Server using systemd
sudo systemctl start mysqld
Start MySQL Server on Windows
On Windows, you can start the MySQL Server using the mysqld program as follows:
First, open the Run dialog by pressing Windows+R keyboards:
Second, type cmd and press Enter:
Third, type mysqld and press Enter:
mysqld
If the bin folder is not in the Windows path environment, you can navigate to the bin folder e.g., C:Program
FilesMySQLMySQL Server 8.0bin and use the mysqld command.
17. Stop MySQL Server
Stop MySQL Server on Windows
On Windows, you can stop MySQL Server using the program mysqladmin.
The program mysqladmin locates in the folder <path_to_installation_dir>bin, where path_to_installation_dir is the path to the
installation directory e.g., C:Program FilesMySQLMySQL Server 8.0
Typically, you should add the pathname of the MySQL bin directory to Windows path environment variable to access any MySQL
programs in the bin directory faster.
To stop MySQL, you follow these steps:
First, launch the Command Prompt by pressing Windows+R to open the Run box and type cmd and press Enter.
Second, navigate to the bin folder of the MySQL if it is not in the Window path environment.
Third, use the following command to stop MySQL Server:
mysqladmin -u root -p shutdown
Enter password: ********
It prompts for a password of the root account. You need to enter the password and press the Enter keyboard.
The program will stop the MySQL Server.
18. Stop MySQL Server
Stop MySQL Server on Linux
To stop MySQL Server on Linux, you use the following command:
/etc/init.d/mysqldstop
Some Linux distributions provide server command:
service mysqldstop
Or
service mysql stop
RestartMySQL Server
You use the following commandto restart the MySQL server On Linux:
service mysql restart
If the name is MySQL service is mysqldnot mysql, you need to change the service name in the command
as shown in the following command:
service mysqld restart
Or you can use the init.d to start the MySQL service:
/etc/init.d/mysqld restart
19. User Management
CREATE USER [IF NOT EXISTS] account_name IDENTIFIED BY 'password';
mysql -u root -p
Enter password: ********
mysql> create user bob@localhost identified by 'Secure1pass!';
mysql -u bob -p
Enter password: ********
mysql> show databases;
use bobdb
mysql> select user from mysql.user;
mysql> grant all privileges on bobdb.* to bob@localhost;
mysql> show tables;
SHOW GRANTS FOR super@localhost;
SHOW GRANTS;
HOW GRANTS FOR CURRENT_USER;
SHOW GRANTS FOR CURRENT_USER();
20. SELECT current_role();
mysql> select user, host from mysql.user;
mysql> drop user dbadmin@localhost
RENAME USER old_user1 TO new_user;
SELECT host, user FROM mysql.user WHERE user = 'doe' and host = 'localhost';
RENAME USER fx TO fc;
USE mysql;
Change MySQL user password using UPDATE statement
UPDATE user
SET authentication_string = PASSWORD('dolphin’) WHERE user = 'dbadmin' AND host = 'localhost';
FLUSH PRIVILEGES;
Change MySQL user password using the SET PASSWORD statement
SET PASSWORD FOR 'dbadmin'@'localhost' = PASSWORD('bigshark');
SET PASSWORD FOR 'dbadmin'@'localhost' = bigshark;
Change MySQL user password using ALTER USER statement
ALTER USER dbadmin@localhost IDENTIFIED BY 'littlewhale';
21. DB Login & list of Database
C:ProgramFilesMySQLMySQL Workbench 8.0 CE>mysql-u dmapuser -h 198.148.1.110-P 3306 dmap -p
Enter password: *************
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 47
Server version: 8.0.17 MySQL Community Server - GPL
Copyright(c) 2000, 2019, Oracleand/or its affiliates. All rights reserved.
Oracleis a registered trademark of OracleCorporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or 'h' for help. Type 'c' to clear the currentinput statement.
mysql>show databases;
+--------------------+
| Database |
+--------------------+
| dmap |
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)
22. SELECT user(); -- Current user
SELECT current_user();
SELECT user, host, db, command FROM information_schema.processlist;
SELECT user FROM mysql.user GROUP BY user;
SHOW FULL TABLES;
mysql> use globalmap
Database changed
mysql> show tables;
+------------------------------+
| Tables_in_globalmap |
+------------------------------+
| code_object_validation |
| column_constraint_validation |
| data_object_validation |
| db_details_main |
| db_error_master |
| db_migration_plan |
| db_object |
| db_object_error |
| db_object_type_validation |
| db_objects_type |
| parameter_execution |
| parameter_value_store |
| user_detail |
23. Backup
mysqldump -u dmapuser -h 192.168.1.11-P 3306 dmap -p > C:databasebackupMySQL_Backupdmap_backup_mysql_on2410.sql
C:ProgramFilesMySQLMySQL Workbench 8.0 CE>mysqldump -u dmapuser -h 192.168.1.11-P 3306 dmap -p >
C:databasebackupMySQL_Backupdmap_backup_mysql_on2410.sql
Enter password: *************
C:ProgramFilesMySQLMySQL Workbench 8.0 CE>
mysqldump --databases database_onedatabase_two >two_databases.sql
mysqldump --all-databases >all_databases.sql
shell> mysqldump --databases db1 db2 db3 >dump.sql
Tables Backup
mysqldump -u{backup_user} -p{backup_password} from_db_nametable_to_backup > backup_file.sql
mysqldump -u mkyong -p wrdp1 wp_postmeta >wp_postmeta.sql
mysqldump -u dmapuser -h 192.168.1.11-P 3306 dmap -p code_object_validation > C:databasebackupMySQL_Backupcode_object.sql
Restoring aBackup
mysqldatabase_name< database_name.sql
To dump only specific tables froma database, name them on the command line following the database name:
shell> mysqldump testt1 t3 t7 > dump.sql
24. Backup
Back a table ‘wp_postmeta’ to a SQL file ‘wp_postmeta.sql’
$ mysqldump -u mkyong -p wrdp1 wp_postmeta > wp_postmeta.sql
Enter password:
# backup + gzip
$ mysqldump -u mkyong -p wrdp1 wp_postmeta | gzip > wp_postmeta.sql
#multiple tables - wp_postmeta and wp_posts
$ mysqldump -u mkyong -p wrdp1 wp_postmeta wp_posts > tables.sql
25. MySQL Export Table to CSV
SELECT orderNumber, status, orderDate, requiredDate, comments FROM orders WHERE status = 'Cancelled'
INTO OUTFILE 'C:/tmp/cancelled_orders.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ';'
ESCAPED BY '"'
LINES TERMINATED BY 'rn';
Exporting data with column headings
(SELECT 'Order Number','Order Date','Status')
UNION
(SELECT orderNumber,orderDate, status FROMorders INTO OUTFILE 'C:/tmp/orders.csv’ FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '“’ LINES TERMINATED
BY 'rn');
Handling NULL values
SELECT orderNumber, orderDate, IFNULL(shippedDate, 'N/A’) FROM orders INTO OUTFILE 'C:/tmp/orders2.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ';'
ESCAPED BY '"' LINES
TERMINATED BY 'rn';
26. Import CSV File Into MySQL Table
CREATE TABLE discounts (
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
expired_date DATE NOT NULL,
amount DECIMAL(10, 2 ) NULL,
PRIMARY KEY (id)
);
LOAD DATA INFILE 'c:/tmp/discounts.csv'
INTO TABLE discounts
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY 'n'
IGNORE 1 ROWS;
27. Transforming data while importing
Sometimes the format of the datadoes not match the target columnsin the table. In simple cases, you can
transform it by using the SET clause in the LOAD DATA INFILE statement.
Suppose the expired date column in the discount_2.csv file is in mm/dd/yyyy format.
LOAD DATA INFILE 'c:/tmp/discounts_2.csv'
INTO TABLE discounts
FIELDS TERMINATEDBY ',' ENCLOSED BY '"'
LINES TERMINATEDBY 'n'
IGNORE 1 ROWS
(title,@expired_date,amount)
SET expired_date= STR_TO_DATE(@expired_date,'%m/%d/%Y');
28. Basic Scripts-1
1. Access monitor: mysql -u[username] -p;(will prompt for password)
2. Show all databases: showdatabases; --The SHOWSCHEMAS command is a synonymfor SHOWDATABASES, thereforethe following
command returns the same result
3. SHOWDATABASES LIKE'%schema’;
4. SELECT schema_name FROM information_schema.schemata;
5. Access database: mysql -u[username]-p[database] (will prompt for password)
6. Create newdatabase: create database[database];
7. Select database: use [database];
8. Determine what database is inuse: selectdatabase();
9. Show all tables: showtables; The SHOWTABLES command allows youto showif a table is a base table or a view. To include the table type in
the result, youuse the following formof the SHOWTABLES statement.
10. SHOWFULL TABLES
11. SHOWTABLES LIKE 'p%’;
12. SHOWTABLES LIKE '%es’;
13. SHOWFULL TABLES WHERE table_type ='VIEW’;
14. SHOWTABLES FROM mysql LIKE'time%’;
15. SHOWTABLES IN mysql LIKE 'time%';
16. Show table structure: describe [table];
17. List all indexes onatable: show index from[table];
18. Create newtable withcolumns: CREATETABLE[table]([column]VARCHAR(120), [another-column]DATETIME);
19. Adding a column: ALTER TABLE[table]ADD COLUMN [column]VARCHAR(120);
20. Adding a column withan unique, auto-incrementing ID: ALTER TABLE[table]ADD COLUMN [column]int NOTNULL AUTO_INCREMENT
PRIMARY KEY;
21. Inserting arecord: INSERTINTO [table]([column], [column]) VALUES ('[value]', [value]');
29. Basic Scripts-2
1. MySQL functionfor datetime input: NOW()
2. Selecting records: SELECT*FROM[table];
3. Explainrecords: EXPLAINSELECT*FROM[table];
4. Selecting parts of records: SELECT[column], [another-column]FROM[table];
5. Counting records: SELECTCOUNT([column]) FROM[table];
6. Counting and selecting grouped records: SELECT*, (SELECT COUNT([column]) FROM[table]) AS countFROM[table] GROUP BY
[column];
7. Selecting specific records: SELECT* FROM[table] WHERE [column] = [value]; (Selectors: <, >, !=; combine multiple selectors
with AND, OR)
8. Select recordscontaining[value]:SELECT* FROM [table]WHERE[column]LIKE '%[value]%';
9. Select recordsstarting with[value]:SELECT* FROM [table]WHERE[column]LIKE '[value]%';
10. Select recordsstarting withval andending with ue: SELECT * FROM [table]WHERE[column] LIKE '[val_ue]';
11. Select arange:SELECT * FROM [table]WHERE[column]BETWEEN [value1]and [value2];
12. Select withcustomorder andonly limit:SELECT * FROM [table]WHERE[column]ORDER BY [column] ASC LIMIT
[value];(Order: DESC, ASC)
13. Updating records:UPDATE[table]SET[column] = '[updated-value]'WHERE[column] = [value];
14. Deleting records:DELETEFROM [table]WHERE[column]= [value];
15. Delete all recordsfroma table (without dropping the table itself): DELETEFROM [table];(This alsoresets the incrementing
counter for autogenerated columns like an id column.)
16. Delete all records in atable:truncate table [table];
17. Removing table columns:ALTER TABLE[table]DROP COLUMN[column];
18. Deleting tables:DROP TABLE[table];
19. Deleting databases:DROP DATABASE[database];
20. Customcolumn output names:SELECT [column] AS [custom-column]FROM [table];
21. Export a database dump (more info here):mysqldump -u[username]-p[database]> db_backup.sql
22. Use --lock-tables=falseoptionfor lockedtables (more info here).
23. Import a database dump (more infohere):mysql -u[username]-p-h localhost [database] <db_backup.sql
24. Logout:exit;
25.
30. Show current logged users
SELECT user, host, db, command FROM information_schema.processlist;
+-------+----------------+---------------+---------+
| user | host | db | command |
+-------+-----------------+---------------+---------+
| local | localhost:50591 | classicmodels | Sleep |
| root | localhost:50557 | NULL | Query |
+-------+-----------------+---------------+---------+
2 rows in set (0.00 sec)
MySQL SHOW PROCESSLIST
mysql>SHOW PROCESSLIST;
+----+-----------------+-----------------+---------------+---------+------+------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------------+---------------+---------+------+------------------------+------------------+
| 4 | event_scheduler | localhost | NULL | Daemon | 2246 | Waiting on empty queue | NULL |
| 14 | root | localhost:50924 | NULL | Query | 0 | starting | SHOW PROCESSLIST |
| 15 | car | localhost:50933 | classicmodels | Sleep | 2 | | NULL |
+----+-----------------+-----------------+---------------+---------+------+------------------------+------------------+
3 rows in set (0.00 sec
DESCRIBE orders;
SHOW COLUMNS FROM table_name;
the following statement lists all columns of the payments table in the classicmodels database.
SHOW FULL COLUMNS FROM payments G;
SHOW COLUMNS FROM table_name LIKE pattern;
SHOW COLUMNS FROM table_name WHERE expression;
31. showauthors - displays names, locations andauthors
show binlog events - see replication
showbinary logs - see replication
showcharacter set
showcollation
showcolumns - informationabout table fields
showcontributors - displays names, locations andcontributors
showcreate [database|event|function|
procedure|schema|trigger|view] - display how the object was created
showdatabases
show[engine|engines]
showerrors
showtable status
showstorage engines
showtables
showtriggers
showvariables
showwarnings
show processlist- display the currently running processes inMySQL
showprofile - this is a newer commandand queries the profiling that can
be done on a session-levelbasis
showschemas - same as show databases
show events - dislays database eventsincluding the time the event
happened
show full tables - same as showtables but withadditional column that
specifies whether the tableis abase table, view or systemview
show functioncode - displays the ordinal positionandinstructionsfor
eachstepina storedfunction
show functionstatus - displays all functions associatedwithall
databases
show grants
show [index|indexes]
show keys
show master status - see replication
show open tables - display all open tables
show plugins - displays all plugins including storage engineplugins
show privileges - display the name, context andcomment about each
type of privilege that canbe usedina grant statement
show procedure code
show procedure status
show slave host - see replication
show slave status - see replication
show status - displays the status of the systemagoodcommand tostart
with
The show extension can obtain metadata information from the information_schema database,
32. Table Maintenance
• check table - used to determine if a table is corrupted (options are extended, meduim, changed, fast, quick, for
upgrade)
• repair table - used to repair a table (optionsare quick, extended use_frm)
• checksum table - obtaina checksum for the table (optionsare quick, extended), see above for checksum's on tables
• analyze table - updatesthe statisticsof a table to help the optimizer
• optimize table - resolves data and index fragmentation
MySQL provides several useful statementsthat allow you to maintaindatabasetables effectively. Those statements
enable you to analyze, optimize, check, and repair database tables.
Analyze table statement
ANALYZE TABLE payments;
Optimize table statement
OPTIMIZE TABLE table_name;
Check table statement
CHECK TABLE table_name;
Repair the table statement
REPAIR TABLE table_name;
33. Backup - Using the mysqldump tool
1) Using the mysqldump tool to make a backup of a single database
mysqldump --user=<username> --password=<password> --result-file=<path_to_backup_file> --databases <database_name>
2) Using the mysqldump tool to make a backup of multiple databases
To make a backup of multiple databases,you specifya list of the database names after the--database option:
mysqldump --user=<username> --password=<password> --result-file=<path_to_backup_file> --databases <dbname1>[,<dbname2>, ...]
mysqldump --user=root--password=Supe!rPass1 --result-file=c:backupclassicmodels_world.sql --databases classicmodels world
3) Using the mysqldump tool to make a backup of all databases
To make a backup of all databases in a MySQL Server, you use the –all-database option:
mysqldump --user=<username> --password=<password> --result-file=<path_to_backup_file> --all-databases
4) Using the mysqldump tool to make a backup of specific tablesfrom a database
To make a backup of specific tables from a database, you use the followingcommand:
mysqldump --user=<username> --password=<password> --result-file=<path_to_backup_file> <database_name> <table_name>
5) Using a mysqldump tool to make a backup of databasestructure only
To make a backup of the database structure only,you use the--no-data option:
mysqldump --user=<username> --password=<password> --result-file=<path_to_backup_file> --no-data --databases <database_name>
6) Using the mysqldump tool to make a backup of data only
To make a backup of the database data only,you use the--no-create-info option:
mysqldump --user=<username> --password=<password> --result-file=<path_to_backup_file> –-no-create-info--databases
<database_name>
34. MySQLRestore: Restoring from a Dump File
Use the source command to load data into the MySQL Server:
First, connect to MySQL server:
C:>mysql -u root -p
Enter password: ********
Second, drop the mydb database:
mysql>drop database mydb;
Third, use the SOURCE command to load the dump file:
mysql>source c:backupmydb.sql
mysql> source c:tempmysqlsampledatabase.sql)
mysql> show databases;
35. MySQL CREATE TABLE statement examples
Let’s take some examples of creatingnew tables.
1) MySQL CREATE TABLE simple example
The followingstatement creates a new table named tasks:
CREATE TABLE IF NOT EXISTS tasks (
task_id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
start_date DATE,
due_date DATE,
status TINYINT NOT NULL,
priorityTINYINT NOT NULL,
description TEXT,
created_at TIMESTAMPDEFAULT CURRENT_TIMESTAMP
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS checklists (
todo_idINT AUTO_INCREMENT,
task_id INT,
todo VARCHAR(255) NOT NULL,
is_completed BOOLEAN NOT NULL DEFAULT
FALSE,
PRIMARY KEY (todo_id , task_id),
FOREIGN KEY (task_id)
REFERENCEStasks (task_id)
ON UPDATE RESTRICT ON DELETE CASCADE
);
36.
37. Numeric Types Description
TINYINT A very small integer
SMALLINT A small integer
MEDIUMINT A medium-sized integer
INT A standard integer
BIGINT A large integer
DECIMAL A fixed-point number
FLOAT
A single-precision
floating point number
DOUBLE
A double-precision
floating point number
BIT A bit field
String Types Description
CHAR
A fixed-length nonbinary (character)
string
VARCHAR A variable-length non-binary string
BINARY A fixed-length binary string
VARBINARY A variable-length binary string
TINYBLOB A very small BLOB (binary large object)
BLOB A small BLOB
MEDIUMBLOB A medium-sized BLOB
LONGBLOB A large BLOB
TINYTEXT A very small non-binary string
TEXT A small non-binary string
MEDIUMTEXT A medium-sized non-binary string
LONGTEXT A large non-binary string
ENUM
An enumeration; each column value
may be assigned one enumeration
member
SET
A set; each column value may be
assigned zero or more SET members
Date and Time Types Description
DATE A date valuein CCYY-MM-DD format
TIME A time value in hh:mm:ss format
DATETIME A date and time value inCCYY-MM-DDhh:mm:ssformat
TIMESTAMP A timestamp valuein CCYY-MM-DD hh:mm:ss format
44. Backups and Recovery
logical backup
this type of backup is createdby saving informationthat represents the logical database structures using SQL statementslike
create database, createtable andinsert. This type of backupis ideal whenyou want to upgrade fromone versionof MySQL to
another however it is a slower methodof backing up.
physical backup
this type of backup is a backup of the actual database files or disk partitions, this type of backup can be very fastto backup and
restore.
full backup
a full backup is a standalone backup containing everything in the database, this could then be restored on another server. A full
backup can be either logical or physical.
incremental backup
this type of backup only contains the data that has changed fromthe lastbackup. The advantageof this type of backup is that it is
faster as there is not some much data to backup, however the disadvantageis that it takes longer to recover.
consistentbackup this is a backup at an exact moment in time, generally you shutdown the database(or quiescent mode) then take the backup.
hot backup this type of backup is taken when the databaseis running, during the backup both reads and writes are not blocked
warm backup
this type of backup is taken when the databaseis running, however reads arenot blocked but writes are prohibited frommaking
any modifications to the database.
cold backup similar to a consistentbackup as the databaseis shutdown beforethe backup begins
point-in-time restore
is a restoration of a databaseto a specified date and time , somedatabases use a full backup and recovery logs to restoreto that
point-in-time, others can only usethe last full backup which means that data might have to be re-keyed into the system.
45. Backup tools for MySQL
Backup method Storage engine Impact Backup speed Recovery speed Recovery granularity
mysqldump ALL WARM MEDUIM SLOWEST MOST FLEXIBLE
mysqldump INNODB HOT MEDUIM SLOWEST MOST FLEXIBLE
select into outfile ALL WARM SLOW SLOW MOST FLEXIBLE
mk-parallel-backup ALL WARM MEDUIM MEDUIM FLEXIBLE
ibbackup INNODB HOT FAST FAST FLEXIBLE
ibbackup ALL WARM FAST FAST FLEXIBLE
backup command in mysqld ALL HOT FAST FAST FLEXIBLE
filesystem (copy files) ALL COLD FASTEST FASTEST NOT FLEXIBLE
snapshot (using LVM, ZFS, VMWare) ALL ALMOST HOT FAST FAST LEAST FLEXIBLE
mysqlhotcopy MyISAM MOSTLY COLD FAST FAST FLEXIBLE
46. mysqldump
## backup all databases
mysqldump --user=root --password --all-databases >backup_<date>_all.sql
## backup a specific database
mysqldump --user=root --password<database_name>>backup_<date>_<database_name>.sql
## backup multiple databases
mysqldump --user=root --password<database_name>,<database_name>>backup_<date>.sql
## backup a table froma database
mysqldump --user=root --password<database_name><table_name>>backup_<date>_<database_name>_<table_name>.sql
## backup some specific data
mysqldump --user=root --password<database_name><table_name>--where"last_name='VALLE'order by first_name >
backup_<date>.sql
## dumping fromone database toanother
mysqldump --databases <database_name>| mysql -h <destination_host><database_name>
restoreamysqldump
## all databases
mysql--user=root--password <backup.sql
## specific database
mysql--user=<user>--password <database_name>< backup_<dataabse_name>.sql
47. select into
outfile / load
data infile
## dump of the accounts table
select * into outfile '/tmp/accounts.txt' from accounts;
## load the dump
load data infile '/tmp/accounts.txt' into table accounts;
You can use the into outfile clause of the select statementto backup individual
tables, the command used to load the dump created is load data infile
48. The mysqlhotcopyis a perl script written to provide a consistent backup of MyISAM and ARCHIVE
tables, it does some limitations one of which when run it uses the lock tables command to create
read locks on the tables being backed up, this allows for a consistent backup.
mysqlhotcopy
## backup a database
mysqlhotcopy<database_name> /backups
## backup multiple databases
mysqlhotcopy<database_name> accounts /backups
## backup a database to to another server
mysqlhotcopy--method=scp <database_name> username@backup.server:/backup
## use pattern match to backup databasesand tables
mysqlhotcopy<database_name>./^employees//backup
49. Recoveringfrom Crashes
➢ Most often you have to recover to a point-in-time after the last
backup, the normal procedure is as follows
➢ restore the latest backup
➢ recovery the data to a point-in-time using recovery log files
➢ MySQL server uses a binary format for the log files to save
space, this means that you cannot view these files directly, a
utility called mysqlbinlog is supplied to convert these log files
into a text format that you can view. So the process for
performing a point-in-time restore for MySQL is
➢ restore the database using the last backup
➢ determine the first binary log and starting position needed
➢ determine the last binary log needed
➢ convert the binary log to text format with the mysqlbinlog utility
using options to specify the start and stop time
➢ check the text file to make sure it's what you need
➢ import the converted binary log(s)
convert the
log files
## convertto a specific binary log file
mysqlbinlog mysql-bin.010310>mysql-
bin.010310.sql
## usea date to end at a specific time
mysqlbinlog --stop-datetime='201204-29 17:00:00'
mysql-bin.010312 >mysql-bin.010312.sql
## other options are
--stop-datetime
--start-datatime
--start-position
--stop-position
restore the
converted
file
mysql --user=root -password < mysql-bin.010310.sql
50. Point-in-Time (Incremental) Recovery Using the Binary Log
1. Point-in-Time Recovery Using Event Times
2. Point-in-Time Recovery Using Event Positions
51. Point-in-Time Recovery Using Event Times
To indicate the startand end times for recovery, specify the--start-datetimeand --stop-datetimeoptions for mysqlbinlog, in DATETIMEformat. As an
example, supposethat exactly at 10:00 a.m. on April 20, 2005 an SQL statement was executed that deleted a large table. To restorethe table and data,
you could restorethe previous night's backup, and then execute the following command:
shell> mysqlbinlog--stop-datetime="2005-04-209:59:59"
/var/log/mysql/bin.123456 | mysql-u root-p
This command recovers allof the data up until the date and time given by the--stop-datetimeoption. If you did not detect the erroneous SQL statement
that was entered until hours later, you will probably also wantto recover the activity that occurred afterward. Based on this, you could run mysqlbinlog
again with a startdate and time, like so:
shell> mysqlbinlog--start-datetime="2005-04-2010:01:00"
/var/log/mysql/bin.123456 | mysql-u root-p
In this command, the SQL statements logged from10:01 a.m. on will be re-executed. The combination of restoring of the previousnight's dump file and the
two mysqlbinlog commands restores everything up until one second before10:00 a.m. and everything from 10:01 a.m. on.
To usethis method of point-in-time recovery, you should examinethe log to be sureof the exact times to specify for the commands. To display the log file
contents without executing them, usethis command:
shell> mysqlbinlog/var/log/mysql/bin.123456>/tmp/mysql_restore.sql
Then open the /tmp/mysql_restore.sqlfilewith a text editor to examine it.
Excluding specific changes by specifying times for mysqlbinlog does not work well if multiple statements executed at the sametime as the one to be
excluded.
52. Point-in-Time Recovery Using Event Positions
Instead of specifying datesand times, the --start-position and --stop-positionoptionsfor mysqlbinlogcan be used for
specifying log positions. They work the same as the start and stop date options, except that you specify log position
numbers rather than dates. Using positionsmay enable you to be more precise about which part of the log to recover,
especiallyif many transactionsoccurred around the same time as a damaging SQL statement. To determine the
positionnumbers, run mysqlbinlogfor a range of times near the time when the unwantedtransactionwas executed,
but redirect the results to a text file for examination.Thiscan be done like so:
shell> mysqlbinlog--start-datetime="2005-04-20 9:55:00" --stop-datetime="2005-04-20 10:05:00"
/var/log/mysql/bin.123456 > /tmp/mysql_restore.sql
This command creates a small text file in the /tmp directory that containsthe SQL statements aroundthe time that the
deleteriousSQL statement was executed. Open this file with a text editor and look for the statement that you do not
want to repeat. Determine the positionsin the binary log for stoppingand resuming the recovery and make note of
them. Positionsare labeled as log_pos followed by a number. After restoring the previousbackup file, use the position
numbers to process the binary log file. For example, you would use commands something like these:
shell> mysqlbinlog--stop-position=368312 /var/log/mysql/bin.123456 | mysql -u root -p
shell> mysqlbinlog--start-position=368315 /var/log/mysql/bin.123456 | mysql -u root -p
The first command recovers all the transactionsup until the stop position given. The second command recoversall
transactionsfrom the starting position given until the end of the binary log. Because the output of mysqlbinlog
includes SET TIMESTAMPstatementsbefore each SQL statement recorded, the recovered data and related MySQL logs
will reflect the originaltimes at which the transactionswere executed.
53. MySQL- Backup/Restoresingletableusingmysqldump
Verification before restore
Check number of records
SELECT COUNT(*) FROM zabgate.host_inventory;
-- 5874 lines
Backup table steps using mysqldump
1) Create temporary backup directory (e.g /var/backups/zabgate/mysqldump) and give permission to mysql (ignore if path exist)
drwxr-xr-x 2 mysql mysql 47 Mar 27 04:39 mysqldump
2) Backup table using mysqldump
Command :
Single table : mysqldump -uitdba -p db_name table_name > path/table_name.sql
Multiple table: mysqldump -u root -p dbname table1 table2 table3 > table.sql
Execution :
mysqldump -uitdba -p zabgate host_inventory > /var/backups/zabgate/mysqldump/host_inventory_190327.sql
Restore table steps
1) Create temp schema/ db
Mysql > create database temp_backup;
2) Move the sql file from mysqldump directory to restore schema and make sure it's executable under mysql
mv /var/backups/zabgate/mysqldump/host_inventory_190327.sql /seamnt/prd101/mysql/temp_backup/
3) Restore table
Command :
mysql -u <user_name> -p db_name
mysql> source <full_path>/table_name.sql
Execution:
mysql > use temp_backup;
mysql > source host_inventory_190327.sql;
Verification after restore
Check number of records
SELECT COUNT(*) FROM temp_backup.host_inventory;
-- 5874 lines