Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
public:mysql [2011/04/08 13:02] – Nico | public:mysql [2022/08/04 15:35] (current) – fix indent Nico | ||
---|---|---|---|
Line 14: | Line 14: | ||
</ | </ | ||
+ | ===== create user with a dedicated database ===== | ||
+ | <code sql> | ||
+ | mysql> CREATE DATABASE db; | ||
+ | mysql> GRANT ALL PRIVILEGES ON db.* TO ' | ||
+ | </ | ||
+ | |||
+ | ===== query cache ===== | ||
+ | |||
+ | you can see the query cache statistics at any time by issuing: | ||
+ | |||
+ | <code sql> | ||
+ | mysql> SHOW VARIABLES LIKE ' | ||
+ | </ | ||
+ | |||
+ | if all values are 0, the query cache is not working. | ||
+ | |||
+ | enable it by setting the cache size in my.cnf: | ||
+ | |||
+ | < | ||
+ | query-cache-type = 1 | ||
+ | query-cache-size = 32M | ||
+ | </ | ||
+ | |||
+ | ===== one innodb file per table ===== | ||
+ | |||
+ | by default, innodb use a gigantic " | ||
+ | |||
+ | < | ||
+ | innodb_file_per_table | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | </ | ||
===== replication ===== | ===== replication ===== | ||
Line 19: | Line 52: | ||
* Set up an account on the master server that the slave server can use to connect | * Set up an account on the master server that the slave server can use to connect | ||
+ | |||
<code sql> | <code sql> | ||
mysql> GRANT REPLICATION SLAVE ON *.* TO ' | mysql> GRANT REPLICATION SLAVE ON *.* TO ' | ||
Line 27: | Line 61: | ||
* Flush all the tables and block write statements | * Flush all the tables and block write statements | ||
+ | |||
<code sql> | <code sql> | ||
mysql> FLUSH TABLES WITH READ LOCK; | mysql> FLUSH TABLES WITH READ LOCK; | ||
</ | </ | ||
+ | |||
* use tar to create an archive that includes all databases | * use tar to create an archive that includes all databases | ||
+ | |||
<code bash># tar -cvf / | <code bash># tar -cvf / | ||
+ | |||
* read the value of the current binary log name and offset on the master: | * read the value of the current binary log name and offset on the master: | ||
+ | |||
<code sql> | <code sql> | ||
mysql > SHOW MASTER STATUS; | mysql > SHOW MASTER STATUS; | ||
Line 41: | Line 80: | ||
+---------------+----------+--------------+------------------+ | +---------------+----------+--------------+------------------+ | ||
</ | </ | ||
+ | |||
* Release the lock | * Release the lock | ||
+ | |||
<code sql> | <code sql> | ||
mysql> UNLOCK TABLES; | mysql> UNLOCK TABLES; | ||
</ | </ | ||
+ | * Here is a backup script you can use (on a daily use, remember to use it on the " | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/sh | ||
+ | |||
+ | BACKUPDIR=/ | ||
+ | LOGFILE=${BACKUPDIR}/ | ||
+ | SENDNSCA=/ | ||
+ | HOSTNAME=`/ | ||
+ | backup_success=1 | ||
+ | |||
+ | mkdir -p ${BACKUPDIR} | ||
+ | rm -f ${LOGFILE}; | ||
+ | |||
+ | for i in `echo 'show databases;' | ||
+ | do | ||
+ | echo -n " ---> backing up ${i}..." | ||
+ | mysqldump --add-drop-table --add-locks --create-options --disable-keys --extended-insert --single-transaction\ | ||
+ | --databases --lock-tables --quick --set-charset $i > ${BACKUPDIR}/ | ||
+ | if [ $? -eq 0 ]; | ||
+ | then | ||
+ | echo " OK" >> ${LOGFILE}; | ||
+ | else | ||
+ | backup_success=0 | ||
+ | fi | ||
+ | done | ||
+ | |||
+ | if [ $backup_success -eq 1 ] | ||
+ | then | ||
+ | echo " | ||
+ | else | ||
+ | echo " | ||
+ | fi | ||
+ | </ | ||
==== slave setup ==== | ==== slave setup ==== | ||
Line 52: | Line 127: | ||
< | < | ||
[mysqld] | [mysqld] | ||
- | server-id=slave_id | + | server-id |
+ | report-host = slave_name | ||
</ | </ | ||
+ | |||
* unpack the snapshot | * unpack the snapshot | ||
* Execute the following statement on the slave | * Execute the following statement on the slave | ||
- | < | + | < |
mysql> CHANGE MASTER TO | mysql> CHANGE MASTER TO | ||
-> | -> | ||
Line 64: | Line 141: | ||
-> | -> | ||
</ | </ | ||
+ | |||
* Execute START SLAVE on the slave. | * Execute START SLAVE on the slave. | ||
+ | |||
+ | |||
+ | ===== optimization ===== | ||
+ | |||
+ | | ||
+ | |||
+ | If MySQLTuner reports fragmented tables, you can check them all with: | ||
+ | |||
+ | <code bash> | ||
+ | mysqlcheck -o --all-databases | ||
+ | </ | ||
===== troubleshooting ===== | ===== troubleshooting ===== | ||
Line 72: | Line 161: | ||
* stop slave | * stop slave | ||
- | < | + | < |
mysql> STOP SLAVE; | mysql> STOP SLAVE; | ||
</ | </ | ||
* either manually delete the duplicate entry, or skip counter by issuing: | * either manually delete the duplicate entry, or skip counter by issuing: | ||
- | < | + | < |
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; | mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; | ||
</ | </ | ||
* start slave | * start slave | ||
- | < | + | < |
mysql> START SLAVE; | mysql> START SLAVE; | ||
</ | </ | ||
==== rebuild database indexes ==== | ==== rebuild database indexes ==== | ||
- | < | + | < |
- | # mysqlcheck -p -r -q -q < | + | # mysqlcheck -p -r -q -q < |
</ | </ | ||
Line 93: | Line 182: | ||
* start MySQL without authentification | * start MySQL without authentification | ||
- | < | + | |
+ | < | ||
# / | # / | ||
</ | </ | ||
+ | |||
* connect to MySQL and change password | * connect to MySQL and change password | ||
- | < | + | |
+ | < | ||
# mysql | # mysql | ||
Welcome to the MySQL monitor. | Welcome to the MySQL monitor. |