MySQL / MariaDB Database

Login

mysql -u root -p 

Optimize all databases

mysqlcheck --optimize --all-databases --host=localhost --user=${DB_USER} --password=${DB_PASS}

Links
phpMyAdmin - phpMyAdmin downloads

GUI

apt-get install -y mysql-admin

Allow access from outsice

sed -i 's|bind-address|#bind-address|g' /etc/mysql/my.cnf 
/etc/init.d/mysql restart

Allow access to database magento for user dbo_magento from any host

GRANT ALL PRIVILEGES ON magento.* TO  dbo_magento@'%' IDENTIFIED BY 'your_pass' 
 
# dont LOCK TABLE on export / import
mysqldump  --single-transaction --quick --skip-trigger ...

Export structure from a single table

mysqldump --no-data ${DB_NAME} ${TABLE_NAME} > ${DB_NAME}.${TABLE_NAME}.$(hostname).$(date -I).sql

Drop all tables from typo3_cms schema

DB_USER=foo
DB_PASS=bar
DB_NAME=db1
mysqldump -u ${DB_USER} -p${DB_PASS} --add-drop-table --no-data ${DB_NAME} | grep ^DROP | mysql -u ${DB_USER} -p${DB_PASS} ${DB_NAME}
 
DROP DATABASE db1;
CREATE DATABASE db1 CHARACTER SET utf8 COLLATE utf8_general_ci;

SSL
http://mysqlserverteam.com/ssltls-and-rsa-improvements-for-openssl-linked-mysql-5-7-binaries/

SSH tunnel

ssh -C -L 3306:db.example.com:3306 foo@gw.example.com -N
echo "show databases;" | mysql --host=127.0.0.1 --port=3306 --user='user' --password='pass'

Report / Tuning
http://www.mysqlcalculator.com/
http://www.cyberciti.biz/faq/mysql-server-status-with-mysqlreport-report-script/
https://github.com/major/mysqltuner-perl

Configure connection

# listen for localhost connections only
bind-address = 127.0.0.1
# listen for connections on all interfaces
bind-address = 0.0.0.0
# listen for connections only on specific IP
bind-address = 192.168.1.2

test MySQL service

telnet 192.168.1.2 3306

Secure access to MySQL server by Iptables
http://www.panticz.de/iptables

Forward localhost (socket) to remote host

apt-get install socat
mkdir -p /var/run/mysqld
socat UNIX-LISTEN:/var/run/mysqld/mysqld.sock,fork,reuseaddr,unlink-early,user=myuser,group=mygroup,mode=777 TCP:db.example.com:3306 &
# test
echo "select @@hostname;" | mysql -u db_user -p

Backup

FLUSH TABLES tbl_list WITH READ LOCK;

Run query from cli

mysql -u user -pPASS1234  database-name -e 'SQL Query'

Show only result

mysql --skip-column-names -s -e "SELECT id FROM table1 WHERE name=hsot");

Format output

SELECT * FROM instances WHERE display_name = 'foo-dev' \G

Modify table

# Add column / alter table
ALTER TABLE table
ADD [COLUMN] column_name column_definition [FIRST|AFTER existing_column];
 
# Remove column
ALTER TABLE table_name
DROP COLUMN column_name;
 
# modify column
ALTER TABLE room MODIFY property_id bigint(20) not null;
 
# add primary key
ALTER TABLE USER ADD PRIMARY KEY (C_USRKEY);

Get process list

SHOW ENGINE INNODB STATUS\G
SELECT count(*) FROM INFORMATION_SCHEMA.PROCESSLIST;
SELECT user, count(*) FROM INFORMATION_SCHEMA.PROCESSLIST group by user;

slave / master status

mysql -e "show slave status\G"
mysql -e "show master status\G"
mysql -e "slave start"
 
# (re)sync
SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';
rm -rf /var/lib/mysql/*
service mysql start

Links
http://www.greensql.net/ - open source database firewall for MySQL and PostgreSQL
http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
http://www.igvita.com/2007/10/10/hands-on-mysql-backup-migration/