For Remote MySQL Server.
# [mysql dir]/bin/mysql -h hostname -u root -p
e.g. [mysql dir]/bin/mysql -h 192.168.0.100 -u root -p
For Local MySQL Server.
# [mysql dir]/bin/mysql -u root -p
How to create a database on the MySql server.
mysql> create database [databasename];
e.g. mysql> create database theitideas;
How to List all databases on the MySQL server.mysql> show databases;
How to Switch to a database to execute query.mysql> use [dbname];
e.g. mysql> use theitidieas;
To see all the tables in the database.mysql> show tables;
To check database's field formats.mysql> describe [table name];
e.g.mysql> describe table1;
To delete a database.mysql> drop database [database name];
e.g. mysql> drop database theitideas;
To delete a table.mysql> drop table [table name];
e.g. mysql> drop table table1;
Show all data in a table.mysql> SELECT * FROM [table name];
e.g. mysql> SELECT * FROM table2;
Returns the columns and column information pertaining to the designated table.mysql> show columns from [table name];
e.g. mysql> show columns from table2;
Show certain selected rows with the value "yes".mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";
e.g. mysql> SELECT * FROM table2 WHERE vehicle = "yes";
Show all records containing the name "vansh" AND the phone number '123456'.mysql> SELECT * FROM [table name] WHERE name = "vansh" AND phone_number = '123456';
e.g. mysql> SELECT * FROM table2 WHERE name = "vansh" AND phone_number = '123456';
Show all records not containing the name "vansh" AND the phone number '123456' order by the phone_number field.mysql> SELECT * FROM [table name] WHERE name != "vansh" AND phone_number = '123456' order by phone_number;
e.g. mysql> SELECT * FROM table2 WHERE name != "vansh" AND phone_number = '123456' order by phone_number;
Show all records starting with the letters 'vansh' AND the phone number '123456'.mysql> SELECT * FROM [table name] WHERE name like "vansh%" AND phone_number = '123456';
e.g. mysql> SELECT * FROM table2 WHERE name like "vansh%" AND phone_number = '123456';
Show all records starting with the letters 'vansh' AND the phone number '123456' limit to records 1 through 5.mysql> SELECT * FROM [table name] WHERE name like "vansh%" AND phone_number = '123456' limit 1,5;
e.g. mysql> SELECT * FROM table2 WHERE name like "vansh%" AND phone_number = '123456' limit 1,5;
Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";
e.g. SELECT * FROM table2 WHERE rec RLIKE "^a";
Show unique records.mysql> SELECT DISTINCT [column name] FROM [table name];
e.g. mysql> SELECT DISTINCT address FROM table2;
Show selected records sorted in an ascending (asc) or descending (desc).mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;
e.g. mysql> SELECT 14,15 FROM table2 ORDER BY 15 DESC;
Return number of rows.mysql> SELECT COUNT(*) FROM [table name];
e.g. mysql> SELECT COUNT(*) FROM table2;
Sum column.
mysql> SELECT SUM(*) FROM [table name];
e.g. mysql> SELECT SUM(*) FROM table2;
Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
e.g. mysql> INSERT INTO user (192.168.1.101,user1,password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;
Change a users password from unix shell.# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'
e.g. [mysql dir]/bin/mysqladmin -u root -h 192.168.1.1 -p Password 'Newpassw0rd'
Change a users password from MySQL prompt. Login as root. Set the password. Update privs.# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
e.g. mysql> SET PASSWORD FOR 'user'@'192.168.1.100' = PASSWORD('newpassword');
mysql> flush privileges;
Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start
Set a root password if there is NO root password.# mysqladmin -u root password newpassword
Update a root password.# mysqladmin -u root -p oldpassword newpassword
Allow the user "vansh" to connect to the server from localhost using the password "Passw0rd". Login as root. Switch to the MySQL db. Give privs. Update privs.# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to vansh@localhost identified by 'passw0rd';
mysql> flush privileges;
Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;
ormysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;
To update info already in a table.mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';
Delete a row(s) from a table.mysql> DELETE from [table name] where [field name] = 'whatever';
e.g. mysql> DELETE from table2 where address = 'yes';
Update database permissions/privilages.mysql> flush privileges;
Delete a column.mysql> alter table table2 drop column colum1;
Add a new column to db.mysql> alter table table2 add column column5 varchar (20);
Change column name.mysql> alter table [table name] change [old column name] [new column name] varchar (50);
e.g. mysql> alter table table2 change column5 column2 varchar (50);
Dump all databases for backup. Backup file is sql commands to recreate all db's.# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
e.g. # [mysql dir]/bin/mysqldump -u root -pPassw0rd --opt >/opt/alldatabases.sql
Dump one database for backup.# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql
e.g. # [mysql dir]/bin/mysqldump -u username -pPassw0rd --databases vansh >/opt/databasename.sql
Dump a table from a database.# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql
e.g. # [mysql dir]/bin/mysqldump -c -u username -pPassw0rd vansh table2 > /opt/vansh.table2.sql
Restore database (or database table) from backup.# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
e.g. # [mysql dir]/bin/mysql -u username -pPassw0rd vansh1 < /opt/backupfile.sql
Create Table in Database
mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));
OR
mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');