Mysql commands: Difference between revisions

From KlavoWiki
Jump to navigationJump to search
No edit summary
Line 3: Line 3:
= From your login shell =
= From your login shell =


Creating a Database<br>
== Securing MySQL ==
<pre>
mysql_secure_installation
</pre>
 
== Creating a Database ==
<pre>
<pre>
mysqladmin create mydatabase
mysqladmin create mydatabase
</pre>
</pre>


Dropping (Removing) a Database<br>
== Dropping (Removing) a Database ==
<pre>
<pre>
mysqladmin drop mydatabase
mysqladmin drop mydatabase
</pre>
</pre>


Populating an Existing Database from a *.sql File <br>
== Import Database ==
Populating an Existing Database from a *.sql File
<pre>
<pre>
mysql databasename < filename.sql
mysql databasename < filename.sql
</pre>
</pre>


Dumping Database Structure and Data to a *.sql file<br>
== Export Database ==
Dumping Database Structure and Data to a *.sql file
<pre>
<pre>
mysqldump databasename > filename.sql
mysqldump databasename > filename.sql
mysqldump databasename table1 > table1.sql
mysqldump databasename table1 > table1.sql
</pre>
== User Management ==
=== List Users ===
<pre>
mysql -B -N -pMyPassword -e "SELECT user, host FROM user" mysql
</pre>
=== List User Permissions ===
<pre>
mysql -B -N -pMyPassword -e "SHOW GRANTS FOR 'MyUserName'"
</pre>
</pre>


= From within the MySQL interface =
= From within the MySQL interface =


Starting MySQL from the Command Line<br>
== MySQL CLI ==
Starting MySQL from the Command Line
<pre>
<pre>
mysql
mysql -uusername -pMyPassword
</pre>
</pre>


Line 41: Line 60:
</pre>
</pre>


Seeing What Databases are Available<br>
== Show Databases ==
Seeing What Databases are Available
<pre>
<pre>
show databases;
show databases;
Line 47: Line 67:
(be sure to use the semi-colon to terminate the command)
(be sure to use the semi-colon to terminate the command)
<br>
<br>
== Select Database ==
Telling MySQL to Use a Specific Database<br>
Telling MySQL to Use a Specific Database<br>
<pre>use mydatabase;</pre>
<pre>use mydatabase;</pre>


== Show Tables ==
Seeing What Tables are Available Within a Database<br>
Seeing What Tables are Available Within a Database<br>
<pre>
<pre>
Line 55: Line 79:
</pre>
</pre>


== List Data from a Table ==
Looking at the Data in a Particular Table<br>
Looking at the Data in a Particular Table<br>
<pre>
<pre>
Line 60: Line 85:
</pre>
</pre>


== Rename Table ==
Rename an existing Table
Rename an existing Table
<pre>
<pre>
Line 65: Line 91:
</pre>
</pre>


Adding a Database User with Password<br>
== Delete Table ==
<pre>
use databasename;
drop tablename;
</pre>
 
== User Management ==
 
=== Add User ===
Adding a Database User with Password
<pre>
grant all privileges on [databasename].* to [dbusername]@localhost identified by '[dbpassword]';
grant all privileges on [databasename].* to [dbusername]@localhost identified by '[dbpassword]';
<pre>grant all privileges on mydatabase.* to joeuser@localhost identified by 'supersecretpasswd';
grant all privileges on mydatabase.* to joeuser@localhost identified by 'supersecretpasswd';
flush privileges;
flush privileges;
</pre>
</pre>


=== Delete User ===
Removing a Database User<br>
Removing a Database User<br>
<pre>
<pre>
Line 77: Line 114:
</pre>
</pre>


== Data Manipulation ==
=== Add Data ===
Add data to a table<br>
Add data to a table<br>
<pre>
<pre>
Line 84: Line 124:
</pre>
</pre>


Delete data
 
=== Delete Data ===
<pre>
<pre>
delete from cid where name like '0%';
delete from cid where name like '0%';
</pre>
</pre>


=== Search & Replace ===
Performing Search-and-Replace Actions on a Table<br>
Performing Search-and-Replace Actions on a Table<br>
<pre>
update DatabaseName set TableName = REPLACE(TableName,"[String to Search For]","[String Replacement]");
update DatabaseName set TableName = REPLACE(TableName,"[String to Search For]","[String Replacement]");
<pre>update roesublist set Grades = REPLACE(Grades,"Grades: Any","K,1,2,3,4,5,6,7,8,9,10,11,12");</pre>
update roesublist set Grades = REPLACE(Grades,"Grades: Any","K,1,2,3,4,5,6,7,8,9,10,11,12");
</pre>


[[Category : Linux]]
[[Category : Linux]]

Revision as of 23:21, 18 August 2014

Handy Cheat-Sheet of MySQL Commands

From your login shell

Securing MySQL

mysql_secure_installation

Creating a Database

mysqladmin create mydatabase

Dropping (Removing) a Database

mysqladmin drop mydatabase

Import Database

Populating an Existing Database from a *.sql File

mysql databasename < filename.sql

Export Database

Dumping Database Structure and Data to a *.sql file

mysqldump databasename > filename.sql
mysqldump databasename table1 > table1.sql

User Management

List Users

mysql -B -N -pMyPassword -e "SELECT user, host FROM user" mysql

List User Permissions

mysql -B -N -pMyPassword -e "SHOW GRANTS FOR 'MyUserName'"

From within the MySQL interface

MySQL CLI

Starting MySQL from the Command Line

mysql -uusername -pMyPassword

You will be welcomed with the following message:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is ## to server version: #.##.##

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

The prompt changes to "mysql>", which will be shown in each example below.

Show Databases

Seeing What Databases are Available

show databases;

(be sure to use the semi-colon to terminate the command)

Select Database

Telling MySQL to Use a Specific Database

use mydatabase;


Show Tables

Seeing What Tables are Available Within a Database

show tables;

List Data from a Table

Looking at the Data in a Particular Table

select * from [tablename];

Rename Table

Rename an existing Table

RENAME TABLE tbl_name TO new_tbl_name

Delete Table

use databasename;
drop tablename;

User Management

Add User

Adding a Database User with Password

grant all privileges on [databasename].* to [dbusername]@localhost identified by '[dbpassword]';
grant all privileges on mydatabase.* to joeuser@localhost identified by 'supersecretpasswd';
flush privileges;

Delete User

Removing a Database User

delete from mysql.user where user='techgeek' and host='localhost';
flush privileges;

Data Manipulation

Add Data

Add data to a table

insert into cid  (name, number) values 
('My Name', '0731234321'),
('Mr Name', '0412344321');


Delete Data

delete from cid where name like '0%';

Search & Replace

Performing Search-and-Replace Actions on a Table

update DatabaseName set TableName = REPLACE(TableName,"[String to Search For]","[String Replacement]");
update roesublist set Grades = REPLACE(Grades,"Grades: Any","K,1,2,3,4,5,6,7,8,9,10,11,12");