Kcko
3/23/2018 - 3:32 PM

mysql command

# To connect to a database:
mysql database_name
 
# To connect to a database, user will be prompted for a password:
mysql -u user --password database_name
 
# To connect to a database on another host:
mysql -h database_host database_name
 
# To connect to a database through a Unix socket:
mysql --socket path/to/socket.sock
 
# To execute SQL statements in a script file (batch file):
mysql database_name < script.sql
 
# ---
 
# To connect to a database
$ mysql -h localhost -u root -p
 
# To backup all databases
$ mysqldump --all-databases --all-routines -u root -p > ~/fulldump.sql
 
# To restore all databases
$ mysql -u root -p  < ~/fulldump.sql
 
# Create database with utf-8 collation and utf-8 charset
$ mysql> create database <database_name> character set utf8 collate utf8_general_ci;
 
# Create a table with an auto-incrementing primary key (id), w/ utf-8
$ mysql> create table <table_name> (`id` int unsigned auto_increment, primary key (`id`)) default character set utf8 collate utf8_general_ci;
 
# List all databases on the sql server.
$ mysql> show databases;

# To dump a database to a file (Note that your password will appear in your command history!):
mysqldump -uusername -ppassword the-database > db.sql
 
# To dump a database to a file:
mysqldump -uusername -p the-database > db.sql
 
# To dump a database to a .tgz file (Note that your password will appear in your command history!):
mysqldump -uusername -ppassword the-database | gzip -9 > db.sql
 
# To dump a database to a .tgz file:
mysqldump -uusername -p the-database | gzip -9 > db.sql
 
 # To dump all databases to a file (Note that your password will appear in your command history!):
mysqldump -uusername -ppassword --all-databases > all-databases.sql
 
# To dump all databases to a file:
mysqldump -uusername -p --all-databases > all-databases.sql
 
# To export the database structure only:
mysqldump --no-data -uusername -p the-database > dump_file
 
# To export the database data only:
mysqldump --no-create-info -uusername -p the-database > dump_file