MySQLCheatSheet
MySQLisanopen-sourcerelationaldatabasemanagementsystem(RDBMS).
bylam
󰅂
LiveMonitoringof
MySQL
Therearetwousefultools:
mytop
innotop
with"mytop"beinganown
Debianpackage,while
"innotop"isincludedinthe
"mysql-client"package.
Frombothinnotophasthe
moreadvancedfunctionality.
Bothneedtobecalledwith
credentialstoconnecttothe
database:
mytop -u <user> -p<pas
sword>
innotop -u <user> -p<p
assword>
󰅂ShowMySQLStatus
Youcangetaverysimple
statusbyjustentering"s"in
the"mysql"commandline
clientprompt:
mysql> s
Youcanshowthereplication
statususing
SHOW SLAVE STATUS G
SHOW MASTER STATUS G
Notethatthe"G"insteadof
";"justmakestheoutputmore
readable.Ifyouhave
configuredslavestoreport
namesyoucanlistthemon
themasterwith:
SHOW SLAVE HOSTS;
󰅂CheckInnoDBstatus
show /*!50000 ENGINE
*/ INNODB STATUS;
󰅂
List
Databases/Tables/C
olums
Youcaneitherusethe
"mysqlshow"tool:
mysqlshow
# List all d
atabases
mysqlshow <database>
# List all t
ables of the given data
base
mysqlshow <database> <t
able> # List all c
olumns of the given tab
le in the given DB
Andyoucanalsodoitusing
queries:
SHOW DATABASES;
USE <database>;
SHOW TABLES;
DESCRIBE <table>;
󰅂
CheckandChange
LiveConfiguration
Parameters
Notethatyoucannotchange
allexistingparameters.
Somelike
innodb_pool_bufferrequirea
DBrestart.
show variables;
# L
ist all configuration
settings
show variables like 'k
ey_buffer_size'; # L
ist a specific paramet
er
set global key_buffer_
size=100000000; # S
et a specific paramete
r
󰅂
MySQLParameter
Optimization
YoucancheckMySQL
parametersofarunning
instanceusingtoolslike
MySQLTuner
MySQLTuning-Primer
pt-variable-advisor
Alsohavealookatthis
MySQLconfigparameter
explanation.
󰅂
RemoteMySQL
DumpandImport
Thefollowingcommand
allowsdumpingadatabase
fromonesourcehostthat
doesn'tseethetargethost
whenexecutedonathirdhost
thatcanaccessboth.Ifboth
hostscanseeeachotherand
onehasSSHaccesstothe
󰅂
ImportaCSVfileinto
MySQL
LOAD DATA IN '<CSV fil
ename>' INTO TABLE <ta
󰏪
󰅢
CheatSheetMaker.com SimpleCheatSheet.com
# Finally ensure to ed
it my.cnf to make the
change persistent
otheryoucansimplydropone
ofthesshcalls.
ssh <user@source host>
"mysqldump --single-tra
nsaction -u root --pass
word=<DB root pwd> <DB
name>" | ssh <user@targ
et host> "mysql -u roo
t --password=<DB root p
wd> <DB name>"
ble name> FIELDS TERMI
NATED BY ',' (<name o
f column #1>,<<name o
f column #2>,<...>);
󰅂
MySQLPager-
OutputHandling
Using"PAGER"orPyoucan
controloutputhandling.
Insteadofhaving10klines
scrollingbyyoucanwrite
everythingtoafileoruse
"less"toscrollthroughitfor
example.Touselessissue
pager less
Pageoutputintoascript
pager /home/joe/myscrip
t.sh
OrifyouhavePercona
installedgetatree-like
"EXPLAIN"outputwith
pager mk-visual-explain
andthenrunthe"EXPLAIN"
query.
󰏪
󰅢

MySql cheat sheet

  • 1.
    MySQLCheatSheet MySQLisanopen-sourcerelationaldatabasemanagementsystem(RDBMS). bylam 󰅂 LiveMonitoringof MySQL Therearetwousefultools: mytop innotop with"mytop"beinganown Debianpackage,while "innotop"isincludedinthe "mysql-client"package. Frombothinnotophasthe moreadvancedfunctionality. Bothneedtobecalledwith credentialstoconnecttothe database: mytop -u <user>-p<pas sword> innotop -u <user> -p<p assword> 󰅂ShowMySQLStatus Youcangetaverysimple statusbyjustentering"s"in the"mysql"commandline clientprompt: mysql> s Youcanshowthereplication statususing SHOW SLAVE STATUS G SHOW MASTER STATUS G Notethatthe"G"insteadof ";"justmakestheoutputmore readable.Ifyouhave configuredslavestoreport namesyoucanlistthemon themasterwith: SHOW SLAVE HOSTS; 󰅂CheckInnoDBstatus show /*!50000 ENGINE */ INNODB STATUS; 󰅂 List Databases/Tables/C olums Youcaneitherusethe "mysqlshow"tool: mysqlshow # List all d atabases mysqlshow <database> # List all t ables of the given data base mysqlshow <database> <t able> # List all c olumns of the given tab le in the given DB Andyoucanalsodoitusing queries: SHOW DATABASES; USE <database>; SHOW TABLES; DESCRIBE <table>; 󰅂 CheckandChange LiveConfiguration Parameters Notethatyoucannotchange allexistingparameters. Somelike innodb_pool_bufferrequirea DBrestart. show variables; # L ist all configuration settings show variables like 'k ey_buffer_size'; # L ist a specific paramet er set global key_buffer_ size=100000000; # S et a specific paramete r 󰅂 MySQLParameter Optimization YoucancheckMySQL parametersofarunning instanceusingtoolslike MySQLTuner MySQLTuning-Primer pt-variable-advisor Alsohavealookatthis MySQLconfigparameter explanation. 󰅂 RemoteMySQL DumpandImport Thefollowingcommand allowsdumpingadatabase fromonesourcehostthat doesn'tseethetargethost whenexecutedonathirdhost thatcanaccessboth.Ifboth hostscanseeeachotherand onehasSSHaccesstothe 󰅂 ImportaCSVfileinto MySQL LOAD DATA IN '<CSV fil ename>' INTO TABLE <ta 󰏪 󰅢
  • 2.
    CheatSheetMaker.com SimpleCheatSheet.com # Finallyensure to ed it my.cnf to make the change persistent otheryoucansimplydropone ofthesshcalls. ssh <user@source host> "mysqldump --single-tra nsaction -u root --pass word=<DB root pwd> <DB name>" | ssh <user@targ et host> "mysql -u roo t --password=<DB root p wd> <DB name>" ble name> FIELDS TERMI NATED BY ',' (<name o f column #1>,<<name o f column #2>,<...>); 󰅂 MySQLPager- OutputHandling Using"PAGER"orPyoucan controloutputhandling. Insteadofhaving10klines scrollingbyyoucanwrite everythingtoafileoruse "less"toscrollthroughitfor example.Touselessissue pager less Pageoutputintoascript pager /home/joe/myscrip t.sh OrifyouhavePercona installedgetatree-like "EXPLAIN"outputwith pager mk-visual-explain andthenrunthe"EXPLAIN" query. 󰏪 󰅢