打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
mysql 标签

存档

文章标签 ‘mysql’

mysql too many connections

2014年1月11日 评论已被关闭

连接mysql时报too many connections错误,是因为打开连接太多导致。
通过show processlist你会发现有很多sleep连接,这是因为使用了长连接(mysql_pconnect)或者短连接(mysql_connect)未正常关闭导致的。
可以修改mysqld的最大连接数,同时设置一个短的超时时间,让短连接尽快的自动关闭即可解决次问题:

修改/etc/my.cnf,添加下面内容,并重启mysqld服务即可:

1
2
3
max_connections = 500 #由默认的150改成500个连接
wait_timeout=600 #由默认的2880改为600秒
interactive_timeout = 600 #由默认的2880改为600秒
分类: 数据库 标签:

Linux下LAMP服务selinux权限问题

2013年9月17日 评论已被关闭

我们在开启了selinx的linux系统中,即使正确的设置了文件系统权限,常常还是会出现权限问题,这是我们要思考是不是selinx引起的了。

1、mysql:

selinux打开的前提下,修改/etc/my.cnf中mysqld数据路径后启动Mysql服务出错,查看/var/log/mysql.log提示:

1
2
110806 16:32:16 [Warning] Can't create test file /storage/db/mysql/mysqllocalhost.lower-test
/usr/sbin/mysqld: Can't change dir to '/storage/db/mysql/' (Errcode: 13)

给目录设置selinux权限:

1
2
chcon -R -t mysqld_db_t /storage/db/mysql/
ls -ldZ /storage/db/mysql/

再次启动mysql服务提示:

1
Can't open and lock privilege tables: Table 'mysql.host' doesn't exist

执行mysql_install_db创建mysql数据库,然后启动mysql成功:

1
mysql_install_db --datadir /storage/db/mysql/
2、httpd站点:

在selinux打开的前提下,运行PHP代码在/var/log/http/下错误日志中发现如下错误:

1
2
PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
[Tue Sep 17 16:03:45 2013] [error] [client 106.2.169.130] PHP Fatal error:  Unknown: Failed opening required '/storage/web/pzycx/index.php' (include_path='.:/usr/share/pear:/usr/share/php') in Unknown on line 0

配置站点目录的selinux权限即可:

1
chcon -R -h -t httpd_sys_content_t /storage/web/
3、关闭selinux:

修改/etc/sysconfig/selinux文件,将SELINUX=enforcing改成SELINUX=disabled并重启服务器,即能永久关闭selinux.

分类: Linux 标签: , , , , ,

连接Mysql提示Can’t connect to local MySQL server through socket的解决方法

2013年9月6日 1 条评论

mysql,Mysqldump,Mysqladmin,php连接mysql服务常会提示下面错误:

1
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

这是由于修改mysql服务的了socket文件mysql.sock位置,而导致无法通过mysql socket文件连接到mysql服务引起的,具体解决办法如下:
1、查看mysql服务的socket文件位置:
mysql socket文件的位置是在/etc/my.cnf中设置的,cat /etc/my.cnf内容如下:

1
2
3
4
[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock
user=mysql

其中socket等于的路径就是socket文件的位置,我们只要修改my.cnf文件,告诉mysql,mysqldump,mysqladmin mysql服务的socket位置在哪里就可以。

2、修改my.cnf文件:
在/etc/my.cnf文件中添加如下内容,并重启mysqls服务,即可解决mysql,mysqldump,mysqladmin的“Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'”问题:

1
2
3
4
5
6
7
8
9
10
11
12
[mysqld]
datadir=/storage/db/mysql
socket=/storage/db/mysql/mysql.sock
[mysql]
socket=/storage/db/mysql/mysql.sock
[mysqldump]
socket=/storage/db/mysql/mysql.sock
[mysqladmin]
socket=/storage/db/mysql/mysql.sock

3、php连接mysql服务提示"Can't connect to local MySQL server through socket..."的解决方法
有时候mysql服务正常运行,用户名密码也完全正确,使用php的mysql_connect函数却连接不了mysql,调用php的mysql_error()函数提示“Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'”,这是我们需要修改/etc/php.ini文件。
在/etc/php.ini文件中"[MySQL]"项下找到"mysql.default_socket",并设置其值指向正确的mysql服务socket文件即可,如:

1
2
3
[MySQL]
...省略n...
mysql.default_socket = "/storage/db/mysql/mysql.sock"

4、python连接mysql提示"Can't connect to local MySQL server through socket..."的解决方法:
在连接mysql数据库函数中指定socket文件,如下:

1
2
3
4
5
6
7
8
#!/usr/bin/python
from MySQLdb import connect
conn = connect(db="pzy", user="root", host="localhost", unix_socket="/storage/db/mysql/mysql.sock")
cur = conn.cursor()
count=cur.execute("show databases")
print 'there has %s dbs' % count
conn.commit()
conn.close()

5. php pdo连接mysql提示"Can't connect to local MySQL server through socket..."的解决方法:
同样在连接字符串添加mysql socket文件的位置即可,如下:

1
2
3
4
5
6
7
8
<?php
$dsn = "mysql:host=localhost;dbname=pzy;unix_socket=/storage/db/mysql/mysql.sock";
$db = new PDO($dsn, 'root', '');
$rs = $db->query("SELECT * FROM qrtest");
while($row = $rs->fetch()){
    print_r($row);
}
?>
分类: 数据库 标签: , ,

Mysql计算两GPS坐标的距离

2013年8月24日 没有评论

mysql计算两GPS坐标的距离函数:

PHP计算两个GPS点之间的距离
Mysql计算两GPS坐标的距离
javascript计算两个GPS点之间的距离

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
drop function getDistance;
DELIMITER $$  
CREATE DEFINER=`root`@`localhost` FUNCTION `getDistance`(
     lon1 float(10,7)
    ,lat1 float(10,7)
    ,lon2 float(10,7)
    ,lat2 float(10,7)
) RETURNS double
begin
    declare d double;
    declare radius int;
    set radius = 6378140; #假设地球为正球形,直径为6378140米
    set d = (2*ATAN2(SQRT(SIN((lat1-lat2)*PI()/180/2)  
        *SIN((lat1-lat2)*PI()/180/2)+  
        COS(lat2*PI()/180)*COS(lat1*PI()/180)  
        *SIN((lon1-lon2)*PI()/180/2)  
        *SIN((lon1-lon2)*PI()/180/2)),  
        SQRT(1-SIN((lat1-lat2)*PI()/180/2)  
        *SIN((lat1-lat2)*PI()/180/2)  
        +COS(lat2*PI()/180)*COS(lat1*PI()/180)  
        *SIN((lon1-lon2)*PI()/180/2)  
        *SIN((lon1-lon2)*PI()/180/2))))*radius;
    return d;
end
$$
DELIMITER ;
select getDistance(116.3899,39.91578,116.3904,39.91576); #调用函数

Mysql计算两GPS坐标的距离SQL语句:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#lat为纬度, lng为经度, 一定不要弄错
declare @lon1 float;
declare @lat1 float;
declare @lon2 float;
declare @lat2 float;
set @lon1=116.3899;
set @lat1=39.91578;
set @lon2=116.3904;
set @lat2=39.91576;  
select (2*ATAN2(SQRT(SIN((@lat1-@lat2)*PI()/180/2)  
        *SIN((@lat1-@lat2)*PI()/180/2)+  
        COS(@lat2*PI()/180)*COS(@lat1*PI()/180)  
        *SIN((@lon1-@lon2)*PI()/180/2)  
        *SIN((@lon1-@lon2)*PI()/180/2)),  
        SQRT(1-SIN((@lat1-@lat2)*PI()/180/2)  
        *SIN((@lat1-@lat2)*PI()/180/2)  
        +COS(@lat2*PI()/180)*COS(@lat1*PI()/180)  
        *SIN((@lon1-@lon2)*PI()/180/2)  
        *SIN((@lon1-@lon2)*PI()/180/2))))*6378140;

更多关于GPS计算请参考:http://www.movable-type.co.uk/scripts/latlong.html

错误及解决方法:

在创建函数前报如下错误:

1
This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)

解决方法:

在创建函数前执行下面SQL语句:

1
set global log_bin_trust_function_creators=1;

或者修改my.cnf文件并重启mysqld服务:

1
log_bin_trust_function_creators=1
分类: 数据库 标签: ,

unix时间戳(unix timestamp)与北京时间的互转方法

2013年7月1日 没有评论

unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp),是从1970年1月1日(UTC/GMT的午夜)开始到某一时刻所经过的秒数,不考虑闰秒。

时间戳0按照ISO 8601格式写出来为 1970-01-01T00:00:00Z 。一小时为3600秒,一个基准日(也称纪元日,epoch day)是86400秒,闰秒没有计算在内。

转换方法: bash mysql php javascript java java python java perl sqlserver Vbs/Asp ruby

1、在linux bash下北京时间与unix时间戳互转:

获取unix timestamp:

  1. 命令:date "+%s"
  2. 输出:1372654714

获取北京时间:

  1. 命令:date '+%Y-%m-%d %H:%M:%S'
  2. 输出:2013-07-01 12:55:56

unix timestamp转北京时间:

  1. 命令:date -d@1372654714 '+%Y-%m-%d %H:%M:%S'
  2. 输出:2013-07-01 12:58:34

北京时间转unix timestamp:

  1. 命令:date -d"2013-07-01 12:58:34" '+%s'
  2. 输出:1372654714

阅读全文...

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
MySQL错误Another MySQL daemon already running with the same unix socket.
如何为实时性应用存取经纬度?
Web墨卡托坐标与WGS84经纬度互转 java代码
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket 解决办法
mysql 通过2个经纬度 计算距离
LBS应用中的地理距离搜索
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服