Useful MySQL Command Allow MySQL client to connect Remote Host $ mysql -u root -p Enter password: mysql> use mysql mysql> GRANT ALL ON *.* to root@'192.168.1.4' IDENTIFIED BY 'your-root-password'; mysql> FLUSH PRIVILEGES; MySQL Database Dump mysqldump -u user -p db-name > db-name.out Delete MySQL Data from Table There are two ways to delete all the data in a MySQL database table. TRUNCATE TABLE tablename; This will delete all data in the table very quickly. In MySQL the table is actually dropped and recreated, hence the speed of the query. The number of deleted rows for MyISAM tables returned is zero; for INNODB it returns the actual number deleted. DELETE FROM tablename; This also deletes all the data in the table, but is not as quick as using the “TRUNCATE TABLE” method. In MySQL >= 4.0 the number of rows deleted is returned; in MySQL 3.23 the number returned is always zero. Exporting DB Schema/Structure (no data) only mysqldump -u root -p --no-data dbname > schema.sql Backup Multiple Database mysqldump –u[user name] –p[password] [database name 1] [database name 2] .. > [dump file] Backup ALL Database shell> mysqldump –u[user name] –p[password] –all-databases > [dump file] Backup Specific Table in a Database shell> mysqldump --user [username] --password=[password] [database name] [table name] > /tmp/sugarcrm_accounts_contacts.sql Restoring MySQL Database shell> mysql --u [username] --password=[password] [database name] < [dump file] Export Database in Zipped (.gz) format mysqldump -hlocalhost -uUSERNAME -pPA$$W0RD DATABASE | gzip > /home/USERNAME/backups-mysql/BACKUP.gz Optimize Single Database: ./mysqlcheck -o database_name Optimize All Databases: ./mysqlcheck -o -A ./mysqlcheck -o --all-databases Analyze Single Database: ./mysqlcheck -a database_name Analyze All Databases: ./mysqlcheck -a -A ./mysqlcheck -a --all-databases Repair Single Database: ./mysqlcheck -r database_name Repair All Databases: ./mysqlcheck -r -A ./mysqlcheck -r --all-databases Importing & Exprting a Table from Database Export- mysqldump -p - -user=username dbname tableName > tableName.sq Import- mysql -u username -p -D dbname < tableName.sql MySQL Drop Table mysql> use database 'yourdbname'; mysql> drop table 'yourtablename'; View MySQL users and privileges mysql> select user,host from mysql.user; mysql> show grants for 'root'@'%'; mysql> select * from mysql.user; mysql> desc mysql.user; select host, user, password from mysql.user; How to Create a New User CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; The first thing to do is to provide the user with access to the information they will need. GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost'; Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges. FLUSH PRIVILEGES; How To Grant Different User Permissions Here is a short list of other common possible permissions that users can enjoy. ALL PRIVILEGES- as we saw previously, this would allow a MySQL user all access to a designated database (or if no database is selected, across the system) CREATE- allows them to create new tables or databases DROP- allows them to them to delete tables or databases DELETE- allows them to delete rows from tables INSERT- allows them to insert rows into tables SELECT- allows them to use the Select command to read through databases UPDATE- allow them to update table rows GRANT OPTION- allows them to grant or remove other users' privileges To provide a specific user with a permission, you can use this framework: GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@'localhost’; If you want to give them access to any database or to any table, make sure to put an asterisk (*) in the place of the database name or table name. Each time you update or change a permission be sure to use the Flush Privileges command. If you need to revoke a permission, the structure is almost identical to granting it: REVOKE [type of permission] ON [database name].[table name] FROM ‘[username]’@‘localhost’; Just as you can delete databases with DROP, you can use DROP to delete a user altogether: DROP USER ‘demo’@‘localhost’; Run OPTIMIZE TABLE to defragment tables for better performance mysqlcheck -u username -p --auto-repair --optimize --all-databases Obtain MySQL Database size in MB SELECT table_schema AS "Database", ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)" FROM information_schema.TABLES GROUP BY table_schema;