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失败的问题解决方案的相关知识就介绍到这里了,希望本次的介绍能够对您有所收获!
【编辑推荐】