I am able to connect to redis cluster in elasticache with ec2 instance (given in aws documentation) and be able to add and get keys, values. But when i try to connect through phpredis on same ec2 instance, I'm getting no error and also no data. Please Help me with this. There's not much info on the internet for this specific problem. I am able to connect to redis running on the same ec2 instance but not to elasticache. If i could get some example on how to except for changing the host (endpoint of redis cluster).
Thanks
Use Predis library.
Connect to Redis ElastiCache Endpoint in Cluster mode using Predis, see below example.
try{
// Put your AWS ElastiCache Configuration Endpoint here.
$servers = ['aliceredis.8xyzwu.clustercfg.euw2.cache.amazonaws.com:6379'];
// Tell client to use 'cluster' mode.
$options = ['cluster' => 'redis'];
// Create your redis client
$redis = new Predis\Client($servers, $options);
// Do something you want:
// Set the expiration for 7 seconds
$redis->set("tm", "I have data for 7s.");
$redis->expire("tm", 7);
$ttl = $redis->ttl("tm"); // will be 7 seconds
// Print out value of the key 'tm'
var_dump(array("msg"=>"Successfully connected to Redis Cluster.", "val"=>$redis->get("tm"))) ;
}
catch(Exception $ex){
echo ('Error: ' . $ex->getMessage() ); // output error message.
}
Related
I'm trying to move my application to Heroku but I'm having problems accessing the database via PHP. I'm able to access the AWS database from my machine locally, but when I deploy to Heroku it fails.
I've followed directions from the main page: https://devcenter.heroku.com/articles/amazon-rds
And I've tried other threads on SO/AWS:
Getting Mysql2::Error (SSL connection error: ASN: bad other signature confirmation) on Heroku App with AWS RDS
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_MySQL.html#MySQL.Concepts.SSLSupport
I also saw instructions here:
https://devcenter.heroku.com/articles/getting-started-with-php#provision-a-database
But I'm trying to port an existing application so I need to stick with my existing PDO syntax. I'm trying to get this to work:
$dbInfo = getenv('DATABASE_URL');
try{
$dbh = new PDO($dbInfo);
echo json_encode(array('outcome' => true));
}
catch(PDOException $ex){
echo json_encode(array('outcome' => false, 'message' => 'Unable to connect'));
}
I have the environmental variable setup properly and can run parse_url to access all of the array items; I also have the amazon-rds-ca-cert.pem located in the config folder.
I'm able to login remotely (via SequelPro) and have run the following in the query manager to try to force SSL cert:
GRANT USAGE ON *.* TO 'username'#'%' REQUIRE SSL;
But when I intentionally misspell the cert name my local database can still connect, which makes me think maybe I am missing something with the requirement.
Figured it out; needed to manually specify the SSL certificate within the PDO connection. Also had minor errors in the PDO syntax.
try{
$dbh = new PDO("mysql:host=$pdohost;dbname=$pdodb", $pdouser, $pdopass, array(
PDO::MYSQL_ATTR_SSL_CA =>'path/to/combined-cert.pem'
));
echo json_encode(array('outcome' => true));
}
catch(PDOException $ex){
echo $ex;
echo json_encode(array('outcome' => false, 'message' => 'Unable to connect'));
}
Also needed to be sure to update the AWS inbound rules to 0.0.0.0/0 to allow for Heroku to access.
I'm using laravel 5.2 and MongoDB 3.2.
I want to test if connection with is ok before my app starts (i can't use DB facade), in the monolog configuration. If connection is not ok, i will use logging in file.
By recommendation, i'm testing MongoClient, Mongo and MongoDB\Client, and using whatever is enabled.
I'm trying to test mongo connect as the following:
$mongoClient = new \MongoDB\Client('mongodb://localhost:27017');
$mongoClient->selectCollection('mydb', 'mycollection');
That's the return:
Client {
+manager: Manager {#21}
+uri: "mongodb://localhost:27017"
+typeMap: [
array => "MongoDB\Model\BSONArray",
document => "MongoDB\Model\BSONDocument",
root => "MongoDB\Model\BSONDocument"
]
}
Finnaly, my questions:
Exists a way to use DB facade before app starts?
How and what is the right way to test MongoDB connection with PHP?
If you has another suggestion, i will be thankful.
According to PHP document, the driver connects to the database lazily (http://php.net/manual/en/mongodb-driver-manager.getservers.php), the only way to test connection should be actually execute commands like findOne() stated in yours comment.
In addition, if the name of DB or Collection is uncertain at the point, you can use listDatabases() method which also throws exceptions if connection fails.
using this way you can check MongoDB connection with PHP:
$connection = new MongoClient(); // connects to localhost:27017
$connection = new MongoClient( "mongodb://example.com" ); // connect to a remote host (default port: 27017)
$connection = new MongoClient( "mongodb://example.com:65432" ); // connect to a remote host at a given port
I am building PHP app with codeigniter and using GAE with Mongodb. GAE is billing enabled but the connection to mongo is not stable and it drops 1/4 when the app is online, locally works fine with GAE PHP SDK:
php.ini
google_app_engine.enable_functions = "libxml_disable_entity_loader"
google_app_engine.enable_curl_lite = “1”
extension="mongo.so"
mongo_library.php
try {
//connect to the mongodb server
SELF::$mongo_client = new MongoClient($config_data['mongo_connection_string']);
//select the mongodb database
$this->db = SELF::$mongo_client->selectDB($config_data['mongo_database']);
} catch (MongoConnectionException $exception) {
//if mongodb is not connect, then display the error
show_error('Unable to connect to Database', 500);
}
I am using mongo free plan from mongolab.com google cloud provider.
Here is the link where is show My app and where you can see how it fails by refreshing couple of times.
$a = mongo_db::$mongo_client;
$connections = $a->getConnections();
print_r($connections);
Here is the issue I opened:
https://code.google.com/p/googleappengine/issues/detail?id=12392
and this issue/bug is already fixed.
I am trying to build a php application using memcached on the Bluemix cloud.
When adding the memcached service I get the server name, port, username and password. I installed memcached on my local server to test my application and it works:
$memcache = new Memcached();
$memcache->addServer('Server', port) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";
$memcache->set('val', 99);
echo $memcache->get('val');
But when I run it on Bluemix it returns the version as: 255.255.255 and doesn't read back values I store.
There is username and password which I tried to add using:
$memcache->setSaslAuthData('user', 'pass');
But when I run this it, the page simply stops loading on that line.
I am new to memcached so any ideas and help will be appritiated.
The only mention of Memcached I can fin on Bluemix is a 3rd party service provided by RedisLabs.
On the HowTo Connect page, it mentions a couple of additional calls that are required:
$memcache->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
// addServer
// setSaslAuthData - that is also in your code
So, it appears that the RedisLabs may also require the use of the Memcache binary protocol as well.
I am working on a Webservice and I'm using Predis as a redis library and I want the clients to be able to reuse the same TCP socket.
Currently , after running a load test on my service , I found that the number of socket in TIME_WAIT state increase fast on the server and at some point the connexion to Redis server gets refused.
According Redis documentation using PhpiredisStreamConnection with presistant option fixes the problem , but after adding this to my connect code , I m still facing the same issue. Any ideas?
Im using TCP connection as the redis instance are not on front servers.
<?php
include 'autoload.php';
$parameters = array(
'tcp://some.host01:6379?database=0&alias=master&persistent=1',
'tcp://some.host02:6379?database=0&alias=slave&persistent=1',
);
$options = array( 'replication' => true ,
'connections' => array('tcp' => 'Predis\Connection\PhpiredisStreamConnection','unix' => 'Predis\Connection\PhpiredisStreamConnection') );
$predis = new Predis\Client($parameters, $options);
?>
As per the documents Predis works for persistent connections when php process are configured as persistent process . Look at the following for more details :
https://github.com/joindin/joindin-web2/blob/master/vendor/predis-0.8/FAQ.md
http://php-fpm.org/