Skip to content

Instantly share code, notes, and snippets.

@davidkarban
Last active January 27, 2020 14:42
Show Gist options
  • Save davidkarban/563af83fe2b52713458d4eed060efe6a to your computer and use it in GitHub Desktop.
Save davidkarban/563af83fe2b52713458d4eed060efe6a to your computer and use it in GitHub Desktop.
Training materials

Pre flight check

Putty: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

Installation

Load test database

apt install git
git clone https://github.com/datacharmer/test_db
cd test_db
mysql < employees.sql

Configuration

Dynamic variables:

Storage Engines

Innodb

SQL Modes

SHOW commands

Transactions

Isolation levels

Deadlocks

MySQL problems

  • FK on myISAM
CREATE TABLE parent (
id INT NOT NULL,
name varchar(128),
PRIMARY KEY (id)
) ENGINE=MyISAM;

CREATE TABLE child (
id INT,
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id)
REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=MyISAM;

SHOW CREATE TABLE child;
  • SQL MODES
SELECT @@GLOBAL.sql_mode;
INSERT INTO parent VALUES('123test', 'Foo Bar');
SELECT * FROM parent;

SET SESSION sql_mode='';
INSERT INTO parent VALUES('123test');
SHOW WARNINGS;
SELECT * FROM parent;

User Management

Logging

SHOW VARIABLES LIKE "%general%";
SET GLOBAL general_log=ON;
  • slow log
SHOW VARIABLES LIKE "%slow%";
SET GLOBAL slow_query_log=ON;
SHOW VARIABLES LIKE "%time%";
SET GLOBAL long_query_time=0.1;

UPDATE salaries SET salary=salary*2;
UPDATE salaries SET salary=salary+2;
pt-query-digest /var/lib/mysql/localhost-slow.log
  • binary log
log_bin=localhost-binary
systemctl restart mysql

# do updates/inserts

mysqlbinlog /var/lib/mysql/localhost-binary.000001

Security

Root user security

my.cnf: Older way, works, not so secure (saving root pwd to the file).

vim /root/.my.cnf
[client]
password=YOURSECRETPASSWORD
chmod 600 /root/.my.cnf

unix auth plugin

  • Secure non password auth. Only root user can log to root account.

Application level trivia

SELECT * FROM table WHERE id=$variable;
$variable="15; UPDATE TABLE users;"
https://app.com/show.php?variable=15; UPDATE TABLE

db: SELECT * FROM table WHERE id=15; UPDATE TABLE users;

$sql =prepare SELECT * FROM table WHERE id=?
query $sql, $variable

db: SELECT * FROM table WHERE id="15; UPDATE TABLE users"

Network security

Backup and recovery

TASK: xtrabackup: https://www.percona.com/doc/percona-xtrabackup/2.3/backup_scenarios/incremental_backup.html

Disaster recovery

Management tools

Monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment