1. Sql
1.1.1 插入insert的使用
1.insert ignore into
當插入資料時,如出現錯誤時,如重複資料,将不傳回錯誤,隻以警告形式傳回。是以使用ignore請確定語句本身沒有問題,否則也會被忽略掉。例如:
INSERT IGNORE INTO books (name) VALUES ('MySQL Manual')
2.on duplicate key update
當primary或者unique重複時,則執行update語句,如update後為無用語句,如id=id,則同1功能相同,但錯誤不會被忽略掉。例如,為了實作name重複的資料插入不報錯,可使用一下語句:
INSERT INTO books (name) VALUES ('MySQL Manual') ON duplicate KEY UPDATE id = id
3.insert … select … where not exist
根據select的條件判斷是否插入,可以不光通過primary 和unique來判斷,也可通過其它條件。例如:
INSERT INTO books (name) SELECT 'MySQL Manual' FROM dual WHERE NOT EXISTS (SELECT id FROM books WHERE id = 1)
4.replace into
如果存在primary or unique相同的記錄,則先删除掉。再插入新記錄。
REPLACE INTO books SELECT 1, 'MySQL Manual' FROM books
1.1.2 grant使用
grant select on testdb.* to [email protected] identified by '123456!';
賦予使用者teamuser從伺服器10.161.204.81上查詢資料庫testdb所有表的權限;注意賦予内網時,登入也得用内網ip,mysql -h10.161.204.81 -uteamuser -p'123456!'
所有權限用all,即用all代替select
導出資料:
mysql -uroot -p'123456!' testdb -e "select id,name from testarticle where id=\"123456\"" > /home/app/a.txt
導出insert語句
mysqldump -uroot -p'123456!' testdb categorys --default-character-set=utf8 > /home/b.txt
二. 資料庫維護
2.1故障處理
2.1.1主從不同步的處理-1062
1)檢視slave狀态:show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.100.22.15
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000362
Read_Master_Log_Pos: 175989792
Relay_Log_File: relay-bin.000540
Relay_Log_Pos: 258368116
Relay_Master_Log_File: mysql-bin.000361
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1062
Last_Error: Error 'Duplicate entry '1342170' for key 'PRIMARY'' on query. Default database: 'usercenter'. Query: 'insert into test_charge ……
Skip_Counter: 0
Exec_Master_Log_Pos: 673522500
Relay_Log_Space: 836682055
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1062
Last_SQL_Error: Error 'Duplicate entry '1342170' for key 'PRIMARY'' on query. Default database: 'usercenter'. Query: 'insert into test_charge ……
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
分析:

正常情況Seconds_Behind_Master應該為0
解決方法:
A)這種适用于該方法适用于主從庫資料相差不大,或者要求資料可以不完全統一的情況,資料要求不嚴格的情況,具體指令:
slave stop;
set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
slave start;
B)由此表可知 MySQL Server System Variables, slave_skip_errors 是無法被動态改變的.
要改變 slave_skip_errors 的方法就隻有在 MySQL 啟動時加:
--slave_skip_errors=all
或者于 /etc/my.cnf 加入:
slave-skip-errors=all # 于 [mysqld] 下面加入此行
再 restart mysql 即可
确認是否忽略所有錯誤
mysql> show variables like "%slave%";
slave_skip_errors => ALL # 看到 ALL 就代表會忽略掉所有錯誤訊息
忽略某個錯誤編号
若隻要排除 Duplicate entry (Last_Errno: 1062), 可以單獨指定 Error No, 多個寫法如下:
--slave-skip-errors=1062,1053
--slave-skip-errors=all
詳細可見: Replication Slave Options and Variables
于 my.cnf 設定忽略
vim /etc/my.cnf
slave-skip-errors=1062 # 于 [mysqld] 下面加入此行
2)擴充:
2.1.2主從不同步的處理,errcode:1236
1)檢視slave狀态:show slave status\G;
Last_IO_Errno: 1236
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'
A)進入mysql server,即mysql -uusername -ppassword
在mysql 指令行中運作如下:
set global max_allowed_packet = 2*1024*1024*10
退出mysql指令行,然後重新登入。
show VARIABLES like '%max_allowed_packet%';
檢視下max_allowed_packet是否編輯成功
B)可以編輯my.cnf來修改(windows下my.ini),在[mysqld]段或者mysql的server配置段進行修改。
max_allowed_packet = 20M
如果找不到my.cnf可以通過
mysql --help | grep my.cnf
去尋找my.cnf檔案。
有了配置檔案,在配置檔案中的[mysqld]下邊加些常用的配置參數。重新開機mysql伺服器後,該參數即可生效。
2.2 主從設定
2.1.2 從指定位置同步
1)有的時候主從同步有問題了以後,需要從log位置的下一個位置進行同步,相當于跳過那個錯誤,這時候也可以使用CHANGE MASTER指令來處理,隻要找到對應的LOG位置就可以,比如:
CHANGE MASTER TO
MASTER_HOST='10.1.1.75',MASTER_USER='replication',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=106;
2.3 備份
2.3.1 邏輯備份
1)備份:
mysqldump -hdb.test.com -uusername -ppassword testdb table1 table2 table3 | gzip > /home/mysql_bak/bakup-`date +%Y-%m-%d`.sql.gz
2)恢複:
gunzip < /home/mysql_bak/bakup-2015-03-14.sql.gz | mysql -uusername -ppassword testdb
2.4 啟動和停止伺服器
2.4.1 啟動mysql
1)配置my.cnf檔案,然後啟動:
./bin/mysqld_safe --user=mysql &
2.4.2 停止mysql
$mysql_dir/bin/mysqladmin -uuser -ppassword shutdown
或./bin/mysqladmin -S /tmp/mysql.sock -uroot -pq1234 shut
2.5優化處理
2.6 常用指令使用
2.6.1 常用指令
1.show processlist;
解釋:顯示系統中正在運作的所有程序,也就是目前正在執行的查詢。大多數使用者可以檢視
他們自己的程序,但是如果他們擁有process權限,就可以檢視所有人的程序,包括密碼。
2.telnet 123.124.92.80 3306 檢視是否有權限
3.檢視建立表語句:
show create table tablename;
例如:
要檢視jos_modules表結構的指令: