Linux cron執行MySQL失敗的實例的問題解決方案是本文我們主要要介紹的,接下來就讓我們一起來了解一下這部分內容吧。
1.首先,為了統計服務器平均在線人數,於是寫了個SHELL腳本,每天7點-0點之間每小時執行該命令,獲得當前在線用戶,以便統計最高在線用戶,以及平均在線用戶.
SHELL腳本如下:
- time08_23.sh
- -----------------------------------------------------------------------------------
- #!/bin/bash
- # crontab -e 57 7-22 * * * /home/nemo/crontab/time08_23.bash
- DB_USER="root"
- DB_PASS="***"
- DB_HOST="localhost"
- DB_PORT="3306"
- # Others vars
- BIN_DIR="/usr/bin"
- # TODO
- # 獲得8080端口
- THETIME=`netstat -ant | grep 8080 | wc -l`
- # 獲得當前小時
- THEHOUR=`date +%H`
- # 更新當前小時的現在人數
- $BIN_DIR/mysql -u$DB_USER -p$DB_PASS -h$DB_HOST -P$DB_PORT << EOF
- use wapgame4sky2;
- UPDATE MgrSky SET time$THEHOUR = $THETIME WHERE TO_DAYS(date) = TO_DAYS(now());
- EOF
2.以上的SHELL腳本,在本地以及遠程服務器上,通過 source time08_23.sh 以及 ./time08_23.sh 都能正確運行,數據無誤,一切正常。
3.但以上腳本使用cron任務調度器執行數據庫卻沒有數據,這樣我一度認為是任務調度器配置錯誤,或者權限問題,但查詢錯誤日志獲得以下信息:
- Character set 'ascii' is not a compiled character set and is not specified in the '/usr/local/share/mysql/charsets/Index.xml'
- <img src='http://hi.csdn.net/attachment/201108/25/0_1314264038vkzR.gif' />
4.分析一下便清楚是編碼格式的問題.於是逐步查詢問題所在.
- show variables like '%char%';
- +--------------------------+----------------------------+
- | Variable_name | Value |
- +--------------------------+----------------------------+
- | character_set_client | utf8 |
- | character_set_connection | utf8 |
- | character_set_database | utf8 |
- | character_set_filesystem | binary |
- | character_set_results | utf8 |
- | character_set_server | latin1 |
- | character_set_system | utf8 |
- | character_sets_dir | /opt/mysql/share/charsets/ |
- +--------------------------+----------------------------+
查看本地mysql編碼集設置:
- +--------------------------+----------------------------+
- | Variable_name | Value |
- +--------------------------+----------------------------+
- | character_set_client | latin1 |
- | character_set_connection | latin1 |
- | character_set_database | latin1 |
- | character_set_filesystem | binary |
- | character_set_results | latin1 |
- | character_set_server | latin1 |
- | character_set_system | utf8 |
- | character_sets_dir | /usr/share/mysql/charsets/ |
- +--------------------------+----------------------------+
然後,查詢mysql官網,對於該問題的解決辦法如下:http://dev.mysql.com/doc/refman/5.0/en/charset-configuration.html
- [client]
- character-sets-dir=/usr/local/mysql/share/mysql/charsets
- [client]
- character-sets-dir="C:/Program Files/MySQL/MySQL Server 5.0/share/charsets"
- Character set 'charset_name' is not a compiled character set and is not
- specified in the '/usr/share/mysql/charsets/Index.xml' file
- [client]
- default-character-set=charset_name
問題應該算是明確了.按照我的理解,linux cron 在編譯shell腳本的時候,應該采用的是latin1字符集.而這裡卻使用UTF-8,導致編碼不支持.遂聯系那邊運維,取得了my.cnf,以及mysql的root權限.
5.修改vi /etc/my.cnf,在[client]下添加,default-character-set=latin1,在[mysqld]下添加,default-character-set=latin1,重啟mysql。
開啟mysql命令:
- /usr/local/mysql/bin
- ./mysqld_safe &
- 關閉mysql服務的命令是:
- ./mysqladmin -u root -p shutdown
按照以上的步驟操作,問題就解決了。
關於Linux cron執行MySQL失敗的問題解決方案的相關知識就介紹到這裡了,希望本次的介紹能夠對您有所收獲!