Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Last active September 30, 2019 10:01
Show Gist options
  • Save joseluisq/8274fc2fe24e5ba717cbb5547e7e99f8 to your computer and use it in GitHub Desktop.
Save joseluisq/8274fc2fe24e5ba717cbb5547e7e99f8 to your computer and use it in GitHub Desktop.
Some useful commands and scripts for MySQL.

MySQL helpful commands and scripts

Some useful commands and scripts for MySQL.

Update: All these commands and more at https://github.com/joseluisq/awesome-mysql-queries-commands

Import script file from console

mysql -h host -u username -p password --default_character_set utf8 database_name < mysql_script.sql

Or on MySQL shell type:

mysql> USE DATABASE mydatabase_name;
mysql> SOURCE mysql_script.sql

Export script file from console

SQL file

mysqldump -h localhost -u username -p database_name > ./mysql_script.sql

Export database to SQL + GZIP file

mysqldump -h localhost -u username -p database_name | gzip > ./mysql_script.sql

or using --single-transaction if you got an mysqldump error (because you lack privileges to lock the tables)

mysqldump -h localhost -u username -p database_name --single-transaction | gzip > ./mysql_script.sql

Export tables to SQL file

mysqldump -h localhost -u username -p database_name table_name1 table_name2 > mydb_tables.sql

Finding duplicated values

SELECT code, COUNT(code) duplicates FROM client GROUP BY code HAVING duplicates > 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment