Advertisement
flycat

Шпора по MySQL/MariaDB

May 8th, 2020 (edited)
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.94 KB | None | 0 0
  1. # Пользователи, права и пароли
  2. create user 'alexey_li'@'%' identified by 'D5';
  3. grant select on tlight.* to 'alexey_li'@'%';
  4. grant select on mysql.* to 'alexey_li'@'%' identified by 'D5';
  5. revoke select on mysql.* from 'alexey_li'@'%' identified by 'D5';
  6. grant update(column-name) on table-name to user-name;
  7. grant all privileges on database_name.* TO 'username'@'localhost';
  8. flush privileges;
  9.  
  10. # Update password:
  11. alter user 'userName'@'localhost' identified by 'New-Password-Here';
  12. # For older mysql:
  13. set password for 'user-name-here'@'hostname' = PASSWORD('new-password');
  14.  
  15. show grants for 'replication'@'5.11.19.29';
  16. select User,Host from mysql.user;
  17. select * from mysql.user where User='root'\G
  18. select * from table where YourColumn IS NOT NULL;
  19.  
  20. # Процессы (аналог show full processlist):
  21. select * from INFORMATION_SCHEMA.PROCESSLIST;
  22. # Самый долгий запрос
  23. select * from INFORMATION_SCHEMA.PROCESSLIST order by TIME_MS desc limit 1\G
  24.  
  25. # Таблицы
  26. TRUNCATE `table`;
  27. show FULL COLUMNS FROM table;
  28. DESCRIBE `table`;
  29. # Структура - показать команду для создания таблицы в текущем виде:
  30. show create table orders\G
  31.  
  32. # Кодировки:
  33. # Посмотреть https://database.guide/show-the-collation-in-mariadb/ :
  34. show variables like "character_set_database";
  35. SHOW VARIABLES LIKE 'collation%';
  36. SHOW SESSION VARIABLES LIKE 'collation%';
  37. SELECT @@collation_server;
  38. # Посмотреть у базы:
  39. SELECT      default_character_set_name,      default_collation_name  FROM information_schema.schemata  WHERE schema_name = 'dbname';
  40.  
  41. # Изменить кодировки
  42. alter database DBNAME CHARACTER SET utf8 COLLATE utf8_general_ci;
  43. mysql --database=DBNAME -B -N -e "SHOW TABLES" | awk '{print "SET foreign_key_checks = 0; ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; SET foreign_key_checks = 1; "}' | mysql --database=DBNAME
  44.  
  45. # Добавить столбец:
  46. alter TABLE tablename ADD columnname datatype;
  47. # Удалить столбец:
  48. alter TABLE tablename DROP COLUMN columname;
  49.  
  50. # Переменные посмотреть:
  51. show variables like "expire_logs_days";
  52. show variables like "skip_name_resolve";
  53. select @@hostname;
  54. show variables where Variable_name like '%host%';
  55. select host from information_schema.processlist WHERE ID=connection_id(); # Client IP
  56. show status where `variable_name` = 'Threads_connected'; # Number of current connections
  57. SHOW STATUS WHERE `variable_name` = 'Max_used_connections'; # Max used connections
  58. SHOW VARIABLES LIKE "max_connections";
  59.  
  60. long_query_time
  61. set GLOBAL log_slave_updates=1;
  62. set GLOBAL slow_query_log=1;
  63.  
  64. # Изменить:
  65. set GLOBAL expire_logs_days=5;
  66. SHOW BINARY LOGS;
  67. PURGE BINARY LOGS BEFORE '2023-07-02 23:46:26';
  68.  
  69. INSERT INTO COLUMN1 (orderkey, customer) values (1, 'flycat');
  70. select customer from COLUMN1 where customer like '%lyc%';
  71.  
  72. # Просто SELECT
  73. select column from table ORDER BY RAND() LIMIT 1;
  74.  
  75. # Работа с временем
  76. select CURRENT_TIMESTAMP;
  77. select now();
  78. select (now()-INTERVAL 1 HOUR);
  79.  
  80. # Установка MariaDB:
  81. curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
  82.  
  83. # Данные пользователя для доступа - файл ~/.my.cnf
  84. [client]
  85. port=3306
  86. host=127.0.0.1
  87. user=flycat
  88. password=******
  89.  
  90. mysql -s # Не выводить таблицу
  91. mysql -ss # Выводить только результат
  92.  
  93. # Size of databases
  94. SELECT table_schema 'DB name', SUM(data_length + index_length) 'Size in Bytes', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) 'Size in MiB' FROM information_schema.tables  GROUP BY table_schema;
  95.  
  96. # Size of tables database bookstore:
  97. SELECT
  98.   TABLE_NAME AS `Table`,
  99.   ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) AS `Size (MB)`
  100. FROM
  101.   information_schema.TABLES
  102. WHERE
  103.   TABLE_SCHEMA = "bookstore"
  104. ORDER BY
  105.   (DATA_LENGTH + INDEX_LENGTH)
  106. DESC;
  107.  
  108. # Конвертация таблиц
  109. req ()
  110. {
  111.     mar="/usr/bin/mariadb -s"
  112.     echo $@| $mar
  113. }
  114.  
  115. #for i in $(req "SELECT DISTINCT * FROM information_schema.columns WHERE table_schema = 'zabbix' AND COLLATION_NAME LIKE 'utf8mb4%' order by table_name;")
  116. for i in $(req "SELECT DISTINCT table_name FROM information_schema.columns WHERE table_schema = 'zabbix'order by table_name;")
  117. do
  118.     req "show create table zabbix.${i};"|grep -q "utf8mb4" && req "ALTER TABLE zabbix.${i} CONVERT TO CHARACTER SET utf8mb3 COLLATE utf8mb3_bin;"
  119. done
  120.  
  121. # my.cnf:
  122. skip-name-resolve=1
  123. performance_schema       = ON
  124. max_allowed_packet     = 1G
  125. max_connections        = 160
  126. plugin_load_add = wsrep_info
  127.  
  128. log_error = /var/log/mysql/error.log
  129. slow_query_log
  130. log_slow_query_file    = /var/log/mysql/mariadb-slow.log
  131. log_slow_query_time    = 10
  132. log_slow_verbosity     = query_plan,explain
  133. log-queries-not-using-indexes
  134. log_slow_min_examined_row_limit = 1000
  135.  
  136. innodb_buffer_pool_size = 18G
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement