Databases: Difference between revisions

From Cheatsheet
Jump to navigationJump to search
Line 14: Line 14:
</syntaxhighlight>
</syntaxhighlight>


=== Movement and checks ===
=== Checks and queries ===
==== Common ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# Switch to a different database
USE MyDatabase;
# Show all databases;
# Show all databases;
SHOW DATABASES;
SHOW DATABASES;
# Switch to database MyDatabase
USE MyDatabase;


# Show all tables within the database
# Show all tables within the database
SHOW TABLES;
SHOW TABLES;


# Show information about the contents of a table
# Show information about the contents of table users
DESCRIBE users;
DESCRIBE users;


# Show indexes and additional information for a table
# Show indexes and additional information for table dogs
SHOW INDEXES FROM dogs;
SHOW INDEXES FROM dogs;


Line 35: Line 36:
</syntaxhighlight>
</syntaxhighlight>


==== Users ====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# List created users
# List created users
Line 43: Line 45:
</syntaxhighlight>
</syntaxhighlight>


==== Select ====
<syntaxhighlight lang="bash">
</syntaxhighlight>
=== Configuration ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# Create a database
# Create a database
CREATE DATABASE mydb;
CREATE DATABASE mydb;


# Create a user with a password
# Switch to database mydb and create a table in it
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';
USE mydb;
CREATE TABLE syslog (
    log_id BIGINT AUTO_INCREMENT,
    year YEAR,
    month TINYTEXT,
    day SMALLINT,
    time TIME,
    severity TINYTEXT,
    facility TINYTEXT,
    client TINYTEXT,
    binaryname TINYTEXT,
    messagecode MEDIUMINT UNSIGNED,
    logmessage TEXT,
    primary key(log_id)
);
 
# Delete a table
DROP TABLE syslog;
</syntaxhighlight>


# Give rights to a user on the database
==== Users and permissions ====
<syntaxhighlight lang="bash">
# Create a user called admin, with a password
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'p4ssw0rd';
 
# Give all rights to a user called admin, on database mydb
GRANT ALL PRIVILEGES ON mydb.* TO 'admin'@'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON mydb.* TO 'admin'@'localhost' WITH GRANT OPTION;
# Allow the monitoring user to login from external IP 1.2.3.4, and give him all rights on database alerts
GRANT ALL ON alerts.* to monitoring@'1.2.3.4' IDENTIFIED BY 'P455w0rd';
# Give user zoey read-only rights rights on database customers
GRANT SELECT ON customers.* TO 'zoey'@'%' IDENTIFIED BY 'P4ssw0rd';
</syntaxhighlight>
</syntaxhighlight>



Revision as of 14:55, 10 September 2024


MYSQL / MARIADB

Login

# Activate the MySQL CLI (as user with proper permission)
mysql;

# Authenticate into the MySQL CLI
mysql -u MyUser -p;

# Authenticate into a specific database
mysql -u MyUser -p MyDatabase;

Checks and queries

Common

# Show all databases;
SHOW DATABASES;

# Switch to database MyDatabase
USE MyDatabase;

# Show all tables within the database
SHOW TABLES;

# Show information about the contents of table users
DESCRIBE users;

# Show indexes and additional information for table dogs
SHOW INDEXES FROM dogs;

# Exit the MySQL CLI
exit

Users

# List created users
SELECT user FROM mysql.user;

# See privileges for the user johan
SHOW GRANTS FOR johan;

Select


Configuration

# Create a database
CREATE DATABASE mydb;

# Switch to database mydb and create a table in it
USE mydb;
CREATE TABLE syslog (
    log_id BIGINT AUTO_INCREMENT,
    year YEAR,
    month TINYTEXT,
    day SMALLINT,
    time TIME,
    severity TINYTEXT,
    facility TINYTEXT,
    client TINYTEXT,
    binaryname TINYTEXT,
    messagecode MEDIUMINT UNSIGNED,
    logmessage TEXT,
    primary key(log_id)
);

# Delete a table
DROP TABLE syslog;

Users and permissions

# Create a user called admin, with a password
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'p4ssw0rd';

# Give all rights to a user called admin, on database mydb
GRANT ALL PRIVILEGES ON mydb.* TO 'admin'@'localhost' WITH GRANT OPTION;

# Allow the monitoring user to login from external IP 1.2.3.4, and give him all rights on database alerts
GRANT ALL ON alerts.* to monitoring@'1.2.3.4' IDENTIFIED BY 'P455w0rd';

# Give user zoey read-only rights rights on database customers
GRANT SELECT ON customers.* TO 'zoey'@'%' IDENTIFIED BY 'P4ssw0rd';

Postgres

# Log into the Postgres CLI
psql

# List all databases
\l

# Connect to a database
-d mydatabase -U databaseuser –W

# Switch to the new database connection
\c mydatabase databaseuser

# List all tables
\dt

# Quit the Postgres CLI
\q