mysql presentation
DESCRIPTION
A basic Mysql instruction to kick start other DBA to learn from itTRANSCRIPT
1Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
An Introduction to MySQLAn Introduction to MySQL forfor
Oracle & MS DBAOracle & MS DBA
2Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
ObjectivesObjectives
• Introduction MySQL the Company
• Architecture of MYSQL
• The Basic
• Configuration of MySQL
• Security
• User Management
• Backup and Restore
• Troubleshooting Step
3Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
• Founded in Sweden in 1995; Privately Owned • Employs over 300 people worldwide• Over 10,000,000 installations• 50,000 web downloads/day• Part of a rapidly growing open source LAMP stack
The World’s Most Popular The World’s Most Popular Open Source DatabaseOpen Source Database
4Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
MySQL Database Server – the 10 million!MySQL Database Server – the 10 million!
Free
Community Edition Available under GPL Software tested by
Community + basic MySQL AB testing
Release early & often cycle (3 to 4 weeks)
Bleeding edge No maintenance SLA Not supported No ISV certification
5Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
MySQL NetworkMySQL Network
Free
Community Edition Available under GPL Software tested by
Community + basic MySQL AB testing
Release early & often cycle (3 to 4 weeks)
Bleeding edge No maintenance SLA Not supported No ISV certification
Great for open source developers &
technology enthusisats
Supported
MySQL Network Software fully tested, certified and
optimized by MySQL AB Enterprise release cycles
(6 to 9 months) Annual Subscription Update Advisor Technical Alert Advisor Knowledge Base Production Support Indemnification
Great for enterprise customers and supported by
MySQL
1. Platform for Innovation
2. Change is revolutionary
3. Informal Communication
1. Platform for Stability
2. Change is evolutionary
3. Formal Support
6Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
Yahoo!Yahoo!
ApplicationMySQL powers over 200 Yahoo! properties around the world including Yahoo! Finance with 260 million rows.
Key Business BenefitLow down time for high-volume, business-critical applications. Using MySQL has reduced cost by more than $2 million.
Why MySQL? Mission-Critical Reliability “We have used MySQL far more than
anyone expected. We went from experimental to mission-critical in a couple of months.”
Jeremy Zawodny MySQL Database Expert
Yahoo! Finance
7Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
MYSQL ArchitectureMYSQL Architecture
indows™ : indows™ : my.inimy.ini C:\Program Files\MySQL\MySQL Server x.y\my.ini C:\Program Files\MySQL\MySQL Server x.y\my.ini Linux : Linux : my.cnfmy.cnf /etc/my.cnf or /etc/mysql/my.cnf /etc/my.cnf or /etc/mysql/my.cnf /whereYouInstalledMySQL/data/my.cnf (server-specific options) /whereYouInstalledMySQL/data/my.cnf (server-specific options) (@localstatedir@ for this installation) (@localstatedir@ for this installation)
~/.my.cnf (user-specific options)~/.my.cnf (user-specific options)
8Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
Let’s start workingLet’s start working
9Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
Connecting to and disconnecting from Server Connecting to and disconnecting from Server shell > mysql –h localhost –u user –p shell > mysql –h localhost –u user –p
Welcome to the MySQL monitor. Commands end with ; or \g.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Your MySQL connection id is 1
Server version: 5.0.67-community-nt MySQL Community Edition (GPL)Server version: 5.0.67-community-nt MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> exitmysql> exit
The BasicThe Basic
10Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
•Creating and Using DatabaseCreating and Using Databasemysql> create database tutorial;mysql> create database tutorial;Query OK, 1 row affected (0.00 sec)Query OK, 1 row affected (0.00 sec)mysql> use tutorial;mysql> use tutorial;Database changedDatabase changed
The BasicThe Basic
•Create a tableCreate a tableShell>mysql -h localhost -u root -p tutorialShell>mysql -h localhost -u root -p tutorialEnter password: *******Enter password: *******Welcome to the MySQL monitor. Commands end with ; or \g.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 3Your MySQL connection id is 3Server version: 5.0.67-community-nt MySQL Community Edition (GPL)Server version: 5.0.67-community-nt MySQL Community Edition (GPL)Type 'help;' or '\h' for help. Type '\c' to clear the buffer.Type 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> create table cheese ( Name varchar(15) not null, weight int not null,mysql> create table cheese ( Name varchar(15) not null, weight int not null, -> primary key(name)); -> primary key(name));Query OK, 0 rows affected (0.13 sec)Query OK, 0 rows affected (0.13 sec)
11Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
Configuration files Configuration files Windows™ : Windows™ : my.inimy.ini C:\Program Files\MySQL\MySQL Server x.y\my.ini C:\Program Files\MySQL\MySQL Server x.y\my.ini Linux : Linux : my.cnfmy.cnf /etc/my.cnf or /etc/mysql/my.cnf ( Global options)/etc/my.cnf or /etc/mysql/my.cnf ( Global options)
/whereYouInstalledMySQL/data/my.cnf (server-specific options) /whereYouInstalledMySQL/data/my.cnf (server-specific options) (@localstatedir@ for this installation)(@localstatedir@ for this installation) ~/.my.cnf (user-specific options) ~/.my.cnf (user-specific options)
Repository of tables & stuctures Repository of tables & stuctures Linux : /var/lib/mysql or /whereYouInstalledMySQL/data/ Linux : /var/lib/mysql or /whereYouInstalledMySQL/data/ Windows™ :Windows™ : C:\Program Files\MySQL\MySQL Server x.y\data C:\Program Files\MySQL\MySQL Server x.y\data
System File & TableSystem File & Table
12Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
In WindowIn WindowStart MySql server: Start MySql server: c:\mysql\bin\mysqld-nt --standalone - You can leave the DOS window open or c:\mysql\bin\mysqld-nt --standalone - You can leave the DOS window open or close it. close it. Stop MySql server: Stop MySql server: c:\mysql\bin\mysqladmin shutdown - This will shutdown the server.c:\mysql\bin\mysqladmin shutdown - This will shutdown the server.
If Mysql is install as services If Mysql is install as services c:\> net start mysql - starts mysql database server. c:\> net start mysql - starts mysql database server. c:\> net stop mysql - stops mysql database server. c:\> net stop mysql - stops mysql database server.
In LinuxIn LinuxTo start MySQL serverTo start MySQL server /etc/init.d/mysqld start /etc/init.d/mysqld startTo stop MySQL serverTo stop MySQL server/etc/init.d/mysqld stop/etc/init.d/mysqld stopTo check the statusTo check the status/etc/init.d/mysqld status/etc/init.d/mysqld statusOrOr/bin/safe_mysqld &/bin/safe_mysqld &
Command -Start & Stop ServerCommand -Start & Stop Server
13Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
Manually: Manually: mysql -u root -p -P 3306mysql -u root -p -P 3306 create myDatabasecreate myDatabaseBy a SQL script: By a SQL script:
SQL commands, or: SQL commands, or: sourcesource fichier.sqlfichier.sql
mysql -u root -p < C:\fichier.sqlmysql -u root -p < C:\fichier.sql
Running external scriptRunning external script
14Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
Security setting• Set the root password
– The default installation of MySQL leave the root password blank. So the first step to do when we login is
Shell>mysql –uroot mysql
mysql> update user set password = PASSWORD('abc123') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
15Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
Security setting• List anonymous users: • SELECT Host, User FROM mysql.user WHERE
User='';
• Delete anonymous users: • DELETE FROM mysql.user WHERE Host='localhost'
AND User=''; • FLUSH PRIVILEGES; -- { Update and reflect change
on existing session} • Careful: by default, root has no password !
• To set a password for root:
– mysqladmin -u root password unPassword
16Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
Security setting• MySQL uses Access Control Lists (ACLs) for all
connections, queries, and other operations that a user may attempt to perform. The ACLs are composed of tables which are used to determine privilege.
MySQL access control involves two stages:
Stage 1 Connection Verification: The server checks whether you are even allowed to connect.
Stage 2 Request Verification: Assuming you can connect, the server checks each request you issue to see whether you have sufficient privileges to perform it.
17Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
Security setting• The server uses the user, db, and host tables in the mysql
database at both stages of access control.
• For the second stage of access control, the server may, if the request involves tables, additionally consult the tables_priv and columns_priv tables.
18Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
User Management• The server uses the user, db, and host tables in the mysql
database at both stages of access control.• For the second stage of access control, the server may, if the
request involves tables, additionally consult the tables_priv and columns_priv tables.
19Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
Security setting• Adding Users. MySQL users and their privileges are normally
created with GRANT statements. You may however edit the user table manually with INSERT statements. Below are examples of each.
Using GRANT statements.shell> mysql --user=root mysql mysql> GRANT ALL PRIVILEGES ON *.* TO cheese@localhost -> IDENTIFIED BY 'some_pass' WITH GRANT OPTION; mysql> GRANT RELOAD,PROCESS ON *.* TO admin@localhost; mysql> GRANT USAGE ON *.* TO dummy@localhost; Using INSERT statements.shell> mysql --user=root mysql mysql> INSERT INTO user VALUES('localhost','cheese',PASSWORD('some_pass'), -> 'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y'); mysql> INSERT INTO user SET Host='localhost',User='admin', -> Reload_priv='Y', Process_priv='Y'; mysql> INSERT INTO user (Host,User,Password) -> VALUES('localhost','dummy',''); mysql> FLUSH PRIVILEGES;
20Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
A simple example of creating a backup of one database is the following: A simple example of creating a backup of one database is the following:
mysqldump --opt database > backup-file.sql mysqldump --opt database > backup-file.sql You can then restore it like so: You can then restore it like so: mysql database < backup-file.sql mysql database < backup-file.sql
It is possible to dump several databases with one command: It is possible to dump several databases with one command: mysqldump --databases database1 [database2 ...] > my_databases.sql mysqldump --databases database1 [database2 ...] > my_databases.sql
If all the databases are wanted, one can use: If all the databases are wanted, one can use: mysqldump --all-databases > all_databases.sql mysqldump --all-databases > all_databases.sql
Mysqldump-backupMysqldump-backup
21Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
Use the "mysql" Database Use the "mysql" Database use mysql; use mysql; Show a list of Tables in the Database Show a list of Tables in the Database show tables;show tables;++---------------------------+---------------------------+| Tables_in_mysql || Tables_in_mysql |+---------------------------++---------------------------+| columns_priv || columns_priv || db || db || func || func || help_category || help_category || help_keyword || help_keyword || help_relation || help_relation || help_topic || help_topic || host || host || proc || proc || procs_priv || procs_priv || tables_priv || tables_priv || time_zone || time_zone || time_zone_leap_second || time_zone_leap_second || time_zone_name || time_zone_name || time_zone_transition || time_zone_transition || time_zone_transition_type || time_zone_transition_type || user || user |+---------------------------++---------------------------+
Basic MySQL syntax (i)Basic MySQL syntax (i)
22Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
•SHOW DATABASES;SHOW DATABASES;mysql> show databases;mysql> show databases;+--------------------++--------------------+| Database || Database |+--------------------++--------------------+| information_schema || information_schema || mysql || mysql |+--------------------++--------------------+USE database_name;USE database_name;
List data from "user" table List data from "user" table select Host, User, Password from user; select Host, User, Password from user; select * from user;select * from user;select * from user\G; { in row order} select * from user\G; { in row order}
Basic MySQL syntaxBasic MySQL syntax
23Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
•mysql -u root -p -P 3306 mysql -u root -p -P 3306 mysql> status mysql> status mysql> show status ; (<=> mysqladmin -u root -p extended-status) mysql> show status ; (<=> mysqladmin -u root -p extended-status)
mysql> show warnings ; mysql> show warnings ; mysql> show variables ; mysql> show variables ; mysql> show processlist ; mysql> show processlist ; mysql> show full processlist ; mysql> show full processlist ; mysql> show databases ; mysql> show databases ; mysql> connect mysql; ("system" tables of MySQL)mysql> connect mysql; ("system" tables of MySQL) mysql> show table status\G mysql> show table status\G mysql> show table status like "a_table"\G mysql> show table status like "a_table"\G mysql> connect myDatabase ; ( or: mysql> use myDatabase) mysql> connect myDatabase ; ( or: mysql> use myDatabase) mysql> show tables ; mysql> show tables ; mysql> describe a_table ; mysql> describe a_table ; EXPLAIN - EXPLAIN - useful for optimize & debug SQL requests useful for optimize & debug SQL requests example: mysql> explain SELECT ... example: mysql> explain SELECT ... InnoDBInnoDB SHOW INNODB STATUS \G SHOW INNODB STATUS \G show variables like '%inno%' ; show variables like '%inno%' ;
Summary useful commandSummary useful command
24Copyright 2006 MySQL AB The World’s Most Popular Open Source Database
•Linux : check if mysqld is configurated to start in current init level: Linux : check if mysqld is configurated to start in current init level: [root]# chkconfig mysql [root]# chkconfig mysql [root]# echo $?[root]# echo $? Check if mysqld is startedCheck if mysqld is started telnet localhost 3306telnet localhost 3306 mysqladmin -u root -p pingmysqladmin -u root -p pingmysqladmin -u root -p statusmysqladmin -u root -p statusmysqladmin -u root -p extended-statusmysqladmin -u root -p extended-statusmysqladmin -u root -p processlistmysqladmin -u root -p processlist
Connection testConnection test