mysql運維之---每日一得01

2015年7月1日-------------------

1、MHA修復宕機的機器
首先cat /var/log/manager.log|grep -i "All other slaves should start"確定change master命令,把宕掉的數據庫給啓動,登陸進去後,slave status爲空,使用change master命令設置應用的主節點,啓動slave進程
然後設置read_only=1,最後檢查複製環境,必須啓動mha manager的監控(ps aux|grep perl)並查看狀態,刪除app1.failover.complete,並把# mysql -e "set global relay_log_purge=0"
2、主從複製中,使用alter event把事件enable,不會影響從庫的事件狀態SLAVESIDE_DISABLED,進行切換後,現在的主庫事件狀態SLAVESIDE_DISABLED,需要手動進行enable,可以使用如下方式:
select concat('alter event ',EVENT_SCHEMA,'.',EVENT_NAME,' disable;') from information_schema.events;

2015年7月2日------------------

表結構:

CREATE TABLE `question_2` (
  `qid` int(11) NOT NULL DEFAULT '0',
  `QuestionID` varchar(50) NOT NULL COMMENT '只做數據冗餘,不做查詢條件,不添加索引',
  `UserID` int(11) DEFAULT NULL,
  `QuestionTitle` varchar(500) NOT NULL,
  `Age` int(11) NOT NULL,
  `Month` int(11) NOT NULL,
  `CatalogID` int(11) NOT NULL,
  `Sex` int(11) NOT NULL,
  `QuestionDesc` longtext NOT NULL,
  `QuestionTag` varchar(400) DEFAULT NULL,
  `Score` int(11) DEFAULT NULL,
  `Anonym` int(11) DEFAULT '0',
  `CommentCount` int(11) NOT NULL DEFAULT '0',
  `Source` int(11) DEFAULT NULL,
  `IsAutoAdd` int(11) DEFAULT '0',
  `QuestionStatus` int(11) DEFAULT NULL,
  `OperateStatus` int(11) DEFAULT '0',
  `OperateTime` datetime DEFAULT NULL,
  `CreateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '展示時間',
  `UpdateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`qid`),
  KEY `idx_2_uid_ctime_qstatus` (`UserID`,`CreateTime`,`QuestionStatus`,`OperateStatus`),
  KEY `idx_2_qstatus_opstatus_sc_so` (`QuestionStatus`,`OperateStatus`,`Age`,`Score`,`Source`),
  KEY `idx_2_ctime_qstatus_opstatus` (`CreateTime`,`QuestionStatus`,`OperateStatus`,`CatalogID`,`Age`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

select count(*) from question_2;-- 4086112
explain select * from `question_2` where `questionstatus` >= 0 and `operatestatus` =2 and `age` in ('1','2') order by qid desc limit 60000,20;
explain select qid from `question_2` where `questionstatus` >= 0 and `operatestatus` =2 and `age` in ('1','2') order by qid desc limit 60000,20;
select count(*) from `question_2` where `questionstatus` >= 0;-- 4064825/4086112
select count(*) from `question_2` where `questionstatus` >= 0 and `operatestatus` =2;-- 3649271/4086112
---------------優化後的sql
explain select * from question_2 inner join 
(select qid from `question_2` where `questionstatus` >= 0 and `operatestatus` =2 and `age` in ('1','2') order by qid desc limit 60000,20) a using (qid);

2015年7月10日------------

3、先給root@localhost創建密碼,然後刪除mysql.user password=''的用戶,記得保留一個本地管理員的用戶
mysql> set autocommit=0;
mysql> select * from mysql.user where password is null or password='';
mysql> delete from mysql.user where password is null or password='';
mysql> select host,user,password from mysql.user where password is null or password='';
mysql> commit;
mysql> flush privileges;  ##立刻刷新權限表
4、max_connect_errors:與主機中斷的數目超過這個數值,該主機會阻塞之後的連接,可以使用flush hosts命令解鎖鎖定的主機
5、mysqldump某個庫,備份失敗
(1)、請檢查backup權限是否足夠
(2)、是否導出不完整那個庫的視圖、存儲過程、事件等是否訪問了一些不存在的表或字段
mysql> create database testdump;
mysql> GRANT ALL PRIVILEGES ON testdump.* TO 'testdump'@'192.168.0.%' IDENTIFIED BY '123456';
mysql> flush privileges;
mysql -h 192.168.0.3 -utestdump -p ##在192.168.0.4上進行連接,通過192.168.0.4出去
mysql> select user();
+----------------------+
| user()               |
+----------------------+
| [email protected] |
+----------------------+
delimiter $$
CREATE PROCEDURE `test_continue_proc`(IN `v_session_id` int)
BEGIN
declare v_incre int default 0;
while v_incre < 100000 do
insert into test.testincre values(v_incre,concat('yangsq',v_incre,v_session_id));
set v_incre=v_incre+1;
select sleep(10);
end while;
END
$$
delimiter ;
6、日誌相關的參數
general-log:查詢日誌,記錄客戶端的所有語句。
slow-query-log/long-query-time/slow-query-log-file
log-error/log-bin(默認是關閉的)/binlog-format/binlog_cache_size

binlog日誌的刪除-----
show master logs;
show slave hosts;
(1)reset master:刪除所有的binlog日誌,新日誌編號從000001開始 例如:在從庫執行reset master後,查看show slave status\G沒有影響
(2)purge master logs to 'mysql-bin.xxxxxx':刪除xxxxxx編號之前的所有日誌
system ls -ltr mysql-bin.*
(3)purge master logs before 'yyyy-mm-dd hh24:mi:ss':刪除指定日期之前產生的所有日誌
(4)--expire-logs-days:過了指定的天數後,binlog會被自動刪除
mysql> reset slave;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
/*
mysql> start slave; 然後
2015-07-09 18:29:21 18071 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000012', position 8344
2015-07-09 18:29:46 18071 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2015-07-09 18:29:46 18071 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2015-07-09 18:29:46 18071 [Note] Slave SQL thread initialized, starting replication in log 'FIRST' at position 0, relay log './yaolansvr_slave01-relay-bin.000001' position: 4
2015-07-09 18:29:46 18071 [Note] Slave I/O thread: connected to master '[email protected]:3306',replication started in log 'FIRST' at position 4
2015-07-09 18:29:51 18071 [ERROR] Slave SQL: Error 'Duplicate entry '10122588' for key 'PRIMARY'' on query. Default database: 'stat'. Query: 'INSERT INTO `stat`.`seo_comment_src` (`AutoIncreCID`, `CommentID`, `AutoIncreQID`, `QuestionID`, `ExpertID`, `CommentContent`, `UserID`, `UserName`, `IsBestComment`, `CDate`, `InsertDate`, `InsertStatus`, `pkid_incaiji`, `id`) VALUES (NULL, '1503041036297b89d2c4', NULL, '15030409501268517b3a', NULL, '你好,根據你描述的情況,一般正常閉合是在1.5歲左右的,你寶寶這個可能是血鈣偏高的問題。', '1970798', 'xu978322', '0', '2015-02-26 20:23:41', '2015-03-04 10:36:29', '0', '390410', '10122588')', Error_code: 1062
2015-07-09 18:29:51 18071 [Warning] Slave: Duplicate entry '10122588' for key 'PRIMARY' Error_code: 1062
2015-07-09 18:29:51 18071 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000001' position 13372
2015-07-09 18:30:20 18071 [ERROR] Error reading packet from server: Lost connection to MySQL server during query ( server_errno=2013)
2015-07-09 18:30:20 18071 [Note] Slave I/O thread killed while reading event
2015-07-09 18:30:20 18071 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000012', position 8344
*/
7、主庫使用statement binlog format,主庫執行load data infile語句會在/tmp產生SQL_LOAD_MB-*文件,從庫也是執行load data infile語句,並把主庫/tmp的SQL_LOAD_MB-*文件拷貝到從庫的/tmp下
如果導入的文件超過1G,則從庫報錯:Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'log event entry exceeded max_allowed_packet; Increase max_allowed_packet on master'

max_allowed_packet-The protocol limit for max_allowed_packet is 1GB
slave_max_allowed_packet-The default and maximum value is 1073741824 (1 GB)
select * from testinfile into outfile 'testinfile20150710' fields terminated by ',';
load data infile 'testinfile20150710' into table test.testinfile fields terminated by ',';

2015年7月15日-------------

8、某些常用命令
? contents/? auto_increment
刪除test1庫下所有以tmp爲前綴的表:select concat('drop table test.',table_name,';') from information_schema.tables t1 where t1.table_schema='test' and table_name like 'test%';
# mysql -e "select concat('drop table test.',table_name,';') from information_schema.tables t1 where t1.table_schema='test' and table_name like 'test%';"|grep -Evi "&&|$$"
9、mysql客戶端程序使用system clear命令可以清屏;sql_mode的值其實都是一些原子模式的組合,類似於角色和權限的關係
10、##查看所有庫的不同引擎的數據大小、索引大小
select  engine,  
round(sum(data_length) /1024/1024, 1) as "data mb",  
round(sum(index_length)/1024/1024, 1) as "index mb",  
round(sum(data_length + index_length)/1024/1024, 1) as "total mb",  
count(*) "num tables"  
from  information_schema.tables  
where  table_schema not in ("information_schema", "performance_schema")  
group by  engine;
11、##查看某個庫下所有表的數據表大小、索引大小
select table_schema,table_name,round(data_length/1024/1024,1) as "data  mb",round(index_length/1024/1024,1) as "index mb",round((data_length+index_length)/1024/1024,1) as "total mb" from information_schema.tables where table_schema='yaolanbbs2015';

2015年8月5日-------------

12、監控myisam的索引緩存使用情況
key_buffer_size:索引緩存大小,是對MyISAM表性能影響最大的一個參數索引命中緩存率:
key_buffer_read_hits=(1-Key_reads/Key_read_requests)*100% 
key_buffer_write_hits=(1-Key_writes/Key_write_requests)*100%
mysql> show status like 'key_read%';
+-------------------+------------+
| Variable_name     | Value      |
+-------------------+------------+
| Key_read_requests | 1337113881 |
| Key_reads         | 957612     |
+-------------------+------------+
2 rows in set (0.00 sec)
mysql> show status like 'key_blocks%';
+------------------------+----------+
| Variable_name          | Value    |
+------------------------+----------+
| Key_blocks_not_flushed | 0        |
| Key_blocks_unused      | 16686929 |
| Key_blocks_used        | 1155344  |
+------------------------+----------+
3 rows in set (0.00 sec)
mysql> select 957612/1337113881;
+-------------------+
| 957612/1337113881 |
+-------------------+
|            0.0007 |
+-------------------+
1 row in set (0.00 sec)
mysql> select @@key_buffer_size;
+-------------------+
| @@key_buffer_size |
+-------------------+
|       22548578304 |
+-------------------+
1 row in set (0.00 sec)
mysql> select 1155344*1024/22548578304;
+--------------------------+
| 1155344*1024/22548578304 |
+--------------------------+
|                   0.0525 |
+--------------------------+
13、Innodb_buffer_pool_wait_free
一般情況,通過後臺向InnoDB緩衝池寫。但是,如果需要讀或創建頁,並且沒有乾淨的頁可用,則它還需要先等待頁面清空。該計數器對等待實例進行記數。如果已經適當設置緩衝池大小,該值應小。
Innodb_buffer_pool_pages_data:包含數據的頁數(髒或乾淨)。
Innodb_buffer_pool_pages_free:空頁數。
Innodb_data_read:至此已經讀取的數據數量(字節)。
Innodb_data_reads:數據讀總數量。
Innodb_data_writes:數據寫總數量。
Innodb_data_written:至此已經寫入的數據量(字節)。
innodb buffer pool命中百分比:Innodb_buffer_pool_read_requests / (Innodb_buffer_pool_read_requests + Innodb_buffer_pool_reads) * 100 = InnoDB Buffer Pool hit ratio
show status like 'innodb_buffer_pool_%'與select @@innodb_page_size;


2015年8月20日-----

12、query_cache_size調整
query_cache有一個致命的缺陷,當某個表的數據有任何的變化,會導致所有引用了該表的select語句在query cache中的緩存數據失效。所以,當數據變化非常頻繁的情況下,使用query cahche可能會得不償失。
mysql> show status;
mysql> show variables like '%query_cache%';
+------------------------------+----------+
| Variable_name                | Value    |
+------------------------------+----------+
| have_query_cache             | YES      |
| query_cache_limit            | 2097152  |
| query_cache_min_res_unit     | 4096     |
| query_cache_size             | 67108864 |
| query_cache_type             | ON       |
| query_cache_wlock_invalidate | OFF      |
+------------------------------+----------+
mysql> show status like 'qcache_%';
mysql> select @@query_cache_type;
select sql_no_cache * from pre_data_post where id=527352;

13、select current_user();
SET PASSWORD [FOR user] =
    {
        PASSWORD('cleartext password')
      | OLD_PASSWORD('cleartext password')
      | 'encrypted password'
    }
mysql> grant select,insert,update,delete on test.* to testpriv@'192.168.0.%' identified by '123456';
mysql> set password for testpriv@'192.168.0.%'=password('yangsq123');

14、mysql> set global log_output='file,table';
Query OK, 0 rows affected (0.02 sec)

mysql> show variables like 'log_output';
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| log_output    | FILE,TABLE |
+---------------+------------+

mysql> select @@long_query_time;
+-------------------+
| @@long_query_time |
+-------------------+
|          5.000000 |
+-------------------+
1 row in set (0.00 sec)

mysql> set global long_query_time=0.01;
mysql> select sql_no_cache * from sakila.payment t1,sakila.customer t2 where t1.customer_id=t2.customer_id;
mysql> select * from mysql.slow_log where start_time between curdate()-1 and curdate()+1 order by query_time desc;
+---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------------------------------------------------------------------------------------------+-----------+
| start_time          | user_host                 | query_time | lock_time | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text                                                                                            | thread_id |
+---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------------------------------------------------------------------------------------------+-----------+
| 2015-07-24 15:59:52 | root[root] @ localhost [] | 00:00:00   | 00:00:00  |         0 |             0 |    |              0 |         0 |     16803 | truncate table mysql.slow_log                                                                       |         5 |
| 2015-07-24 16:00:05 | root[root] @ localhost [] | 00:00:00   | 00:00:00  |     16049 |         16648 |    |              0 |         0 |     16803 | select sql_no_cache * from sakila.payment t1,sakila.customer t2 where t1.customer_id=t2.customer_id |         5 |
+---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------------------------------------------------------------------------------------------+-----------+

15、MyISAM表索引和數據是分開的,OPTIMIZE TABLE可以整理數據文件,並且可以對索引樹進行排序以便更快地查找鍵值。alter table xxx engine=innodb它會起到優化作用,它會創建整個表,刪掉空閒空間。

16、USAGE權限,可以創建賬戶而不授予任何權限,只能夠登錄,除了內置的test庫,對其他庫沒有任何權限,show databases只能列出information_schema/test。
usage權限不能被回收,也即REVOKE用戶並不能刪除用戶。
mysql> show grants for root@'127.0.0.1';
+---------------------------------------------------------------------+
| Grants for [email protected]                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION |
+---------------------------------------------------------------------+
mysql> revoke all privileges,grant option from root@'127.0.0.1';
mysql> show grants for root@'127.0.0.1';
+------------------------------------------+
| Grants for [email protected]                |
+------------------------------------------+
| GRANT USAGE ON *.* TO 'root'@'127.0.0.1' |
+------------------------------------------+

17、對錶做優化是一個非常危險的操作,千萬不要修復表時,執行中斷操作(網絡、ctrl+c),例如mysiam表中斷優化,is marked as crashed and last (automatic?) repair failed
解決:不停主庫,將從庫optimize的後的表物理拷貝到主庫,覆蓋主庫數據文件
mysql> show status like 'slow_queries';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries  | 0     |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show status like 'long_query_time';

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章