Bases de dades — fitxa
Seccions d'aquesta pàgina 7
01General#
| W3-mSQL | PHP3 (es) (ch) | JDBC | |||
|---|---|---|---|---|---|
| Info | GUI | ||||
| miniSQL | |||||
| MySQL |
|
||||
| PostgreSQL | |||||
Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
02SQL#
- Comparison
of relational database management systems
- Firebird
- MariaDB
(fork from MySQL) (SkySQL) (wp)
- MySQL
- PostgreSQL
- SQLite
- commands:
MySQL
sqlite
PostgreSQL
admin user
root
postgres
default database
postgres CLI
user
create a user
su - postgrecreateuser --createdb my_user
delete a user
dropuserdatabase
create a database
createdb--username my_user my_dbdelete a database
dropdbstatistics
pg_stat_activity
pg_cancel_backend
backup database
backup one database
sqlite3 sqlite.db .dump >toto.sqlpg_dump[-h <host>] [-p <port>] [-t <table>] my_db[| gzip] >my_db.sql[.gz]
restore one database
- (create users)
createdb [-T template0] my_db
psql [--set ON_ERROR_STOP=on] [--single-transaction] my_db <my_db.sql- or:
cat my_db.sql.gz | gunzip | psql my_db
backup one database (custom dump format)
pg_dump -Fc my_db >my_db.sql
restore one database (cuustom dump format)
pg_restore-d my_db my_db.sql
backup cluster
backup all databases in cluster
pg_dumpall >my_cluster.sql
backup cluster structure only
pg_dumpall--globals-only>my_cluster.sqlrestore cluster
psql -fmy_cluster.sql postgres
connection
connection to server mysql -u my_user -psqlite3 toto.dbpsql --username my_user [-h <host>] [-p <port>] my_dbcommands
general
help
.help
\?
info
\conninfo
database
show databases
show databases;
\l
use database
use toto_db
\c toto_db
show tables
show tables;
.tables.schema
\dt
schema of a table
desctable_name;.schema table_name
\d table_name
users
\du
processes
show processlist;
max connections
SHOW VARIABLES LIKE 'max_connections';
SQL
Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
- SQL
Language (wp)
- MySQL 5.7 Reference Manual
- 11. Data types
- 11.3 Date and time types
`tempsCreacio` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
- SQL
Tutorial (Zentut)
- Creation of a database
CREATE DATABASE toto_db;
- Deletion of a database
DROP DATABASE toto_db;
- Creation of a user
CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';- At the same time of database creation:
CREATE DATABASE IF NOT EXISTS mydatabase_db;
GRANT ALL ON mydatabase_db.* TO 'mydatabase_user'@'%' IDENTIFIED BY '...';
GRANT ALL ON mydatabase_db.* TO 'mydatabase_user'@'localhost' IDENTIFIED BY '...'; FLUSH PRIVILEGES;
- Esborra una fila en una taula.
DELETE FROM toto_table WHERE camp = 'S';- segons la data / according to date:
- esborrat en cadena en taules relacionades / chain
deletion in related tables
- SQL DELETE
- 1.8.5.4 Foreign key differences
- InnoDB
- ON DELETE
- MyISAM
- Multiple table DELETE example (MyISAM):
DELETE toto1, toto2, toto3
FROM toto1 INNER JOIN toto2 INNER JOIN toto3
WHERE toto1.tempsCreacio < CURRENT_TIMESTAMP - interval '12' hour
AND toto2.id=toto1.id
AND toto3.id=toto1.id;
- Afegeix una columna (camp) a una taula (ALTER, NOT NULL):
ALTER TABLE toto_table ADD COLUMN toto_field varchar(10) NOT NULL
- Esborra totes les files d'una taula:
TRUNCATE TABLE toto_table;
- Esborra una taula de la base de dades:
- DROP TABLE toto_table;
- Afegeix un element a una taula
INSERT INTO nom_taula(columna1, columna2)
VALUES ('first value_1', 'second_value_1'),
('first_value_2','second_value_2');
- Mostra el contingut d'una taula
SELECT * FROM toto_table;
- Subqueries
- Correlated subquery (wp)
- CREATE VIEW toto1_view AS
SELECT id AS toto1ID
FROM toto1
WHERE tempsCreacio < CURRENT_TIMESTAMP - interval '20' minute;
SELECT *
FROM toto2 NATURAL JOIN toto1_view
WHERE toto2.id = toto1ID;
DELETE toto2
FROM toto2 INNER JOIN toto1_view
WHERE toto2.id = toto1ID;DROP VIEW toto1_view;
03No SQL (document-oriented)#
- NOSQL
Databases
- NoSQL (wp)
- Implementacions / Implementations
04MongoDB#
- MongoDB
(wp)
- Integration with Django
- Javascript
- MongoDB replication (replica set)
- Admin
UIs
05MySQL / MariaDB#
- MySQL (es)
- Install
and set the root password
- MySQL Workbench
urpmi mysql-workbench [python-sqlite2]
- MySQL Tools
- MySQL Migration Toolkit
- MySQL Administrator
urpmi mysql-administrator
- Server hostname: localhost (127.0.0.1 does not work)
- MySQL Query Browser
- urpmi mysql-query-browser
- Utilitats / Utilities
- Contrasenya root / Root password
- Accés remot / Remote access
- Time zones
- Config
- /etc/mysql/my.cnf
- max_connections = 151
- Problemes / Problems
- InnoDB: Fatal error: cannot allocate memory for the buffer pool
- Too many connections
- Django: "Too many connections"
06Oracle#
07PostgreSQL#
- Installation
- Mageia
urpmi postgresql9.4-serversystemctl enable postgresql.servicesystemctl start postgresql.service- CentOS
- YUM installation
yum install https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_64/pgdg-centos94-9.4-3.noarch.rpm
yum install postgresql94-serverpostgresql94-contrib/usr/pgsql-9.4/bin/postgresql94-setup initdb
systemctl enable postgresql-9.4.servicesystemctl start postgresql-9.4.servicesystemctl status postgresql-9.4.service
- version from CentOS original repository (9.2):
yum install postgresql-serverpostgresql-setup initdb
systemctl enable postgresql.servicesystemctl start postgresql.service-
repo
version
bin dir
config dir
database dir
service
CentOS
original repo
9.2
/bin/
/var/lib/pgsql/data/
/var/lib/pgsql/data/base/ systemctl start postgresql.service postgresql.org
9.4
/usr/pgsql-9.4/bin/
/var/lib/pgsql/9.4/data/ /var/lib/pgsql/9.4/data/base/ systemctl start postgresql-9.4.service Mageia
distribution
9.4
/usr/bin/
/var/lib/pgsql/data/
/var/lib/pgsql/data/base/ systemctl start postgresql.service
Ubuntu
distribution
9.3
/etc/postgresql/9.3/main/
/var/lib/postgresql/9.3/main/base/ sudo service postgresql start Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
- Get running version:
psql_version=$(sudo -u postgres psql postgres -c 'SELECT version()' | awk '$1 ~ /PostgreSQL/ {print $2}' | egrep -o '[0-9]{1,}\.[0-9]{1,}')
- Setup
- authentication
- What is the Default Password for PostgreSQL?
- Authentication Methods
- /var/lib/pgsql/[<version>]/data/pg_hba.conf
- password check (sent in md5):
host all all 127.0.0.1/32 md5- no password check:
host all all 127.0.0.1/32 trust- Fabric
from fabric.contrib.files import sed
def postgresql_install():
...
# spaces are important
sed('/var/lib/pgsql/data/pg_hba.conf',
'host all all 127.0.0.1/32 ident',
'host all all 127.0.0.1/32 md5',
use_sudo=True )
- Django (GIS)
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': '...',
'USER': '...',
'PASSWORD': open(os.path.join(BASE_DIR, 'db_p.txt'),'r').read().strip(),
'HOST': '127.0.0.1',
'PORT': '', # Set to empty string for default.
}
}- accessible from other computers (e.g.:
172.31.0.0/16)
- /var/lib/pgsql/data/postgresql.conf
listen_addresses = '*'
- /var/lib/pgsql/data/pg_hba.conf
host all all 172.31.0.0/16 md5systemctl restart postgresql.service- open firewall: TCP 5432
- create a superuser named 'my_su'
sudo su - postgrescreateuser --superuser my_suexit- create a user named 'geo_user', which can create databases
- bash
- Mageia
- option 1: password specified interactively
createuser --username postgres --createdb geo_user- option 2: specified password
psql--username postgres--command="CREATE USER geo_user WITH PASSWORD 'xxxx' CREATEDB;"- CentOS
- as, by default, access is "ident", running commands as master_user (postgres) will have superuser access
sudo su - postgres- option 1: password specified interactively
createuser --createdb geo_user- option 2: specified password
psql --command="CREATE USER geo_user WITH PASSWORD 'xxxx' CREATEDB;"
exit- Fabric
env.db_user = 'geo_user'
env.postgresql_master_user = 'postgres'
put('db_p.txt','/tmp')
sudo('psql --command "CREATE USER %(db_user)s WITH PASSWORD \'$(cat /tmp/db_p.txt)\' CREATEDB;"' % env, user='%(postgresql_master_user)s' % env)
- create a database as user geo_user
- optionally, to avoid having to specify a password, create the file:
echo '127.0.0.1:5432:postgres:geo_user:xxxx' > ~/.pgpasschmod 600 ~/.pgpass
createdb -h 127.0.0.1 --username=geo_user geodjango-
NOTES:
cli tools
psql
create a superuser
createuser --superuser my_su
create a user that can create databases
createuser[-hdb_host]--username master_user --createdb my_userpsql [-h db_host]--username master_user--command="CREATE USER my_user WITH PASSWORD 'xxxx' CREATEDB;"create a database
createdb [-h db_host] --username=my_user my_databasepsql [-h db_host]--username my_user--command="CREATE DATABASE my_user;"Aquesta taula continua cap a la dreta — desplaça-la o gira el mòbil.
- usually, master_user is
postgres
- in previous examples, my_user was geo_user
- if not specified, db_host is 127.0.0.1
- usually, master_user is
- check the connectivity to the database
- optionally, to avoid having to specify a password, modify the file:
echo '127.0.0.1:5432:geodjango:geo_user:xxxx' >> ~/.pgpass
psql -h 127.0.0.1 --username=geo_user geodjango
- remove a user
sudo su - postgresdropuser geo_userexit- completely remove database server data:
- sudo rm -rf /var/lib/pgsql/data
- then you can run "
postgresql-setup initdb" again: it will create and populate /var/lib/pgsql/data - 9.26. System Administration Functions
- list of active connections:
SELECT * FROM pg_stat_activity;
- PostGIS
- GeoDjango
- Installation
- Mageia
urpmi postgis- CentOS
yum install postgis- Only client
- when server is at e.g. AWS RDS, and we need to run manage.py migrate
- CentOS
yum install geos gdal
- Setup
- Installing
PostGIS (GeoDjango)
- add postgis extension to the database (used user must have root privileges)
psql --username postgresgeodjango --command "CREATE EXTENSION postgis;"- Backup
- SQL Dump
- Install
- CentOS
sudo yum install postgresql96-contrib
sudo yum install holland-pgdump- Usage
- pg_dump
- pg_dumpall
$ sudo su - postgres -c "/usr/pgsql-${pgsql_version}/bin/pg_dumpall | gzip > /tmp/db_dump_$(date '+%Y%m%d_%H%M').sql.gz"- #
su postgres -c "/usr/pgsql-${pgsql_version}/bin/pg_dumpall | gzip > /tmp/db_dump_$(date '+%Y%m%d_%H%M').sql.gz" /usr/pgsql-${pgsql_version}/bin/psql -fmy_cluster.sql postgres- pg_recover
- Upgrade
- Upgrading a PostgreSQL Cluster (9.4)
- pg_dumpall
- pg_upgrade (pg_migrator)
- PostgreSQL upgrade on CentOS
- 9.2 -> 9.4
- install new postgresql version
- Linux downloads (Red Hat family)
sudoyum install https://download.postgresql.org/pub/repos/yum/...sudo yum install postgresql94-server/usr/pgsql-9.4/bin/postgresql94-setup initdb- install new pg_upgrade
sudo yum install postgresql94-contrib- check upgrade
sudo su postgres
/usr/pgsql-9.4/bin/pg_upgrade --check -b /usr/bin -B /usr/pgsql-9.4/bin -d /var/lib/pgsql/data -D /var/lib/pgsql/9.4/data- Problemes / Problems
FATAL: unrecognized configuration parameter "unix_socket_directory"- pg_upgrade unrecognized configuration parameter “unix_socket_directory”
postgres --describe-config | grep unix_socket
- stop old service
sudo systemctl stop postgresql.service- run upgrade
sudo su postgres
/usr/pgsql-9.4/bin/pg_upgrade -b /usr/bin -B /usr/pgsql-9.4/bin -d /var/lib/pgsql/data -D /var/lib/pgsql/9.4/data
- copy config files
cp/var/lib/pgsql/data/postgresql.conf/var/lib/pgsql/9.4/data/postgresql.confcp/var/lib/pgsql/data/pg_hba.conf/var/lib/pgsql/9.4/data/pg_hba.conf- start new service
sudo systemctl start postgresql-9.4.servicesudo systemctl enable postgresql-9.4.service- analyze new cluster
./analyze_new_cluster.sh
- Replication
- SQLite Manager (Firefox extension)
- Integration with Django
- Replicació / Replication
- Limitacions / Limitations
- Dump to sql file
sqlite3 sqlite.db .dump > toto.sql- Migration from sqlite to MySQL
- Using Django
- python manage.py dumpdata
- Quick easy way to migrate SQLite3 to MySQL?
- answer
7747146: sqlfairy, SQL-Translator
- answer 6410922
- using SQL-Translator
- Instal·lació
- Mageia
- urpmi perl-SQL-Translator perl-DBD-SQLite
perl-Template-DBI
- Usage
- ...
http://www.francescpinyol.cat/bd.html
Primera versió: / First version:
Darrera modificació: 22 de juny de 2017 / Last update: 22nd June 2017