MySQL commands

From LemonWiki共筆
Jump to navigation Jump to search

Exporting data into MySql sql file

  • Good.gif export the compressed *.sql file (especially for BIG sql file). It will show a progress bar and estimated time to complete the mysqldump[1][2]:
    1. (optional if the pv was not installed)
      • yum install pv for RHEL / CentOS / SL / Fedora Linux[3];
      • apt-get install pv for Debian / Ubuntu[4];
      • Install (1) Cygwin, (2) pv package and (3) MySQL database clients apps package for Win Os windows.png
      • Install (1) brew install pv to get the latest version of pv package[5] and (2) MySQL database clients from XAMPP for Mac icon_os_mac.png . Path of mysql is: /Applications/xampp/xamppfiles/bin/mysql
    2. (for InnoDB tables)mysqldump -h localhost -u username -p --force --single-transaction DATA_BASE_NAME | pv | gzip -c > database.sql.gz [6][7] and press Enter
    3. ask to enter password and press Enter


  • Bulk export multiple tables into the compressed *.sql files
mysqldump -h localhost -u username -pPASSWORD --force --single-transaction DB_NAME TABLE_1 | pv | gzip -c > table_1.sql.gz
mysqldump -h localhost -u username -pPASSWORD --force --single-transaction DB_NAME TABLE_2 | pv | gzip -c > table_2.sql.gz
   

note: the last line of above part is return symbol. The last command will not executed automatically without the return symbol.

  • export the *.sql file. It will show a progress bar and estimated time to complete the mysqldump [8]:
    1. (optional if the pv was not installed) yum install pv for RHEL / CentOS / SL / Fedora Linux
    2. (for InnoDB tables)mysqldump -h localhost -u username -p --force --single-transaction DATA_BASE_NAME | pv > database.sql and press Enter
    3. enter password and press Enter
  • export the *.sql file
    1. mysqldump -h localhost -u username -p DATA-BASE-NAME > /path/to/database.sql

Importing data from MySql sql file

Icon_exclaim.gif Notice: existing database will be overwritten

  • Good.gif Import the compressed *.sql file with gzip (especially for BIG sql file). It will show a progress bar and estimated time to complete the mysql command:
    1. (optional if the pv was not installed) yum install pv for RHEL / CentOS / SL / Fedora Linux
    2. pv database.sql.gz | gunzip | mysql -u username -p -h localhost --default_character_set utf8 --force DATA_BASE_NAME [9] and press Enter
    3. enter password and press Enter The console window will show ETA (Estimated Time of Arrival)
  • Import *.sql file:
    1. (optional if the .sql file was compressed) unzip data.zip or gzip -d data.gz if the file was compressed by gzip
    2. mysql -u username -p -h localhost --default_character_set utf8 --force DATA_BASE_NAME < data.sql [10] and press Enter
    3. enter password and press Enter
  • Import the *.sql file (especially for BIG sql file). It will show a progress bar and estimated time to complete the mysql command[11]:
    1. (optional if the pv was not installed) yum install pv for RHEL / CentOS / SL / Fedora Linux
    2. pv data.sql | mysql -u username -p -h localhost --default_character_set utf8 --force DATA_BASE_NAME [12] and press Enter
    3. enter password and press Enter The console window will show ETA (Estimated Time of Arrival)
  • Import ZIP file to MySql directly:
    1. (optional if the 7Zip was not installed) yum install p7zip
    2. 7za x -so data.zip | mysql -u username -p -h localhost --default_character_set utf8 --force DATA_BASE_NAME [13][14] and press Enter
    3. enter password and press Enter

Show the MySQL process list & kill the process

Show the MySQL process list & kill the process (especial for SLOW query command)[15]

  1. mysql -h HOST -u ACCOUNT -p
  2. And keyin the PASSWORD of your MySQL ACCOUNT. The first character will be changed to mysql> from $ or #
  3. mysql> SHOW FULL PROCESSLIST\G to show the process list
  4. mysql> kill 101; to kill the process with Id number: 101
  5. mysql> exit; to leave the MySQL command.


troubleshooting of MySQL command

command not found: mysqldump

  1. locate the mysqldump command
    • sudo find / -iname mysqldump for Mac icon_os_mac.png or Linux Os linux.png
    • And find /Applications/XAMPP/xamppfiles/bin/mysqldump from XAMPP for Mac icon_os_mac.png
  2. input the complete path of mysqldump command
    • old command which caused error mysqldump -h 127.0.0.1 -u root -p --force --single-transaction DATABASE_NAME | pv | gzip -c > DATABASE_NAME.sql.gz
    • new command /Applications/XAMPP/xamppfiles/bin/mysqldump -h 127.0.0.1 -u root -p --force --single-transaction DATABASE_NAME | pv | gzip -c > DATABASE_NAME.sql.gz

references

further reading