Showing posts with label flush. Show all posts
Showing posts with label flush. Show all posts

Wednesday, January 29, 2014

MySQL Basics

Overview

This Post is to give you the basic commands in mysql so you can do basic administration.
This is based on mysql installed on a linux host

How to start/Stop/Restart MySQL

service mysql start

service mysql stop
service mysql restart 

How to check MySQL instance status

mysql -uroot -pyour_password -e STATUS
 
--------------
mysql  Ver 14.14 Distrib 5.5.27, for Linux (x86_64) using  EditLine wrapper

Connection id:          70
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.5.27-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 14 hours 47 min 44 sec

Threads: 1  Questions: 546080  Slow queries: 0  Opens: 19103  Flush tables: 27  Open tables: 2079  Queries per second avg: 10.252
--------------
 

How to see the MySQL processes in linux

ps -ef | grep -i mysql
 
root     16111     1  0 08:55 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/prodmysql02.pid
mysql    16540 16111  1 08:55 ?        00:12:45 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/prodmysql02.err --pid-file=/var/lib/mysql/prodmysql02.pid --socket=/var/lib/mysql/mysql.sock 

How to connect to the instance mysql

mysql -uusername -pyour_password

How to create a database

The simpliest way is

create database my_database;


or to avoid error message if the database already exist

create database if not exists my_database;


How to drop a database

Just a warning, when dropping a database, the system won't ask you to confirm if you want to drop it.

drop database my_database;


How to connect or change database

use rcinet
or
use rcinet;

How to list the databases in MySQL

show databases;
 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
10 rows in set (0.01 sec)

How to check the size of the databases

This is a select that was given to me by one of my friend when I was looking for this information. I kept it and scripts it.


SELECT table_schema "db_name", 
sum(data_length+index_length) /1024/1024 "db_size in Mb"
FROM information_schema.tables
GROUP BY 1; 
 
+--------------------+---------------+
| db_name            | db_size in Mb |
+--------------------+---------------+
| information_schema |    0.00878906 |
| mysql              |    0.62183380 |
| performance_schema |    0.00000000 |
+--------------------+---------------+
9 rows in set (10.94 sec) 

How to create a table in MySQL

CREATE TABLE my_table (
mycol1 INT(5) NOT NULL DEFAULT 0,
mycol2 CHAR(100) DEFAULT NULL,
PRIMARY KEY (mycol1) 
);

How to show the tables in a database

show tables; 
 
+-------------------------+
| Tables_in_xmas_card2012 |
+-------------------------+
| my_table                |
+-------------------------+
1 row in set (0.00 sec) 

How to create a user in MySQL

create user account1 identified by 'your_password';
grant all privileges on *.* to account1;  

How to list the users in mysql

select user,host from mysql.user; 
 
+-------------+--------------------------------+
| user        | host                           |
+-------------+--------------------------------+
| root        | %                              |
| root        | 127.0.0.1                      |
| dbaadmin    | localhost                      |
| root        | localhost                      |
+-------------+--------------------------------+
15 rows in set (0.02 sec) 

How to show the status of a Master or Slave/Replica in MySQL

show master status\G   This is on a master instance
 
*************************** 1. row ***************************
            File: log-bin.000078
        Position: 64150203
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
 
or
show slave status\G    This is on a Slave/Replica instance 
 
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: xxx.xxx.xxx.xxx
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: log-bin.000056
          Read_Master_Log_Pos: 986876833
               Relay_Log_File: mysql_relay-bin.000130
                Relay_Log_Pos: 64787846
        Relay_Master_Log_File: log-bin.000056
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 986876833
              Relay_Log_Space: 64972302
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 123
1 row in set (0.00 sec) 

How to flush the data to disk and set the database in read only

flush tables with read lock; 

How to remove the read only on a database

unlock tables; 

How to check and repair a database

mysqlcheck -c your_database  -uroot -pyour_password --auto-repair

Character set in MySQL

Character sets are mainly used to tell MySQL what to store in MySQL dependng of the language. Character set can be defined at 
  • the server level 
  • the database level
  • the table level
  • the column level

show character set;
 
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
| dec8     | DEC West European           | dec8_swedish_ci     |      1 |
| cp850    | DOS West European           | cp850_general_ci    |      1 |
| hp8      | HP West European            | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 |
| swe7     | 7bit Swedish                | swe7_swedish_ci     |      1 |
| ascii    | US ASCII                    | ascii_general_ci    |      1 |
| ujis     | EUC-JP Japanese             | ujis_japanese_ci    |      3 |
| sjis     | Shift-JIS Japanese          | sjis_japanese_ci    |      2 |
| hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   |      1 |
| tis620   | TIS620 Thai                 | tis620_thai_ci      |      1 |
| euckr    | EUC-KR Korean               | euckr_korean_ci     |      2 |
| koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    |      1 |
| gb2312   | GB2312 Simplified Chinese   | gb2312_chinese_ci   |      2 |
| greek    | ISO 8859-7 Greek            | greek_general_ci    |      1 |
| cp1250   | Windows Central European    | cp1250_general_ci   |      1 |
| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |
| ucs2     | UCS-2 Unicode               | ucs2_general_ci     |      2 |
| cp866    | DOS Russian                 | cp866_general_ci    |      1 |
| keybcs2  | DOS Kamenicky Czech-Slovak  | keybcs2_general_ci  |      1 |
| macce    | Mac Central European        | macce_general_ci    |      1 |
| macroman | Mac West European           | macroman_general_ci |      1 |
| cp852    | DOS Central European        | cp852_general_ci    |      1 |
| latin7   | ISO 8859-13 Baltic          | latin7_general_ci   |      1 |
| utf8mb4  | UTF-8 Unicode               | utf8mb4_general_ci  |      4 |
| cp1251   | Windows Cyrillic            | cp1251_general_ci   |      1 |
| utf16    | UTF-16 Unicode              | utf16_general_ci    |      4 |
| cp1256   | Windows Arabic              | cp1256_general_ci   |      1 |
| cp1257   | Windows Baltic              | cp1257_general_ci   |      1 |
| utf32    | UTF-32 Unicode              | utf32_general_ci    |      4 |
| binary   | Binary pseudo charset       | binary              |      1 |
| geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  |      1 |
| cp932    | SJIS for Windows Japanese   | cp932_japanese_ci   |      2 |
| eucjpms  | UJIS for Windows Japanese   | eucjpms_japanese_ci |      3 |
+----------+-----------------------------+---------------------+--------+
39 rows in set (0.03 sec)
 

Collation in MySQL

Collation in MySQL is used for how the data is sort. Each character has a position in the ordering process.

show character set;
 
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
| dec8     | DEC West European           | dec8_swedish_ci     |      1 |
| cp850    | DOS West European           | cp850_general_ci    |      1 |
| hp8      | HP West European            | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 |
| swe7     | 7bit Swedish                | swe7_swedish_ci     |      1 |
| ascii    | US ASCII                    | ascii_general_ci    |      1 |
| ujis     | EUC-JP Japanese             | ujis_japanese_ci    |      3 |
| sjis     | Shift-JIS Japanese          | sjis_japanese_ci    |      2 |
| hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   |      1 |
| tis620   | TIS620 Thai                 | tis620_thai_ci      |      1 |
| euckr    | EUC-KR Korean               | euckr_korean_ci     |      2 |
| koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    |      1 |
| gb2312   | GB2312 Simplified Chinese   | gb2312_chinese_ci   |      2 |
| greek    | ISO 8859-7 Greek            | greek_general_ci    |      1 |
| cp1250   | Windows Central European    | cp1250_general_ci   |      1 |
| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |
| ucs2     | UCS-2 Unicode               | ucs2_general_ci     |      2 |
| cp866    | DOS Russian                 | cp866_general_ci    |      1 |
| keybcs2  | DOS Kamenicky Czech-Slovak  | keybcs2_general_ci  |      1 |
| macce    | Mac Central European        | macce_general_ci    |      1 |
| macroman | Mac West European           | macroman_general_ci |      1 |
| cp852    | DOS Central European        | cp852_general_ci    |      1 |
| latin7   | ISO 8859-13 Baltic          | latin7_general_ci   |      1 |
| utf8mb4  | UTF-8 Unicode               | utf8mb4_general_ci  |      4 |
| cp1251   | Windows Cyrillic            | cp1251_general_ci   |      1 |
| utf16    | UTF-16 Unicode              | utf16_general_ci    |      4 |
| cp1256   | Windows Arabic              | cp1256_general_ci   |      1 |
| cp1257   | Windows Baltic              | cp1257_general_ci   |      1 |
| utf32    | UTF-32 Unicode              | utf32_general_ci    |      4 |
| binary   | Binary pseudo charset       | binary              |      1 |
| geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  |      1 |
| cp932    | SJIS for Windows Japanese   | cp932_japanese_ci   |      2 |
| eucjpms  | UJIS for Windows Japanese   | eucjpms_japanese_ci |      3 |
+----------+-----------------------------+---------------------+--------+
39 rows in set (0.01 sec)