databases:mysql_cheat_sheet
Table of Contents
MySQL Cheat Sheet
Clear Screen
On Linux
CTRL+L
General Database
Connect to MySQL
mysql -uUSERNAME -p -h hostname -P 3306
Assuming that you are connecting to the localhost and that MySQL is running on the default port of 3306, you can use a shorter command
mysql -uUSERNAME -p
Allow Perl to Connect To MySQL
Run this command to allow Perl to connect to Database server while still having SELinux enabled.
semanage boolean -m --on httpd_can_network_connect_db
Allow Remote Connection
In this example we allow the user 'testuser' on remote PC workstation1.example.com to access everything on the database testdb. We also set the password to be 123456;
GRANT ALL PRIVILEGES ON testdb.* to 'testuser'@'db.example.com' IDENTIFIED BY '123456';
Get Database Size
SELECT table_schema "Data Base Name", sum( data_length + index_length ) / 1024 / 1024 "Data Base Size in MB" FROM information_schema.TABLES GROUP BY table_schema ;
Users
List Users
SELECT user FROM mysql.user;
SELECT user,host FROM mysql.user;
Create User
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'PASSWORD';
Delete User
DROP USER 'testuser'@'localhost';
Passwords
Change Password
SET PASSWORD FOR 'testuser'@'%' = PASSWORD('cleartextpassword');
SET PASSWORD FOR 'testuser'@'localhost' = PASSWORD('cleartextpassword');
Permissions
Show Permissions/Grants
SHOW GRANTS;
SHOW GRANTS FOR 'root'@'localhost';
Grant Permissions
GRANT ALL ON testdb.* TO 'testuser'@'localhost'; flush privileges;
GRANT SELECT, INSERT, UPDATE, DELETE ON testdb.* TO 'testuser'@'localhost'; flush privileges;
Revoke Permissions
REVOKE UPDATE, DELETE ON testdb.* FROM 'testuser'@'localhost'; flush privileges;
Databases and Tables
List Databases
SHOW DATABASES;
Create Database
CREATE DATABASE testdb;
Delete Database
DROP DATABASE testdb;
List Tables
SHOW TABLES FROM testdb
List Columns
SHOW COLUMNS FROM testdb;
Adding Data
Update Data
UPDATE databasename.tablename SET columnname="some_value" WHERE columnname="value";
Insert Data
INSERT INTO databasename.tablename (columnname1, columnname2) VALUES ("value1", 132);
Use COUNT
select a.col1, a.col2, (select count(*) from table2 b where b.col1=a.col1) from table1 as a group by col1;
databases/mysql_cheat_sheet.txt · Last modified: by 127.0.0.1
