分类目录归档:MySQL

xtrabackup 快速增量备份教程

xtrabackup –backup –compress –target-dir=/mysqlbackup/full –user=aaa –host=hostname –password=xxxx –databases=ggggg

xtrabackup –backup –compress –target-dir=/mysqlbackup/incr –incremental-basedir=/mysqlbackup/full –user=aaa –host=hostname –password=xxxx –databases=ggggg

 

–compress 压缩

MySQL UPDATE JOIN

JOIN似乎不太满足这里的需求子查询更好

UPDATE novel 
SET 
    chat_num = (SELECT 
            COUNT(*)
        FROM
            chats
        WHERE
            novel_id = ? AND status = 1)
WHERE
    id = ?

UPDATE JOIN 似乎无法使用limit

UPDATE pay_record
        JOIN
    article_info ON article_info.id = pay_record.object_id 
SET 
    novel_id = special_id
WHERE
    ISNULL(novel_id) AND pay_record.id = 17;

MySQL类型不匹配导致的慢查询

这是个慢查询

SELECT *, `user_id` as uid
FROM (`journals`)
WHERE `user_id` IN ('1456870', 2573141) -- 字符串
AND `status` =  1
ORDER BY `id` desc
LIMIT 20

这个则不是

SELECT *, `user_id` as uid
FROM (`journals`)
WHERE `user_id` IN (1456870, 2573141) -- 数字
AND `status` =  1
ORDER BY `id` desc
LIMIT 20

用php监控从库同步延迟个数

<?php
$mysqli = new mysqli('slave_host', 'root', 'passwd', 'dbname');

/*
 * This is the "official" OO way to do it,
 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
 */
if ($mysqli->connect_error) {
 die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}

/*
 * Use this instead of $connect_error if you need to ensure
 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
 */
if (mysqli_connect_error()) {
 die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}

echo 'Success... ' . $mysqli->host_info . "\n";

/* Create table doesn't return a resultset */
/**
 * 
 * @var mysqli_result $result
 */
$result = $mysqli->query("show slave status");

if ($mysqli->errno) {
 printf("Error number: %s\n", $mysqli->errno);
 printf("Error message: %s\n", $mysqli->error);
}

if ($result !== false) {
 $row = $result->fetch_assoc();
 echo "master : {$row['Master_Log_File']}\n";
 echo "slave relay : {$row['Relay_Master_Log_File']}\n";
 echo " : " . ($row['Read_Master_Log_Pos'] - $row['Exec_Master_Log_Pos']);
}

$mysqli->close();

MYSQL远程直接导入数据库方法

mysqldump -uroot -pxxx -hmaster nimei –single-transaction –default-character-set=utf8mb4 | mysql –default-character-set=utf8mb4 -uroot -pxxx -h127.0.0.1 nimei

注意 –default-character-set 别丢了,否则emoji丢失!

13.4.2.1 CHANGE MASTER TO Syntax

HANGE MASTER TO option [, option] ...

option:
    MASTER_BIND = 'interface_name'
  | MASTER_HOST = 'host_name'
  | MASTER_USER = 'user_name'
  | MASTER_PASSWORD = 'password'
  | MASTER_PORT = port_num
  | MASTER_CONNECT_RETRY = interval
  | MASTER_RETRY_COUNT = count
  | MASTER_DELAY = interval
  | MASTER_HEARTBEAT_PERIOD = interval
  | MASTER_LOG_FILE = 'master_log_name'
  | MASTER_LOG_POS = master_log_pos
  | MASTER_AUTO_POSITION = {0|1}
  | RELAY_LOG_FILE = 'relay_log_name'
  | RELAY_LOG_POS = relay_log_pos
  | MASTER_SSL = {0|1}
  | MASTER_SSL_CA = 'ca_file_name'
  | MASTER_SSL_CAPATH = 'ca_directory_name'
  | MASTER_SSL_CERT = 'cert_file_name'
  | MASTER_SSL_CRL = 'crl_file_name'
  | MASTER_SSL_CRLPATH = 'crl_directory_name'
  | MASTER_SSL_KEY = 'key_file_name'
  | MASTER_SSL_CIPHER = 'cipher_list'
  | MASTER_SSL_VERIFY_SERVER_CERT = {0|1}
  | IGNORE_SERVER_IDS = (server_id_list)

server_id_list:
    [server_id [, server_id] ... ]
STOP SLAVE; -- if replication was running
CHANGE MASTER TO MASTER_PASSWORD='new3cret';
START SLAVE; -- if you want to restart replication

实例:

stop slave;
CHANGE MASTER TO
 MASTER_HOST = 'master',
 MASTER_USER = 'repl',
 MASTER_PASSWORD = 'xxxxxxxx',
 MASTER_PORT = 3306,
 MASTER_LOG_FILE ='mysql-bin.005029',
 MASTER_LOG_POS =0;
set global replicate_do_db = 'doufu';
set global replicate_ignore_db ='mysql,information_schema,performance_schema';
start slave;
show slave status;
show variables like '%Replicate%';

pt-online-schema-change 在线热修改表,不锁表

连接到master上执行

pt-online-schema-change --user=root --password=xxxxx--host=master D=doufu,t=user_comment --execute --alter "ADD COLUMN content_image text NULL COMMENT '' AFTER like_uids" --nocheck-replication-filters

选项说明:
–user=root 用户名
–password=xxxxx 密码
–host=master 主机ip或域名
D=doufu,t=user_comment 数据库名和表明
–execute 不加此参数不会真的执行
–alter “ADD COLUMN content_image text NULL COMMENT ” AFTER like_uids” 需要执行的修改操作
–nocheck-replication-filters 如果有主从结构必须加此参数才能运行, 必须在主库上执行

 

Waiting for query cache lock

原文地址
http://250688049.blog.51cto.com/643101/1560425
Mysql InnoDB 引发 Waiting for query cache lock
标签:Mysql InnoDB Waiting
线上数据库中,如果是InnoDB的话,配备足够的innodb buffer pool后,就把query cache关闭掉(query_cache_size 和 query_cache_type 同时设置为 0),大量的更新+查询时,更容易引发 Waiting for query cache lock。

。。。

— show variables like ‘query_cache%’;
— set global query_cache_type=0;
— set global query_cache_size=0;
— show variables like ‘%slow%’;