
Terminal - Commands tagged mysql - 50 results
ssh username@remotehost 'mysqldump -u <dbusername> -p<dbpassword> <dbname> tbl_name_1 tbl_name_2 tbl_name_3' | mysql -u <localusername> -p<localdbpassword> <localdbname> < /dev/stdin
This is sample output - yours may be different.
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 <backup_name>.tar.gz | mysql -u root <database_name>
This is sample output - yours may be different.
`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 :)
This is sample output - yours may be different.
tshark -i any -T fields -R mysql.query -e mysql.query
This is sample output - yours may be different.
mysqlcheck -op -u<user> <db>
This is sample output - yours may be different.
mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.host Table is already up to date
-o : optimize
-p : asks for password
-u : user to use for authentication
(pv -n ~/database.sql | mysql -u root -pPASSWORD -D database_name) 2>&1 | zenity --width 550 --progress --auto-close --auto-kill --title "Importing into MySQL" --text "Importing into the database"
This is sample output - yours may be different.
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
http://www.daniweb.com/forums/post1253285.html#post1253285
mysql -e"SHOW STATUS LIKE '%uptime%'"|awk '/ptime/{ calc = $NF / 3600;print $(NF-1), calc"Hour" }'
This is sample output - yours may be different.
zcat database.sql.gz | mysql -uroot -p'passwd' database
This is sample output - yours may be different.
This way you keep the file compressed saving disk space.
Other way less optimal using named pipes:
mysql -uroot -p'passwd' database <
mysql>use DBNAME; mysql>source FILENAME
This is sample output - yours may be different.
mysqldump -u UNAME -p DBNAME TABLENAME> FILENAME
This is sample output - yours may be different.
for I in $(mysql -e 'show databases' -u root --password=root -s --skip-column-names); do mysqldump -u root --password=root $I | gzip -c | ssh user@server.com "cat > /remote/$I.sql.gz"; done
This is sample output - yours may be different.
It grabs all the database names granted for the $MYSQLUSER and gzip them to a remote host via SSH.
mysqldump -uUSERNAME -pPASSWORD database | gzip > /path/to/db/files/db-backup-`date +%Y-%m-%d`.sql.gz ;find /path/to/db/files/* -mtime +5 -exec rm {} \;
This is sample output - yours may be different.
ssh user@host "mysqldump -h localhost -u mysqluser -pP@$$W3rD databasename | gzip -cf" | gunzip -c > database.sql
This is sample output - yours may be different.
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.
sqlite3 mydb.sqlite3 '.dump' | grep -vE '^(BEGIN|COMMIT|CREATE|DELETE)|"sqlite_sequence"' | sed -r 's/"([^"]+)"/`\1`/' | tee mydb.sql | mysql -p mydb
This is sample output - yours may be different.
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".
grep CONFIG $(which mysqlbug)
This is sample output - yours may be different.
CONFIGURE_LINE="./configure '--build=i486-linux-gnu' '--host=i486-linux-gnu' '--prefix=/usr' '--exec-prefix=/usr' '--libexecdir=/usr/sbin' '--datadir=/usr/share' '--localstatedir=/var/lib/mysql' '--includedir=/usr/include' '--infodir=/usr/share/info' '--mandir=/usr/share/man' '--with-server-suffix=-24+lenny2' '--with-comment=(Debian)' '--with-system-type=debian-linux-gnu' '--enable-shared' '--enable-static' '--enable-thread-safe-client' '--enable-assembler' '--enable-local-infile' '--with-big-tables' '--with-unix-socket-path=/var/run/mysqld/mysqld.sock' '--with-mysqld-user=mysql' '--with-libwrap' '--without-openssl' '--with-yassl' '--without-docs' '--with-bench' '--without-readline' '--with-extra-charsets=all' '--with-innodb' '--with-archive-storage-engine' '--with-csv-storage-engine' '--with-federated-storage-engine' '--with-blackhole-storage-engine' '--with-sphinx-storage-engine' '--without-embedded-server' '--with-ndbcluster' '--with-ndb-ccflags=-fPIC' '--with-ndb-shm' '--without-ndb-sci' '--without-ndb-test' '--with-embedded-server' '--with-embedded-privilege-control' '--without-ndb-docs' 'CC=gcc' 'CFLAGS=-DBIG_JOINS=1 -O2 -fPIC' 'CPPFLAGS=' 'CXXFLAGS=-DBIG_JOINS=1 -felide-constructors -fno-rtti -O2' 'CXX=g++' 'FFLAGS=-g -O2' 'LDFLAGS=' 'build_alias=i486-linux-gnu' 'host_alias=i486-linux-gnu' 'CFLAGS= -DDBUG_OFF -DBIG_JOINS=1 -O2 -fPIC ' 'CXXFLAGS= -DDBUG_OFF -DBIG_JOINS=1 -felide-constructors -fno-rtti -O2 -fno-implicit-templates -fno-exceptions -fno-rtti'"
`test -n "$CONFIGURE_LINE" && echo "Configure command: $CONFIGURE_LINE"`
Output is from Debian Lenny
cat `whereis mysqlbug | awk '{print $2}'` | grep 'CONFIGURE_LINE='
This is sample output - yours may be different.
CONFIGURE_LINE="./configure '--build=i486-linux-gnu' '--host=i486-linux-gnu' '--prefix=/usr' '--exec-prefix=/usr' '--libexecdir=/usr/sbin' '--datadir=/usr/share' '--localstatedir=/var/lib/mysql' '--includedir=/usr/include' '--infodir=/usr/share/info' '--mandir=/usr/share/man' '--with-server-suffix=-d7-ourdelta44' '--with-comment=(OurDelta - http://ourdelta.org/)' '--with-system-type=debian-linux-gnu' '--enable-shared' '--enable-static' '--enable-thread-safe-client' '--enable-assembler' '--enable-local-infile' '--with-big-tables' '--with-unix-socket-path=/var/run/mysqld/mysqld.sock' '--with-mysqld-user=mysql' '--with-libwrap' '--without-openssl' '--with-yassl' '--without-docs' '--without-readline' '--with-extra-charsets=all' '--with-innodb' '--with-archive-storage-engine' '--with-csv-storage-engine' '--with-federated-storage-engine' '--with-blackhole-storage-engine' '--with-sphinx-storage-engine' '--without-embedded-server' '--with-ndbcluster' '--with-ndb-ccflags=-fPIC' '--with-ndb-shm' '--without-ndb-sci' '--without-ndb-test' '--with-embedded-server' '--with-embedded-privilege-control' '--without-ndb-docs' 'build_alias=i486-linux-gnu' 'host_alias=i486-linux-gnu' 'CC=gcc' 'CFLAGS=-DBIG_JOINS=1 -O2 -fPIC' 'CXX=g++' 'CXXFLAGS=-DBIG_JOINS=1 -felide-constructors -fno-rtti -O2'"
This is sample output - yours may be different.
$ perror 131
OS error code 131: State not recoverable
$perror 132
MySQL error code 132: Old database file
perror should be installed if mysql-server package is installed
slave start; SELECT MASTER_POS_WAIT('master.000088','8145654'); slave stop;
This is sample output - yours may be different.
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.
mysql DATABASE -N -s -r -e 'SQL COMMAND'
This is sample output - yours may be different.
$ mysql DATABASE -e 'SQL COMMAND'
+----+-------+-------------+
| id | tf_id | interpro_id |
+----+-------+-------------+
| 1 | 6802 | IPR015495 |
| 2 | 6802 | IPR001005 |
| 3 | 6802 | IPR001005 |
+----+-------+-------------+
$ mysql DATABASE -N -s -r -e 'SQL COMMAND'
1 6802 IPR015495
2 6802 IPR001005
3 6802 IPR001005
-N removes header
-s removes separator chars
-r raw output
After using these options, the MySQL ouptut can be used with pipes very easily
echo -e "[client]\nuser = YOURUSERNAME\npassword = YOURPASSWORD" > ~/.my.cnf
This is sample output - yours may be different.
[client]
user = YOURUSERNAME
password = YOURPASSWORD
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.
cat schema.sql data.sql test_data.sql | mysql -u user --password=pass dbname
This is sample output - yours may be different.
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.
mysql --database=dbname -B -N -e "SHOW TABLES" | awk '{print "ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;"}' | mysql --database=dbname &
This is sample output - yours may be different.
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.
watch -n 1 uptime\;myqladmin --user=<user> --password=<password> --verbose processlist
This is sample output - yours may be different.
Useful for monitoring both MySQL and the server load at the same time.
ssh user@host 'mysqldump dbname | gzip' > /path/to/backups/db-backup-`date +%Y-%m-%d`.sql.gz
This is sample output - yours may be different.
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 -n 1 mysqladmin --user=<user> --password=<password> processlist
This is sample output - yours may be different.