You are on page 1of 39

Administracin

MySQL-Admin
Comunidad mySQL ORACLE

Repaso
Motores almacenamiento? Un motor de almacenamiento es un componente de mySQL responsible del rendimiento I/O de los datos al mismo tiempo que nos ofrece ciertas caractersticas propias para cada tipo de aplicacin. A partir de la versin 5.0 se incorporan motores de almacenamiento independientes. MyISAM InnoDB FALCON Memory

Agenda

Cliente mysql Schemata Tablespace Database Tables Index System Functions Query Data Handling

SCHEMATA

SCHEMATA
Un SCHEMA es una BASEdeDatos (DATABASE) La tabla del diccionario SCHEMATA proporciona informacin sobre las base de datos

Tablespaces

Tablespaces (5.1)
CREATE TABLESPACE tablespace_name ADD DATAFILE 'file_name' USE LOGFILE GROUP logfile_group [EXTENT_SIZE [=] extent_size] [INITIAL_SIZE [=] initial_size] [AUTOEXTEND_SIZE [=] autoextend_size] [MAX_SIZE [=] max_size] [NODEGROUP [=] nodegroup_id] [WAIT] [COMMENT [=] comment_text] ENGINE [=] engine_name
7

Tablespaces (5.1)
CREATE LOGFILE GROUP logfile_group ADD UNDOFILE 'undo_file' [INITIAL_SIZE [=] initial_size] [UNDO_BUFFER_SIZE [=] undo_buffer_size] [REDO_BUFFER_SIZE [=] redo_buffer_size] [NODEGROUP [=] nodegroup_id] [WAIT] [COMMENT [=] comment_text] ENGINE [=] engine_name
8

Tablespaces (5.1)
ALTER TABLESPACE tablespace_name {ADD|DROP} DATAFILE 'file_name' [INITIAL_SIZE [=] size] [WAIT] ENGINE [=] engine_name

Tablespaces - InnoDB
my.cnf innodb_data_home_dir = /var/lib/mysql innodb_data_file_path = ibdata1:10M:autoextend innodb_file_per_table
InnoDB almacena cada tabla en un fichero .ibd en el mismo directorio donde el fichero .frm es creado

10

Tablespaces ej. InnoDB


InnoDB: The first specified datafile /home/heikki/data/ibdata1 did not exist: InnoDB: a new database to be created! InnoDB: Setting file /home/heikki/data/ibdata1 size to 134217728 InnoDB: Database physically writes the file full: wait... InnoDB: datafile /home/heikki/data/ibdata2 did not exist: new to be created InnoDB: Setting file /home/heikki/data/ibdata2 size to 262144000 InnoDB: Database physically writes the file full: wait... InnoDB: Log file /home/heikki/data/logs/ib_logfile0 did not exist: new to be created InnoDB: Setting log file /home/heikki/data/logs/ib_logfile0 size to 5242880 InnoDB: Log file /home/heikki/data/logs/ib_logfile1 did not exist: new to be created InnoDB: Setting log file /home/heikki/data/logs/ib_logfile1 size to 5242880 InnoDB: Doublewrite buffer not found: creating new InnoDB: Doublewrite buffer created InnoDB: Creating foreign key constraint system tables InnoDB: Foreign key constraint system tables created InnoDB: Started mysqld: ready for connections
11

Database INFORMATION_SCHEMA
INFORMATION_SCHEMA es el diccionario, proporciona informacin del metadata. SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES; SELECT * FROM INFORMATION_SCHEMA.FILES; SELECT TABLESPACE_NAME, FILE_NAME FROM INFORMATION_SCHEMA.FILES; SELECT LOGFILE_GROUP_NAME, LOGFILE_GROUP_NUMBER, EXTRA FROM INFORMATION_SCHEMA.FILES;
12

Cliente mysql

13

MySQL command line client


called "mysql"
> Takes SQL commands > sends them to the server > gets results

but it can do much more ...

14

Connect

$ mysql -u username -ppassword $ mysql -e "select version()"


5.1.45-log

15

Status
+--------------------------------------------+---------------------+ | Variable_name | Value | +--------------------------------------------+---------------------+ | Aborted_clients |0 | | Aborted_connects |0 | | Bytes_received | 155372598 | | Bytes_sent | 1176560426 | ... | Connections | 30023 | | Created_tmp_disk_tables |0 | | Created_tmp_files |3 | | Created_tmp_tables |2 | ... | Threads_created | 217 | | Threads_running | 88 | | Uptime | 1389872 | +------------------------------------------+-----------------------+

mysql> show global|session status

16

Client options
$ mysql -B -e "select version()" $ mysql -B -N -e "select version()" $ mysql -v -e "select * from city" $ mysql -vv -e "select * from city"

17

Systems Functions
$ mysql -u username -ppassword $ mysql -B -N -e "select version()" Mysql> select version(), current_date; Mysql> select version(); select now(); Mysql> select user();
18

Systems vars
$ mysql -e "show variables.... ...like 'port' datadir server_id $ mysql -e "show status... ...like com_select connections
19

Databases

20

Databases
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name mysql> CREATE DATABASE menagerie; mysql> USE menagerie shell> mysql -h host -u user -p menagerie DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
21

Tablas

22

Tablas

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_options] [partition_options] CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)] [table_options] [partition_options] select_statement create_definition: col_name column_definition | [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...) [index_option] ... | {INDEX|KEY} [index_name] [index_type] (index_col_name,...) [index_option] ... | [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...) [index_option] ... | {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...) [index_option] ... | [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition | CHECK (expr)
23

Tablas column_definition:

data_type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY] [COMMENT 'string'] [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}] [reference_definition]

data_type: BIT[(length)] | TINYINT[(length)] [UNSIGNED] [ZEROFILL] | SMALLINT[(length)] [UNSIGNED] [ZEROFILL] | MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL] | INT[(length)] [UNSIGNED] [ZEROFILL] | INTEGER[(length)] [UNSIGNED] [ZEROFILL] | BIGINT[(length)] [UNSIGNED] [ZEROFILL] | REAL[(length,decimals)] [UNSIGNED] [ZEROFILL] | DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL] | FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL] | DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL] | NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL] | DATE | TIME | TIMESTAMP | DATETIME | YEAR
24

Tablas
| CHAR[(length)]

[CHARACTER SET charset_name] [COLLATE collation_name] | VARCHAR(length) [CHARACTER SET charset_name] [COLLATE collation_name] | BINARY[(length)] | VARBINARY(length) | TINYBLOB | BLOB | MEDIUMBLOB | LONGBLOB | TINYTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | TEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | MEDIUMTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | LONGTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | ENUM(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name] | SET(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name] | spatial_type
25

Tablas
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [WHERE where_condition] [ORDER BY ...] [LIMIT row_count] Multiples tablas a la vez : DELETE [LOW_PRIORITY] [QUICK] [IGNORE] tbl_name[.*] [, tbl_name[.*]] ... FROM table_references [WHERE where_condition]
26

Tablas
TRUNCATE [TABLE] tbl_name Vacia la tabla, sin darnos ms informacin DROP TABLE t_old; Elimina la tabla

27

Mantenimiento de tablas
12.4.2. Table Maintenance Statements [+/-] 12.4.2.1. ANALYZE TABLE Syntax 12.4.2.2. CHECK TABLE Syntax 12.4.2.3. CHECKSUM TABLE Syntax 12.4.2.4. OPTIMIZE TABLE Syntax 12.4.2.5. REPAIR TABLE Syntax
28

Analyze
Actualiza la informacin con la distribucin de claves en la tabla. Esta informacin la utiliza el optimizador Durante el analisis, la tabla es bloqueada en lectura MyISAM e InnoDB ANALYZE [NO_WRITE_TO_BINLOG | LOCAL] TABLE tbl_name [, tbl_name] ...

29

ndices

30

Indices

CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [index_type] ON tbl_name (index_col_name,...) [index_option] ... index_col_name: col_name [(length)] [ASC | DESC] index_type: USING {BTREE | HASH} index_option: KEY_BLOCK_SIZE [=] value | index_type | WITH PARSER parser_name | COMMENT 'string'

31

Indices
CREATE INDEX indx_name ON customer(name); DROP INDEX indx_name ON customer;

32

Query/Select

33

Query

$ mysql -e "select count(*) from city $ mysql -NB -e "select count(*) from city" \ | awk '{print $2}'

34

Manejo de datos

35

Data Handling

Pager

$ mysql> pager less

Pginas de tamao menor

mysql> SELECT * FROM world.City; # [ filas en una ventana ] mysql> nopager

Paginacin estandar # Vuelta al estandar


36

Data Handling
# Como 'less' pero puedes editar y salvar

mysql> pager vim -

mysql> pager md5sum

Paginacin en modo 'md5sum' mysql> select * from City; 5d17ffa50d6da200dee621823ade2543 4079 rows in set (0.01 sec)

37

Data Handling
mysql> pager cat > /tmp/t1.txt
mysql> select "one" union select "two"; mysql> select "one" union select "TWO";

mysql> pager cat > /tmp/t2.txt mysql> nopager mysql> \! vimdiff -o /tmp/t[12].txt

# Diferencias ente dos conjuntos de datos


38

Log de la sesin
mysql> tee mylog.txt
# Graba toda la sesin

mysql> notee

# Vuelta a la normalidad (dejar de grabar)

39