Using MySQL
Some general information on using MySQL. See the Legend for an explanation of the symbols and conventions used on this page.View database settings and status
Show settings:> show variables;
> show status;
> show processlist;
View table attributes and status
Show table attributes:> use <dbname>; > describe <tablename>;
> show table status;
> show table status like 'tablename';
Show table size
The 7th column of "show table status", "Data_length", shows the size of each table in bytes. Here is some SQL code to determine the size taken by a table in a more human-readable format. Assuming that the database name is 'emp_database' and table name is 'emp_master':SELECT table_schema, table_name, ROUND(data_length/1024/1024,2) total_size_mb FROM information_schema.tables WHERE table_name = 'emp_master' AND table_schema = 'emp_database';
--------------------------------------------- table_schema | table_name | total_size_mb --------------------------------------------- emp_database | emp_master | 2.57
Change password
To set root password for the first time:$ mysqladmin -uroot password "newpassword"$ mysqladmin -uroot -p "newpassword"$ mysqladmin -u<username> -p "newpassword"Dump a database/table
$ mysqldump [-h<address>] -u<user name> -p <database name> [table name] > <dump file>
- [database name]: The name of the database that we would like the mysqldump utility to backup. Instead of specifying one single database name, we could use either --databases (separate database names with spaces) or --all-databases to backup every single database on our MySQL server.
- [table name]: You can optionally backup specific tables rather than the whole database (space separated list).
- --add-drop-database — Add a DROP DATABASE statement before each CREATE DATABASE statement.
- --add-drop-table — Add a DROP TABLE statement before each CREATE TABLE statement.
- --ignore-table=db_name.table_name — Exclude the table db_name.table_name from the dump. To ignore multiple tables, use this option multiple times.
- -v — Verbose mode. Useful when making a long dump.
- Backing Up Your MySQL Databases With MySQLDump
- Backuping and restoring a single table using mysqldump
- Extract a Single Table from a mysqldump File
Dump a single table
Resources:Dump schema only
$ mysqldump -uroot -p --no-data dbname > dumpfile.sql
Restore a dump
$ mysql [-h<hostname>] -u<user> -p [db name] < <dumpfile>
$ bunzip2 -k < dumpfile.sql.bz2 | mysql -uroot -p databasename
Working with permissions
MySQL permissions are managed separately from the system user permissions. Different permissions can be granted on a database or table basis, to a username connecting from a specified address. Wildcards ("%") and netmasks may be used in the address to specify a range of sources for connections. If you are assigning permissions to connect to a local database, it'll be easier in the long run to assign the same permissions to both localhost and 127.0.0.1, as MySQL does not automatically assume that they are the same. Remember that it is assumed that you are connecting locally if you don't use a --host or -h parameter. For basic use, it's fine to grant all rights on a database at once. For greater security, you should look into using more fine-grained permissions. Syntax:> grant all on databasename.tablename to 'username'@'address' identified by 'password'; > flush privileges;
> grant all on db.* to user@"%" identified by "password"; > grant all on db.* to user@"%.mydomain.com" identified by "password"; > grant all on db.* to user@"192.168.1.%" identified by "password"; > grant all on db.table to user@"192.168.1.0/24" identified by "password";
- skip-networking should not be set
- bind-address should be either set to your network IP address, or not set at all
Caching
Clear the query cache:> RESET QUERY CACHE;
Miscellaneous
Resources
on 11/11/2009 at 22:36