This commit is contained in:
2024-04-03 22:04:13 +02:00
parent 7e68609006
commit 0b373d31db
142 changed files with 7334 additions and 0 deletions

70
databases/mariadb.md Normal file
View File

@ -0,0 +1,70 @@
# MariaDB Cheat-Sheet
MariaDB Server is one of the most popular open source relational databases. Its made by the original developers of MySQL and guaranteed to stay open source. It is part of most cloud offerings and the default in most Linux distributions.
It is built upon the values of performance, stability, and openness, and MariaDB Foundation ensures contributions will be accepted on technical merit. Recent new functionality includes advanced clustering with Galera Cluster 4, compatibility features with Oracle Database and Temporal Data Tables, allowing one to query the data as it stood at any point in the past.
Project Homepage: [MariaDB](https://mariadb.org/)
Documentation: [MariaDB Docs](https://mariadb.org/documentation/)
---
## Installation
### Install MariaDB on Debian/Ubuntu/Mint/Zorin/forks
```bash
sudo apt update
sudo apt install -y mariadb-server mycli --install-recommends
sudo mysql_secure_installation
```
### Install MariaDB on RHEL/Fedora/CentOS/Alma/Rocky
```bash
sudo dnf update
sudo dnf install -y mariadb-server mycli
sudo mysql_secure_installation
```
### Install MariaDB on Arch/Manjaro/Arco/forks
```bash
sudo pacman -Syyu
sudo pacman -S mariadb-server mycli --noconfirm
sudo mysql_secure_installation
```
### Deploy MariaDB in Docker
- [https://hub.docker.com/_/mariadb](https://hub.docker.com/_/mariadb)
- [https://docs.linuxserver.io/images/docker-mariadb/](https://docs.linuxserver.io/images/docker-mariadb/)
### Deploy MariaDB in Kubernetes
- [https://mariadb.org/start-mariadb-in-k8s/](https://mariadb.org/start-mariadb-in-k8s/)
- [https://kubedb.com/kubernetes/databases/run-and-manage-mariadb-on-kubernetes/](https://kubedb.com/kubernetes/databases/run-and-manage-mariadb-on-kubernetes/)
## Access Database from outside
Open `/etc/mysql/mariadb.conf.d/50-server.cnf` and change the line containing `bind-address` to `bind-address = 0.0.0.0`.
---
## Create Administrative User
Access the MySQL command line by entering `mysql -u root -p` in the shell followed by the Database `root` password.
Create a new user `newuser` for the host `localhost` with a new `password`:
```sql
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
```
Grant all permissions to the new user
```sql
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
```
Update permissions
```sql
FLUSH PRIVILEGES
```

88
databases/mysql.md Normal file
View File

@ -0,0 +1,88 @@
# MySQL Community Edition (CE) Cheat-Sheet
MySQL Community Edition is the freely downloadable version of the world's most popular open source database.
Project Homepage: [MySQL Community Edition](https://www.mysql.com/products/community/)
Documentation: [MySQL Docs](https://dev.mysql.com/doc/)
>_Editor's note:_ The MariaDB Project was forked from MySQL when [Sun Microsystems](https://en.wikipedia.org/wiki/Sun_Microsystems)' intellectual property [was acquired](https://en.wikipedia.org/wiki/Acquisition_of_Sun_Microsystems_by_Oracle_Corporation) by [Oracle](https://en.wikipedia.org/wiki/Oracle_Corporation). MariaDB still shares enormous inter-compatibility with MySQL functions and software interoperability, and performance at most scales is arguably indiscernible from MySQL CE. In this writers' opinion it is **not beneficial** in most cases to favor Oracle's monetized option over the GPL's MariaDB alternative.
---
## Installation
### Install MySQL on Debian/Ubuntu/Mint/Zorin/Deb forks
>[!warning]
> Common Debian repositories don't populate MySQL CE and require a vendor-provided repository available [here](https://dev.mysql.com/downloads/repo/apt/). The repo file in this example is current as of 2024-01-20.
```bash
sudo apt update
sudo apt install -y lsb-release gnupg
wget https://dev.mysql.com/get/mysql-apt-config_0.8.29-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.29-1_all.deb
sudo mkdir /var/lib/mysql
sudo apt update
sudo apt install -y mysql-community-server mysql-common mycli --install-recommends
sudo mysql_secure_installation
```
### Install MySQL on RHEL/Fedora/CentOS/Alma/Rocky
```bash
sudo dnf update
sudo dnf install -y mysql-server mysql-common mycli
sudo mysql_secure_installation
```
### Install MySQL on Arch/Manjaro/Arco/ Arch forks
>[!warning]
> Common Arch repositories don't populate MySQL CE and the vendor doesn't provide one. The packages are available in the [AUR](https://aur.archlinux.org/) but this is \***not recommended**\* for a production environment!!
##### Enable the AUR (if not already available)
>[!notice]
>_This **must** be done by a non-root user!_
```shell
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si
```
##### Proceed with installation from the AUR
```bash
yay pacman -Syyu
yay -S mysql mysql-utilities mycli --noconfirm
sudo mysql_secure_installation
```
### Deploy MySQL in Docker
- [https://hub.docker.com/_/mysql/](https://hub.docker.com/_/mysql/)
- [https://dev.mysql.com/doc/mysql-installation-excerpt/8.0/en/docker-mysql-getting-started.html](https://dev.mysql.com/doc/mysql-installation-excerpt/8.0/en/docker-mysql-getting-started.html)
### Deploy MySQL in Kubernetes
- [https://dev.mysql.com/doc/mysql-operator/en/](https://dev.mysql.com/doc/mysql-operator/en/)
- [https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-application/](https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-application/)
---
## Create Administrative User
Access the MySQL command line by entering `mysql -u root -p` in the shell followed by the Database `root` password.
Create a new user `newuser` for the host `localhost` with a new `password`:
```sql
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
```
Grant all permissions to the new user
```sql
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
```
Update permissions
```sql
FLUSH PRIVILEGES
```

206
databases/postgres.md Normal file
View File

@ -0,0 +1,206 @@
# PostgreSQL Cheat-Sheet
[PostgreSQL](https://www.postgresql.org/) or also known as Postgres, is a free and open-source relational database management system. PostgreSQL features transactions with Atomicity, Consistency, Isolation, Durability (ACID) properties automatically updatable views, materialized views, triggers, foreign keys, and stored procedures. It is designed to handle a range of workloads, from single machines to data warehouses or web services with many concurrent users.
---
## Installation
### Install PostgreSQL on Debian/Ubuntu/Mint/Zorin/forks
```bash
sudo apt update
sudo apt install -y postgresql postgresql-contrib postgresql-client
sudo systemctl status postgresql.service
```
### Install PostgreSQL on RHEL/Fedora/CentOS/Alma/Rocky
```bash
sudo dnf update
sudo dnf install -y postgresql-server
```
### Install PostgreSQL on Arch/Manjaro/Arco/forks
```bash
sudo pacman -Syyu
sudo pacman -S postgresql --noconfirm
```
### Deploy PostgreSQL in Docker
- [https://hub.docker.com/_/postgres/](https://hub.docker.com/_/postgres/)
- [https://github.com/postgres/postgres](https://github.com/postgres/postgres)
### Deploy PostgreSQL on Kubernetes with Zalando Postgres Operator
Postgres is probably the database which is most common on Cloud platforms and also, running on Kubernetes environments. There are several so called "Kubernetes Operators" which handle the deployment of Postgres clusters for you. One of it is the [Postgres Operator by Zalando](https://github.com/zalando/postgres-operator).
You can find some tutorials regarding deployment of the operator and how to work with it, in the link list below:
- [Deploy Zalando Postgres Operator on your Kubernetes cluster](https://thedatabaseme.de/2022/03/13/keep-the-elefants-in-line-deploy-zalando-operator-on-your-kubernetes-cluster/)
- [Configure Zalando Postgres Operator Backup with WAL-G](https://thedatabaseme.de/2022/03/26/backup-to-s3-configure-zalando-postgres-operator-backup-with-wal-g/)
- [Configure Zalando Postgres Operator Restore with WAL-G](https://thedatabaseme.de/2022/05/03/restore-and-clone-from-s3-configure-zalando-postgres-operator-restore-with-wal-g/)
---
## Connecting to Postgres
### Connect to local Postgres instance
A local connection (from the database server) can be done by the following command:
```sh
sudo -u postgres psql
```
### Connect to remote Postgres instance
Note, that you first have to install the `postgresql-client` package, (`postgresql` via Homebrew on macOS) on the client machine. A connection from a remote host can be done by the following command:
```sh
psql -h {pg_host} -U {username} -d {database} -p {port}
```
## Set password for postgres database user
The password for the `postgres` database user can be set the the quickcommand `\password` or by `alter user postgres password 'Supersecret'`. A connection using the `postgres` user is still not possible from the "outside" hence to the default settings in the `pg_hba.conf`.
### Update pg_hba.conf to allow postgres user connections with password
In order to allow connections of the `postgres` database user not using OS user authentication, you have to update the `pg_hba.conf` which can be found under `/etc/postgresql/12/main/pg_hba.conf`.
```sh
sudo vi /etc/postgresql/12/main/pg_hba.conf
...
local all postgres peer
...
```
Change the last section of the above line to `md5`.
```sh
local all postgres md5
```
A restart is required in order to apply the new configuration:
```sh
sudo systemctl restart postgresql
```
Now a connection from outside the database host is possible e.g.
```sh
psql -U postgres -d postgres -h databasehostname
```
## Creation of additional database users
A database user can be created by the following command:
```sql
create user myuser with encrypted password 'Supersecret';
CREATE ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
myuser | | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
```
## Creation of additional databases
One can create new Postgres databases within an instance. Therefore you can use the `psql` command to login (see above).
```sql
CREATE DATABASE dbname OWNER myuser;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
dbname | myuser | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
```
You can leave the `OWNER` section of the command, when doing so, the current user will become owner of the newly created database.
To change the owner of an existing database later, you can use the following command:
```sql
postgres=# alter database dbname owner to myuser;
ALTER DATABASE
```
## Backup and Restore
There are near to endless combinations in tools and parameters to backup postgres databases. Below you can find some examples using the Postgres built-in tools `pgdump`, `pg_basebackup` and `pg_restore`.
### pg_dump / pg_dumpall
Using `pg_dump` or `pg_dumpall` enables you to extract / export a PostgreSQL database(s) into a (SQL) script file or a custom archive file.
#### pg_dump
The following command creates a custom archive file from a database specified with `-d`. To export data in custom format, you have to specify so with the `-F c` option. Custom file dumps have the benefit, that they are compressed by default.
```bash
Using the `--create` option will include the SQL commands in the dump script that will create the database before importing it later. The `-Z 9` option in this example compresses the SQL script created with the highest available compression rate (`0-9`).
```bash
pg_dump -h vmdocker -U awx -d awx --create -f -Z 9 /tmp/awx_dump.sql.gz
```
The following command creates a custom archive file from a database specified with `-d`. To export data in custom format, you have to specify so with the `-F c` option. Custom file dumps have the benefit, that they are compressed by default.
```bash
pg_dump -h {pg_host} -U {username} -d {database} -F c -f /pg_dump/dumpfile.dmp
```
Custom format files can only be restored by `pg_restore` (see below). A SQL dump can be restored by using `psql`.
```bash
psql -d newdb -f db.sql
```
A complete guide of `pg_dump` from the official documentation can be found [here](https://www.postgresql.org/docs/current/app-pgdump.html).
#### pg_dumpall
A full dump of all databases of a Postgres instance can be done by `pg_dumpall`. It will include also user creation information.
A difference to `pg_dump`, you cannot choose for different output formats. `pg_dumpall` will always create a SQL script as output. Therefore,
you don't need `pg_restore` for restoring a "full" dump. Only `psql` is needed (see below).
```bash
pg_dumpall -h {pg_host} -U postgres > database.out
```
If you use password authentication it will ask for a password each time. It is convenient to have a `~/.pgpass` file or `PGPASSWORD` environment variable set.
So importing a full dump is really easy by the following `psql` command:
```bash
psql -h {pg_host} -f databaseb.out -U postgres
```
A complete guide of `pg_dumpall` from the official documentation can be found [here](https://www.postgresql.org/docs/current/app-pg-dumpall.html).
### pg_restore
`pg_restore` can be used to restore custom file dumps created by `pg_dump`.
The following command will create the database (which has been dumped before).
```bash
pg_restore -h {pg_host} -U {pg_user} -d postgres --create -F c /tmp/db.dmp -v
```
A complete guide of `pg_restore` from the official documentation can be found [here](https://www.postgresql.org/docs/current/app-pgrestore.html).

12
databases/sqlite.md Normal file
View File

@ -0,0 +1,12 @@
# SQLite Cheat-Sheet
SQLite is a relational database contained in a C library. In contrast to many other databases, SQLite is not a client-server database engine. Rather, it's embedded into an end program.
SQLite generally follows the [PostgreSQL](databases/postgres.md) syntax but does not enforce type checking.
You can open a SQLite Database with `sqlite3 <filename>` directly.
---
## Commands
`.help` Shows all commands `.databases` Show all existing databases `.quit` Exists `.tables` Shows all tables `.backup` Backups current database