通过rails控制台在Model中插入值时出现此错误。
" Mongo ::错误:: NoServerAvailable:没有可用的服务器匹配 preference:#using server_selection_timeout = 30 and local_threshold = 0.015"
两个容器都运行正常,但Rails无法连接mongodb。 我只有一个Dockerfile。
我的docker-compose.yml文件内容是:
<!doctype html>
<html>
<head>
<title>Video upload</title>
</head>
<body>
<?php
function getmimetypes(){
$url='http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types';
$types=array();
array_walk( file( $url ), function( $line, $key, $types ){
if( $line && !empty( $line ) && strpos( $line, chr( 35 ) )===false ){
list( $m, $e )=array_values( array_filter( preg_split( '@\t@', trim( $line ) ) ) );
if( strpos( $e, chr( 32 ) ) ){
$o=explode( chr( 32 ), $e );
foreach( $o as $e ) $types[ $e ]=$m;
} else {
$types[ $e ]=$m;
}
}
}, &$types );
return $types;
}
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES['uploadvideo'], $_POST['upload'] ) ){
/* permitted extensions */
$exts=array( 'mp4', 'mov', 'avi' );
/* permitted mimetypes */
$mimetypes=array( 'video/mp4', 'video/quicktime', 'video/x-msvideo' );
/* Get an array of possible Mimetypes */
$types = getmimetypes();
/* For simplicity, cast as an object */
$obj=(object)$_FILES['uploadvideo'];
$name=$obj->name;
$tmp=$obj->tmp_name;
$size=$obj->size;
$type=$obj->type;
$error=$obj->error;
$cname=str_replace( " ", "_", $name );
/* get file extension */
$ext = pathinfo( $name, PATHINFO_EXTENSION );
/* set the target path */
$target="test_upload/{$cname}";
/* process the uploaded file */
if( is_uploaded_file( $tmp ) && $error == UPLOAD_ERR_OK ){
/*
additional checks on file - is it of the correct extension and mimetype?
*/
if( in_array( $ext, $exts ) && in_array( $type, $mimetypes ) ){
$result = @move_uploaded_file( $tmp, $target );
$sql='insert into `video` ( `name`,`type` ) values ("'.$cname.'","'.$type.'");';
if( $result ){
$result = mysql_query( $sql );
echo $result ? 'Your video '.$cname.' has been successfully uploaded and saved' : 'There was a problem saving your video';
} else {
echo 'Unable to move your video to it\'s new location';
}
}
} else {
echo 'Error: Possible attack';
}
}
?>
<form enctype='multipart/form-data' method='post'>
<input name='MAX_FILE_SIZE' value='100000000000000' type='hidden' />
<input type='file' name='uploadvideo' />
<input type='submit' name='upload' value='Upload' />
</form>
</body>
</html>
我的Dockerfile:
version: '2'
services:
mongo:
image: mongo:3.0
command: mongod --smallfiles --quiet
environment:
- RAILS_ENV=production
- RACK_ENV=production
ports:
- "27017:27017"
app:
depends_on:
- 'mongo'
# - 'redis'
build: .
ports:
- '3000:3000'
volumes:
- '.:/app'
command: rails s -b '0.0.0.0'
env_file:
- '.env'
volumes:
mongo:
答案 0 :(得分:0)
您是否使用mongo(与docker-compose.yml中提到的容器名称相同)作为mongoid.yml中的主机?