using memcached under bluemix with php - php

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.

Related

phppgAdmin has more than 1 Error - also Error Loading servers.php

I am trying to learn PostgreSQL and so installed it and phppgAdmin on my laptop.
I went to localhost/phppgadmin, logged in and I get all these errors on the front page:
I tried going troubleshooting on Google, but unfortunately there are not enough answers to this question.
Also, config.inc.php file looks like this after I altered it according to the Tutorial I followed to connect the Database to PHP:
$conf['servers'][0]['desc'] = 'PostgreSQL';
// Hostname or IP address for server. Use '' for UNIX domain socket.
// use 'localhost' for TCP/IP connection on this computer
$conf['servers'][0]['host'] = 'localhost';
// Database port on server (5432 is the PostgreSQL default)
$conf['servers'][0]['port'] = 5432;
// Database SSL mode
// Possible options: disable, allow, prefer, require
// To require SSL on older servers use option: legacy
// To ignore the SSL mode, use option: unspecified
$conf['servers'][0]['sslmode'] = 'allow';
// Change the default database only if you cannot connect to template1.
// For a PostgreSQL 8.1+ server, you can set this to 'postgres'.
$conf['servers'][0]['defaultdb'] = 'template1';
// Specify the path to the database dump utilities for this server.
// You can set these to '' if no dumper is available.
$conf['servers'][0]['pg_dump_path'] = '/usr/bin/pg_dump';
$conf['servers'][0]['pg_dumpall_path'] = 'C:\\xampp\\pgSql\\11.2\\pg_dumpall.exe';
Thank you in advance
EDIT: Is it possible to just user the original pgadmin?!
This error is because the version of phpPgAdmin you are running does not support php7. You can either downgrade your PHP version or run the code from git master, available at https://github.com/phppgadmin/phppgadmin.

Connect to mongoDb Bitnami server hosted on Amazon EC2

So I have written a basic program with PHP and MongoDB and I tested it locally using xampp. This all works fine. Now I want to publish it as a website.
After some research I decided to use Bitnami and I am running an EC2 server on Amazon. I am able to ssh and run commands from the terminal. Now I want to connect to the server and create a Mongo client. I am a bit puzzled and do not know how to do this exactly. I am using the code below. The ssh works fine but I do not know how to set up the mongo client. The '$m = new MongoClient();' works fine locally for localhost, I have also tried to supply to MongoClient my username and password but without success. Thank you very much for any help!
include('Net/SSH2.php');
include('Crypt/RSA.php');
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('../../etc/bitnami.pem'));
$ssh = new Net_SSH2('ip_address');
if (!$ssh->login('bitnami', $key)) {
exit('Login Failed');
}
$ssh->exec('mongo admin --username root -p <myPassword>');
$m = new MongoClient();
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
$document = array(
"title" => "MongoDB",
"description" => "database"
);
$collection->insert($document);
Update answer to Jota Martos (unable to write it as comment):
Hi, thank you for your answer. Yes for now I allow access from all ports and IP's for test purposes. I can successfully connect via ssh but now I am stuck. I am running a Mean bitnami application and my website is running on GoDaddy server under PHP. So I want to connect with PHP to my mongoDB bitnami app that is hosted on AWS. So now the problem is that on my GoDaddy server mongoDB is not installed. So running the following code does not work:
$m = new MongoDB\Driver\Manager("mongodb://${bitnami}:${AWSpassword}#hostIp");
Fatal error: Class 'MongoDB\Driver\Manager' not found.
So my question how do I connect with PHP from my GoDaddy server to the EC2 Amazon server and connect to Bitnami mongoDB account. Should I first setup an ssh connection to the server and then try to login to mongoDB server or how should I connect?
Any help is appreciated, thank you in advance!
According to php.net the function that is used is Deprecated, refer this.
Can you try something like:
<?php
$manager = new MongoDB\Driver\Manager("mongodb://myusername:myp%40ss%3Aw%25rd#example.com/mydatabase");
?>
Refer this
Hi Bitnami Engineer here,
Apart from this tip, please note that the database port for the nodes in this solution cannot be accessed over a public IP address by default. For security reasons, we do not recommend making the database port accessible over a public IP address.
https://docs.bitnami.com/aws/infrastructure/mongodb/#how-to-connect-to-mongodb-from-a-different-machine
You can open the MongoDB port in the server by accessing the AWS console and modifying the access group configuration
https://docs.bitnami.com/aws/faq/#how-to-open-the-server-ports-for-remote-access
You will also need to modify the MongoDB configuration file (/opt/bitnami/mongodb/mongodb.conf) to allow the database to not to work only on 127.0.0.1.
Regards,
Jota
If you are looking to connect a tool like Studio 3T to a remote Mongodb instance built using bitnami AMI you can follow the steps below:
1. Open inbound port 27017 on instance security group.
2. Open Instance log of the instance and find the password for user root.
3. Open Studio 3T, server name is instance public ip and port 27017.
4. Got to Authentication and select basic and put in the user as root and password as obtained from instance log.
Click on Test Connection, it should work.

PHP cant connect to memcached server [duplicate]

I m running centos 6. Using apache for handling php and nginx to handle scripts images and css
i have installed memcached server.
PORT="11211"
USER="memcached"
MAXCONN="4096"
CACHESIZE="512"
OPTIONS="-l 127.0.0.1"
i have also installed the module for php.
i created a new php file
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
i checked the memcached status and it is running.
I am always getting "Could not connect".
I tried to change the value to 'localhost' from '127.0.0.1' - still not working.
$memcache = new Memcache();
$memcache->addServer('127.0.0.1', 11211) or die ("Could not connect");
var_dump($memcache->getExtendedStats());
$memcache->set('key', 'hello world', false, 60);
echo $memcache->get('key');
//$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
Output
array(1) { ["127.0.0.1:11211"]=> bool(false) }
What does connect and addServer do differently? Which is best way to do?
But i am not getting the Hello World
More updates on the code and on this problem..
phpinfo is showing memcached.
var_dump($memcache->get('key')); gives
bool(false)
why should i use addServer instead of connect?
More update on the code
$memcache = new Memcache;
$memcache->addServer('localhost', 11211);
echo $memcache->getServerStatus('localhost', 11211);
output : 1
//$memcache->set('key', 'hello world') or die("failed to store data");
output : failed to store data
few more details
getsebool httpd_can_network_memcache
it returns off
Should it return on?
Notice: Memcache::connect(): Server 127.0.0.1 (tcp 11211, udp 0) failed with: Permission denied (13)
As flushed out in the comments, it appears you are running Security-Enhanced Linux (SELinux) which adds an extra layer of security at the kernel level. In my experience and usage, I found that SELinux adds a force field around certain services so that they cannot access particular assets on the system. For example, if I want to serve html content from /home/src/web, I have to tell the system that it is ok for the httpd service to access content in the /home/src/web path. To do this I would issue the following command:
$ -> setsebool -P httpd_enable_homedirs 1
Basically, to allow cross-communication between services, you have to allow such access via a policy, much like "pinholing" a firewall to allow access to a specific port, except with SELinux you are not granting access to a port, rather you are granting access to another part of the system, or service. Fortunately for us, there are several built in policies which we can use the above setsebool construct, rather than trying to define our own policies, which can be a pain. For a more complete explanation of SELinux check out the wikipedia page.
Now to answer your specific questions:
why should i use addServer instead of connect?
addserver() will allow you to add multiple ips (or hostnames) to a list from which it is assumed that cached values are present, i.e. a pool of memcache servers. Whereas the connect() will only allow you to connect to the single, specified server.
getsebool httpd_can_network_memcache, it returns off, Should it return on?
Yes, it appears that turning on this specific setting will allow you to connect to a memcache server, with SELinux enabled, however on my production servers I still have it set to off, but have the following set:
$ -> setsebool -P httpd_can_network_connect 1
I believe, that either setting will accomplish the objective, however with the above setting, if you have a memcache server on another host, httpd can still access it.
Here is a decent write-up on pinholing SELinux to allow httpd service access to other services.

How to connect MySQl using php on IBM Bluemix?

I am install service MySQL to my PHP app on Bluemix and the error is on connection establish on this lines:
$con = mysql_connect("192.155.247.248:3307","uqDqUZ2EKoZ5I","pWXeBZbNtdpOv");
if (!$con){
echo "Failed to connect to MySQL: " .mysql_error();
}
mysql_select_db("d65a2b7e14b594d18a049ac918a4a8603",$con);
Quentin suggested to use mysqli instead of mysql_* as the latter is deprecated, i.e. try this:
$mysqli = new mysqli("192.155.247.248:3307","uqDqUZ2EKoZ5I","pWXeBZbNtdpOv", "MYDB");
$result = $mysqli->query("SELECT * from MYTABLE");
$row = $result->fetch_assoc();
Create a folder
.bp-config/options.json in the parent folder
and add
{
"PHP_EXTENSIONS": ["mysqli"]
}
in the options.json folder
the sqli connect will work fine now
Are you getting:
ERROR 2003 (HY000): Can't connect to MySQL server on '$host' (60).
A developer may have asked a similar question on developerWorks.
The answer seemed to be as follows:
Downloaded the latest PHPMyAdmin(4.1.9)
Created a BlueMix application using - cf push -b https://github.com/dmikusa-pivotal/cf-php-build-pack.git
${myPhpAdminApp} . Note: the PHP build pack is used is PHP 5.4.26, which enables multi-byte support (it is different from the Heroku one in the BlueMix docs). This was necessary because the Heroku pack bundled PHP 5.3.27 which
doesn't enable "multi-byte" character support by default. Multi-byte
support is required to be enabled by PHPMyAdmin apparently.
Added the existing MySQL service to this app. And picked the host, port, user, and password details from the VCAP_SERVICES
environment variable.
Copied config.sample.inc.php in the PHPMyAdmin to config.inc.php and added or modified the following lines in it based on the MySQL
service VCAP_SERVICES details picked in previous step -
$cfg['Servers'][$i]['host'] = 'host-ip-from-vcap_services';
$cfg['Servers'][$i]['port'] = 'port-from-vcap_services';
$cfg['Servers'][$i]['user'] = 'user-from-vcap_services';
$cfg['Servers'][$i]['password'] = 'password-from-vcap_services';
Pushed the updates using the above cf push ... again.

memcache for php - unable to connect

I m running centos 6. Using apache for handling php and nginx to handle scripts images and css
i have installed memcached server.
PORT="11211"
USER="memcached"
MAXCONN="4096"
CACHESIZE="512"
OPTIONS="-l 127.0.0.1"
i have also installed the module for php.
i created a new php file
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
i checked the memcached status and it is running.
I am always getting "Could not connect".
I tried to change the value to 'localhost' from '127.0.0.1' - still not working.
$memcache = new Memcache();
$memcache->addServer('127.0.0.1', 11211) or die ("Could not connect");
var_dump($memcache->getExtendedStats());
$memcache->set('key', 'hello world', false, 60);
echo $memcache->get('key');
//$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
Output
array(1) { ["127.0.0.1:11211"]=> bool(false) }
What does connect and addServer do differently? Which is best way to do?
But i am not getting the Hello World
More updates on the code and on this problem..
phpinfo is showing memcached.
var_dump($memcache->get('key')); gives
bool(false)
why should i use addServer instead of connect?
More update on the code
$memcache = new Memcache;
$memcache->addServer('localhost', 11211);
echo $memcache->getServerStatus('localhost', 11211);
output : 1
//$memcache->set('key', 'hello world') or die("failed to store data");
output : failed to store data
few more details
getsebool httpd_can_network_memcache
it returns off
Should it return on?
Notice: Memcache::connect(): Server 127.0.0.1 (tcp 11211, udp 0) failed with: Permission denied (13)
As flushed out in the comments, it appears you are running Security-Enhanced Linux (SELinux) which adds an extra layer of security at the kernel level. In my experience and usage, I found that SELinux adds a force field around certain services so that they cannot access particular assets on the system. For example, if I want to serve html content from /home/src/web, I have to tell the system that it is ok for the httpd service to access content in the /home/src/web path. To do this I would issue the following command:
$ -> setsebool -P httpd_enable_homedirs 1
Basically, to allow cross-communication between services, you have to allow such access via a policy, much like "pinholing" a firewall to allow access to a specific port, except with SELinux you are not granting access to a port, rather you are granting access to another part of the system, or service. Fortunately for us, there are several built in policies which we can use the above setsebool construct, rather than trying to define our own policies, which can be a pain. For a more complete explanation of SELinux check out the wikipedia page.
Now to answer your specific questions:
why should i use addServer instead of connect?
addserver() will allow you to add multiple ips (or hostnames) to a list from which it is assumed that cached values are present, i.e. a pool of memcache servers. Whereas the connect() will only allow you to connect to the single, specified server.
getsebool httpd_can_network_memcache, it returns off, Should it return on?
Yes, it appears that turning on this specific setting will allow you to connect to a memcache server, with SELinux enabled, however on my production servers I still have it set to off, but have the following set:
$ -> setsebool -P httpd_can_network_connect 1
I believe, that either setting will accomplish the objective, however with the above setting, if you have a memcache server on another host, httpd can still access it.
Here is a decent write-up on pinholing SELinux to allow httpd service access to other services.

Categories