Skip to content

Instantly share code, notes, and snippets.

@jfriv
jfriv / _catchall.conf
Last active March 3, 2018 17:33
nginx catchall vhost config
server {
listen 80 default_server;
server_name _;
root /Users/MacbookPro/Sites/$host/;
autoindex on;
index index.html index.php;
# include php settings
@jfriv
jfriv / prod-rds-snap-restore-to-dev-temp.sh
Created August 16, 2016 20:03
RDS manual snapshot and restore script
#!/bin/bash
# set up some variables
NOW_DATE=$(date '+%Y-%m-%d-%H-%M')
RESTORE_FROM_INSTANCE_ID=<source name>
TARGET_INSTANCE_ID=<target name>
TARGET_INSTANCE_CLASS=db.m4.large
VPC_ID=<vpc subnet id>
NEW_MASTER_PASS=<root password>
@jfriv
jfriv / mysql_build_drop_fk_for_table.txt
Last active March 4, 2018 01:56
MySQL build query to drop all foreign keys for a table
---------------------------------------------------------------------
---- build sql to drop all foreign keys for a table
---------------------------------------------------------------------
SELECT CONCAT('ALTER TABLE `',table_schema,'`.`',table_name,'` DROP FOREIGN KEY ',constraint_name,';')
FROM information_schema.table_constraints
WHERE constraint_type='FOREIGN KEY'
AND table_schema='{database}'
AND table_name IN ('{table}');
@jfriv
jfriv / find_all_fk_tables_for_a_table
Created March 4, 2018 01:58
MySQL find all tables which reference another table with a foreign key constraint
------------------------------------------------------------------------------
--- select all tables which have FKs that reference a table
------------------------------------------------------------------------------
SELECT *
FROM information_schema.REFERENTIAL_CONSTRAINTS
WHERE referenced_table_name LIKE '%{table}%';
@jfriv
jfriv / MySQL_grants_all_and_select_only
Created March 6, 2018 02:47
MySQL Grant Statements - All privileges and select only
GRANT ALL PRIVILEGES ON mydatabase.* to 'myuser'@'10.%' IDENTIFIED BY 'mypassword';
GRANT SELECT, CREATE TEMPORARY TABLES, SHOW DATABASES ON *.* TO 'myuser'@'10.%' IDENTIFIED BY 'mypassword';
FLUSH PRIVILEGES;
@jfriv
jfriv / mysql_select_all_column_names_for_a_table
Created March 6, 2018 02:54
MySQL select all column names for a table
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'mydatabase' AND TABLE_NAME = 'mytable'
@jfriv
jfriv / mysql_export_all_foreign_keys_for_database
Created March 6, 2018 02:57
MySQL export all foreign keys for a database into a csv file with header row
SELECT 'TABLE_SCHEMA', 'TABLE_NAME', 'CONSTRAINT_NAME', 'REFERENCED_TABLE_NAME', 'UNIQUE_CONSTRAINT_NAME', 'UNIQUE_CONSTRAINT_SCHEMA', 'UPDATE_RULE', 'DELETE_RULE'
UNION ALL
SELECT kcu.TABLE_SCHEMA, kcu.TABLE_NAME, kcu.CONSTRAINT_NAME, kcu.REFERENCED_TABLE_NAME, rc.UNIQUE_CONSTRAINT_NAME, rc.UNIQUE_CONSTRAINT_SCHEMA, rc.UPDATE_RULE, rc.DELETE_RULE
INTO OUTFILE '/tmp/fk_audit_mydatabase.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
@jfriv
jfriv / mysql_select_table_size_in_gb
Created March 6, 2018 02:58
MySQL select table data size and index size in GB for a database
SELECT table_schema, table_name, (data_length / 1024 / 1024 / 1024) as data_size_gb, (index_length / 1024 / 1024 / 1024) as index_size_gb
FROM information_schema.TABLES
WHERE table_schema='mydatabase'
GROUP BY table_name
ORDER BY index_length DESC LIMIT 10;