MySQL

MySQL Commands

To login from unix shell

mysql -h localhost -u root -p

Create a database on the sql server. List all databases on the sql server. Switch to a database. To see all the tables in the db. To see database's field formats. To delete a db. To delete a table.

mysql> create database [databasename];
mysql> show databases;
mysql> use [db name];
mysql> show tables;
mysql> describe [table name];
mysql> drop database [database name];
mysql> drop table [table name];

Show all data in a table. Show certain selected rows with the value "whatever". Show all records containing the name "May" AND the phone number '34'. Show all records not containing the name "May" AND the phone number '34' order by the phone field. Show all records starting with the letters 'May' AND the phone number '34'. Show all records starting with the letters 'May' AND the phone number '34' limit to records 1 through 5.

mysql> SELECT * FROM [table_name];
mysql> SELECT * FROM [table_name] WHERE [field_name] = "whatever";
mysql> SELECT * FROM [table_name] WHERE name = "May" AND phone = '34';
mysql> SELECT * FROM [table_name] WHERE name != "May" AND phone = '34' order by phone;
mysql> SELECT * FROM [table_name] WHERE name like "May%" AND phone = '34';
mysql> SELECT * FROM [table_name] WHERE name like "May%" AND phone = '34' limit 1,5;

Returns the columns and column information pertaining to the designated table.

mysql> show columns from [table_name];

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";

Show unique records.

mysql> SELECT DISTINCT [column_name] FROM [table_name];

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

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'));
mysql> flush privileges;

Change a users password from unix shell.

mysqladmin -u username -h localhost -p password 'new-password'

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');
mysql> flush privileges;

Set a root password if there is on root password.

mysqladmin -u root password newpassword

Update a root password.

mysqladmin -u root -p oldpassword newpassword

Allow the user "bob" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
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';

Update database permissions/privilages.

mysql> flush privileges;

Delete a column.

mysql> alter table [table name] drop column [column name];

Add a new column to db.

mysql> alter table [table name] add column [new column name] varchar (20);

Change column name.

mysql> alter table [table name] change [old column name] [new column name] varchar (50);

Make a unique column so you get no dupes.

mysql> alter table [table name] add unique ([column name]);

Make a column bigger.

mysql> alter table [table name] modify [column name] VARCHAR(3);