diff --git a/mysql/README.md b/mysql/README.md new file mode 100644 index 0000000..041d002 --- /dev/null +++ b/mysql/README.md @@ -0,0 +1,339 @@ +--- +gitea: none +include_toc: true +--- +# MySQL + +Für **Joins**, siehe auch: [Roger SQL Join Tutorial](files/Roger_SQL_Join_Tut.docx) + +--- + +## System + +### Which MySQL is running? + +Für 5.7.x versionen... Percona, Oracle... + +``` sql +SELECT SUBSTR(variable_value,1, +LOCATE(' ',variable_value) - 1) DBVersion +FROM information_schema.global_variables +WHERE variable_name='version_comment'; +``` + +### Show global variables + +``` sql +SHOW GLOBAL VARIABLES; +SHOW GLOBAL VARIABLES LIKE 'innodb%'; +SHOW GLOBAL VARIABLES LIKE 'innodb_rollback_on_timeout'; +``` + +### Set global variables + +Only until next MySQL restart. For permanent change, use my.cnf under the [mysqld] header. + +``` sql +SET GLOBAL innodb_lock_wait_timeout = 320; +``` + +--- + +## Users, Grants + +### set root password + +``` sql +ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password'; +FLUSH PRIVILEGES; +``` + +siehe auch https://linuxhint.com/change-mysql-root-password-ubuntu/ + +### Show users + +``` sql +SELECT host,user,authentication_string FROM mysql.user; +``` + +### Add user + +``` sql +CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; +``` + +version 8.0 + +``` sq +nCREATE USER 'newuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; +``` + +### Show grants for user + +``` sql +SHOW GRANTS FOR 'user_name'@'host'; +``` + +### Grant + +also see: https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql#how-to-grant-different-user-permissions + +``` sql +GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost'; # give full root privileges +GRANT ALL PRIVILEGES ON db1.* TO 'newuser'@'localhost'; # give privileges for database db1 +FLUSH PRIVILEGES; +``` + +### Revoke + +``` sql +REVOKE type_of_permission ON database_name.* FROM 'username'@'localhost'; +FLUSH PRIVILEGES; +``` + +### Change Password for user + +``` sql +ALTER USER 'user-name'@'localhost' IDENTIFIED BY 'NEW_USER_PASSWORD'; +FLUSH PRIVILEGES; + +# or + +UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPass') +WHERE User = 'username' AND Host = 'localhost'; +FLUSH PRIVILEGES; +``` + +--- + +## Database + +### Drop DB + +``` sql +DROP DATABASE IF EXISTS database_name; +``` + +### Export DB + +``` +mysqldump -h mariadb-p01.infra.vs.ch -P 33003 -uingest -p --routines --lock-tables=false --single-transaction atom > ~/atomp-01-20210425.sql +``` + +Windows + +``` +mysqldump -h mariadb-p01.infra.vs.ch -P 33003 -uingest -p --routines --lock-tables=false --single-transaction atom --result-file="c:\atomp-01-20210425.sql" +``` + +Falls Fehler `mysqldump: Couldn't execute 'SELECT COLUMN_NAME, JSON_EXTRACT.....` diese Option noch mit einbeziehen +`--column-statistics=0` + +See also https://www.linode.com/docs/guides/use-mysqldump-to-back-up-mysql-or-mariadb/ + +### Import DB + +``` +mysql -uusername -p db_name < /path/to/file.sql +``` + +Falls komprimierter dump: + +``` +gunzip < file.sql.gz | mysql -uusername -p db_name +``` + +### Create DB + +``` +mysql -h localhost -u root -p -e "CREATE DATABASE atom CHARACTER SET utf8 COLLATE utf8_unicode_ci;" +``` + +### Get Database Properties + +``` sql +SELECT default_character_set_name, default_collation_name FROM information_schema.schemata WHERE schema_name = 'dar416'; +``` + +--- + +## Tables + +### Count rows in all tables + +``` sql +select sum(TABLE_ROWS) from information_schema.tables where table_schema = 'db-name'; +``` + +### Count rows in each table + +``` sql +SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'db-name'; +``` + +### Export/dump single table + +``` +mysqldump db_name table_name > table_name.sql +``` + +### Import single table + +``` +mysql -u username -p db_name < /path/to/table_name.sql +``` + +### Drop Tables + +``` sql +DROP TABLE IF EXIST [table 1], [table 2], [table n]; +``` + +### Select over single table + +#### SELECT mit GROUP BY und HAVING + +``` sql +SELECT post_title, COUNT(*) c +FROM wp_posts +WHERE post_type='article' + AND post_author=1 +GROUP BY post_title +HAVING c > 1 +``` + +#### Select to file + +```` +mysql -uroot -e "use atom_A; select io.identifier, io.id, information_object_i18n.title from information_object io left join information_object_i18n on io.id = information_object_i18n.id where io.identifier IS NULL or io.identifier like '#%' or io.identifier = '?' order by identifier;" > /home/ubuntu/identifier_not_number.csv +``` + +#### List rows that have the same value in a column + +``` sql +SELECT ve.vrzng_enht_id, vebhltn.bhltn_id +FROM vws_vrzng_enht_grund_daten ve, + vws_vrzng_enht_bhltn vebhltn +WHERE vebhltn.vrzng_enht_id = ve.vrzng_enht_id +AND vebhltn.bhltn_id IN( + SELECT vebhltn.bhltn_id FROM vws_vrzng_enht_bhltn vebhltn + GROUP BY vebhltn.bhltn_id HAVING COUNT(*) > 1 +) +``` + +### List duplicates (grouped), with additional col "found duplicates" + +``` sql +SELECT post_title, COUNT(post_title) +FROM wp_posts +WHERE post_type = 'article' + AND post_status = 'publish' + AND post_title NOT LIKE 'Der Chart des Tages' +GROUP BY post_title +HAVING COUNT(post_title) >= 2; + +# or, if all fields should be visible: +SELECT *, count(post_title) +FROM wp_posts +WHERE post_type = 'article' + AND post_status = 'publish' + AND post_title NOT LIKE 'Der Chart des Tages' +GROUP BY post_title +HAVING count(post_title) >= 2 +``` + +### Select over multiple tables + +#### SELECT with LEFT JOIN + +``` sql +SELECT fuw_master.*, fuw_master_variation.name +FROM fuw_master + LEFT JOIN fuw_master_variation + ON fuw_master.id = fuw_master_variation.id_master; +``` + +### Create + +#### Table with columns from 2 tables (using JOIN) + +``` sql +CREATE TABLE fuw_master_2 +SELECT fuw_master.*, fuw_master_variation.name +FROM fuw_master + LEFT JOIN fuw_master_variation + ON fuw_master.id = fuw_master_variation.id_master; +``` + +### Insert + +``` sql +INSERT INTO blabla (spaltenname1, spaltenname2) VALUES ('wert1', 123) +``` + +### Update + +``` sql +UPDATE table_name SET field1 = new-value1, field2 = new-value2 +[WHERE Clause] +``` + +With replace: + +``` sql +UPDATE table_name SET field_name = REPLACE(field_name, + string_to_find, + string_to_replace) +[WHERE Clause] +``` + +With joins: + +``` sql +UPDATE information_object_i18n AS io + LEFT JOIN property p1 + ON p1.object_id = io.id + LEFT JOIN property_i18n p2 + ON p1.id = p2.id + SET io.title = CONCAT('recovery_failed_', io.title) +WHERE p1.name LIKE 'PID' +AND p2.value = 'CH-000-0:1234'; +``` + +### Export from command line + +``` +mysql>SELECT * FROM tablexy +INTO OUTFILE 'path/outfile.csv' +FIELDS TERMINATED BY ',' +ENCLOSED BY '"', +ESCAPED BY '', +LINES TERMINATED BY '\n' +``` + +`path` muss meistens dem Pfad entsprechen, der unter `select @@secure_file_priv;` gesetzt ist! + +--- + +## Version Upgrade 5.7 to 8.0 ## + + - https://dev.mysql.com/doc/refman/8.0/en/upgrade-binary-package.html + - https://tastethelinux.com/2020/09/14/upgrade-mysql-server-from-5-7-to-8-ubuntu-18-04/ + +## Install 5.7 on Ubuntu 20 + + - https://www.vultr.com/docs/how-to-install-mysql-5-7-on-ubuntu-20-04/ + +## Fehlermeldungen + +### Error "1038 Out of memory, consider increasing server sort buffer size + + - https://www.skynats.com/blog/error-1038-mysql-memory-allocation-error/ + +### Disk voll wegen zu vielen binlogs + +In `/var/lib/mysql` sind zu viele binlogs. Lösung: auf nur 1 Woche reduzieren. Binlogs werden für Restorte gebraucht. + + - mysql service stoppen + - `/etc/mysql/conf.d/mysql.cnf` erweitern um: `binlog_expire_logs_seconds=604800` + - mysql neu starten diff --git a/mysql/files/Roger_SQL_Join_Tut.docx b/mysql/files/Roger_SQL_Join_Tut.docx new file mode 100755 index 0000000..5909389 Binary files /dev/null and b/mysql/files/Roger_SQL_Join_Tut.docx differ