MySQL Snippets

CREATE

CREATE TABLE `user` (`id` bigint(20) unsigned NOT NULL auto_increment,
                     `username` varchar(100) default NULL)

GRANT

GRANT CREATE,DROP,INDEX,ALTER ON `database` TO `username`@'127.0.0.1

RENAME

RENAME TABLE `user` to `users`
ALTER TABLE `user` CHANGE `id` `uid`

INDEX

ALTER TABLE `user` ADD INDEX ( `classId` )
ALTER TABLE `user` ADD UNIQUE (`username`)
SHOW INDEX FROM module

Drop an index, Add an index:

ALTER TABLE my_table DROP INDEX index1, ADD INDEX index2 (my_field1, my_field2)

OPTIMIZE

ALTER TABLE `user` ENGINE=INNODB
OPTIMIZE TABLE `user`

REPAIR

REPAIR TABLE `user` QUICK
REPAIR TABLE `user` EXTENDED
REPAIR TABLE `user` USE_FRM

SLAVE

START SLAVE
SHOW SLAVE STATUS\G
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1

DUMP

mysqldump –defaults-file=/etc/mysql/debian.cnf mysql
mysqldump –defaults-file=/etc/mysql/debian.cnf mysql -t user
-c complete insert indicates all fields in values list
--skip-extended-insert one insert per row
--no-data dump only schema

.my.cnf

  • to connect to different hosts without entering your password:
[client]
host = host1.domain.com
user = username
password = xxxxxx

host = host2.domain.com
user = username
password = xxxxxx

Command line

  • execute a query from the command line
$ mysql database_name -e "select * from table_name"