commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
In the example above 3 tables are copied. You can change the number of tables. You should be able to come up with variants of the command by modifying the mysqldump part easily, to copy some part of remote mysql DB.
`tar xfzO` extracts to STDOUT which got redirected directly to mysql. Really helpful, when your hard drive can't fit two copies of non-compressed database :)
-o : optimize
-p : asks for password
-u : user to use for authentication
This uses PV to monitor the progress of the MySQL import and displays it though Zenity. You could also do this
pv ~/database.sql | mysql -u root -pPASSWORD -D database_name
and get a display in the CLI that looks like this
2.19MB 0:00:06 [ 160kB/s] [> ] 5% ETA 0:01:40
My Nautalus script using this command is here
show Mysql uptime
This way you keep the file compressed saving disk space.
Other way less optimal using named pipes:
mysql -uroot -p'passwd' database <
It grabs all the database names granted for the $MYSQLUSER and gzip them to a remote host via SSH.
This command will dump a database on a remote stream to stdout, compress it, stream it to your local machine, decompress it and put it into a file called database.sql.You could even pipe it into mysql on your local machine to restore it immediately. I had to use this recently because the server I needed a backup from didn't have enough disk space.
Filters out all non-insert SQL operations (we couldn't filter out only lines starting with "INSERT" because inserts can span multiple lines), quotes table names with backticks, saves dump to a file and pipes it straight to mysql.
This transfers only data--it expects your schema is already in place. In Ruby on Rails, you can easily recreate the schema in MySQL with "rake db:schema:load RAILS_ENV=production".
Output is from Debian Lenny
perror should be installed if mysql-server package is installed
say you want to reinitialize the slave database without resetting the master positions. You stop the slave, dump the master database with --master-data=2 then execute the command on the slave and wait for it to stop at the exact position of the dump. reinit the slave db and start the slave. enjoy.
-N removes header
-s removes separator chars
-r raw output
After using these options, the MySQL ouptut can be used with pipes very easily
The file .my.cnf located at user's home directory is used for mysql login. If this file exists, then
mysql -uYOURUSERNAME -pYOURPASSWORD database -e 'SOME SQL COMMAND'
can be replaced with
mysql database -e 'SOME SQL COMMAND'
It saves you from typing!
This is valid for mysqladmin and mysqldump commands as well.
Be aware of using the --password argument as it will appear your password in plain text on the screen. You may use -p argument instead, it will prompt you to enter you password in hidden mode.
This loops through all tables and changes their collations to UTF8. You should backup beforehand though in case some data is lost in the process.
Useful for monitoring both MySQL and the server load at the same time.
I have this on a daily cronjob to backup the commandlinefu.com database from NearlyFreeSpeech.net (awesome hosts by the way) to my local drive. Note that (on my Ubuntu system at least) you need to escape the % signs on the crontab.
Watch is a very useful command for periodically running another command - in this using mysqladmin to display the processlist. This is useful for monitoring which queries are causing your server to clog up.