Moodle database connection failed in local - php

I'm trying to install an existing Moodle app in local to develop a child theme, but it appears the following message after configuring config.php. I'm not able to be aware of the error. That's my file:
<?php
unset($CFG); // Ignore this line
global $CFG; // This is necessary here for PHPUnit execution
$CFG = new stdClass();
$CFG->dbtype = 'mysqli'; // 'mysqli', 'mariadb', 'mysqli', 'mssql', 'sqlsrv' or 'oci'
$CFG->dblibrary = 'native'; // 'native' only at the moment
$CFG->dbhost = 'localhost'; // eg 'localhost' or 'db.isp.com' or IP
$CFG->dbname = '(name)'; // database name, eg moodle
$CFG->dbuser = '(same as above)'; // your database username
$CFG->dbpass = '(pass)'; // your database password
$CFG->prefix = 'pztp_'; // prefix to use for all table names
$CFG->dboptions = array(
'dbpersist' => false, // should persistent database connections be
// used? set to 'false' for the most stable
// setting, 'true' can improve performance
// sometimes
'dbsocket' => false, // should connection via UNIX socket be used?
// if you set it to 'true' or custom path
// here set dbhost to 'localhost',
// (please note mysql is always using socket
// if dbhost is 'localhost' - if you need
// local port connection use '127.0.0.1')
'dbport' => '3306', // the TCP port number to use when connecting
// to the server. keep empty string for the
// default port
);
$CFG->wwwroot = 'http://'.$_SERVER['SERVER_NAME'];
$CFG->dataroot = 'C:/wamp64/www/example/data';
$CFG->directorypermissions = 02777;
$CFG->admin = 'admin';
require_once(dirname(__FILE__) . '/lib/setup.php'); // Do not edit
dbname, dbuser and dbpass are censored due to privacy politics, as well as application name, but two first are the same value and the name of the "root" folder (named example) is given in dataroot path. I've created that user and DB in the DBMS, I've assigned all the privileges to the user for that DB and I've imported the DB. I use PHP 7.0.33 (that can't be updated) due to the Moodle version. I've tried using XAMPP and WAMP with the same result.
Thank you all in advance!

Maybe try creating a test PHP file - in the Moodle root folder - to test the connection to see if its Moodle or the database.
<?php
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
echo 'Success... ' . $mysqli->host_info . "\n";
$mysqli->close();

Related

No such file or directory connecting to RDS through EC2 with php

I've set up an RDS and an EC2 on amazon web services. The RDS is in one security group, and the EC2 in another. The EC2 security group accepts http, https and ssh traffic (though I have tried setting it to all traffic) from any location, while the RDS security group accepts all inbound traffic from itself, and the EC2 security group.
I have some php files on the EC2, with
$server = $_SERVER['DB_SERVER'];
$username = $_SERVER['DB_USERNAME'];
$password = $_SERVER['DB_PASSWORD'];
$database = $_SERVER['DB_DATABASE'];
$connection = new mysqli($server, $username, $password, $database);
echo $connection->connect_error;
where DB_SERVER etc are listed in dbinfo.inc in /var/www/inc/dbinfo.inc, as per this tutorial: http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateWebServer.html
This gives the response "No such file or directory", which I am told is because the mysql.sock file is not being found.
In /etc/my.cnf I have:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
*bunch of comments*
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
but no mysql.sock exists in /var/lib/mysql (or /var/lib/mysql/mysql)
Running
find . mysql.sock
and
find . mysqld.sock
both give "No such file or directory"
Thanks for your help
I had faced same problem while I was trying to deploy php application on aws ec2
and hosting database on rds
First of all, do not follow the amazon documentation for connecting rds database.
reason $_server['anything'] returns empty value. So
$server = $_SERVER['DB_SERVER']; in here $server holds nothing but a empty value
Use following code instead:
<?php
$dbhost = 'your rds instance endpoint';
$dbport = 'port numberdefault 3306 ';
$dbname = 'name of data base';
$charset = 'utf8' ;
$dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname};charset= {$charset}";
$username = 'username ';
$password = 'password';
try {
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "your sql query";
$pdo->exec($sql);
echo "successfully connected";
}
catch(PDOException $e)
{
t;
echo "Connection failed: <br> ". $e->getMessage();
}
$conn = null;
?>

Can't access mysql on amazon ec2

I have 2 amazon ec2 instances.I want to connect from one to another. One has a Mysql database (instance 1). When I run the php code below on instance 2. I get a
ec2-x-x-x-x.us-west-2.compute.amazonaws.com is currently unable to handle this request.
But when I run the same code on http://phpfiddle.org/ the code works. Why does it not work on my server?
<?php
// On WEB_SERVER
$host="public Ip of instance 1"; // Host name
$username="test"; // Mysql usernam
$password="pass"; // Mysql password
$db_name="mydab"; // Database name
$db_port="3306";
// Create connection
$conn = new mysqli($host, $username, $password, $db_name,$db_port);
Check connection
if ($conn->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}else
{
echo "connected";
}
?>

Cant connect to my Bluehost SQLDatabase with php script

i wrote a php script which should connect me with my database. I also uploaded the files in the public_html section, but it always throws this error :
Warning: mysqli::mysqli(): (HY000/2002): Connection refused in /Applications/XAMPP/xamppfiles/htdocs/FoodHelperSwift/db/public_html.php on line 24
Here is the line : $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
This is my code :
public_html
var $conn = null;
var $result = null;
public $dbhost = null;
public $dbname = null;
public $dbuser = null;
public $dbpass = null;
function __construct($dbhost, $dbuser, $dbpassword, $dbname) {
$this->dbhost = $dbhost;
$this->dbuser = $dbuser;
$this->dbpass = $dbpassword;
$this->dbname = $dbname;
}
public function openConnection()
{
$this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
echo "YESSSS";
echo $this->dbhost;
echo $this->dbuser;
echo $this->dbpass;
echo $this->dbname;
if(mysqli_connect_errno())
{
throw new Exception("Could not connect with database");
$this->conn->set_charset("utf8");
}
}
public function closeConnection()
{
if ($this->conn != null)
{
$this->conn->close();
}
}
}
and
register user
:
require("../db/public_html.php");
$dbhost = "127.0.0.1";
$dbname = "xxxxxxxx";
$dbuser = "xxxxxxxxx";
$dbpassword = "xxxxxxx";
$returnValue = array();
if(empty($_REQUEST["userEmail"]) || empty($_REQUEST["userPassword"])
|| empty($_REQUEST["userFirstName"])
|| empty($_REQUEST["userLastName"]))
{
$returnValue["status"]="400";
$returnValue["message"]="Missing required information";
echo json_encode($returnValue);
return;
}
$userEmail = htmlentities($_REQUEST["userEmail"]);
$userPassword = htmlentities($_REQUEST["userPassword"]);
$userFirstName = htmlentities($_REQUEST["userFirstName"]);
$userLastName = htmlentities($_REQUEST["userLastName"]);
$salt = openssl_random_pseudo_bytes(16);
$secured_password = sha1($userPassword . $salt);
$dao = new MySQLDAO($dbhost, $dbuser, $dbpassword, $dbname);
$dao->openConnection();
The password etc. is right and i also tried localhost, but then i got an error that he cant find the file maybe you could help me.
Most likely you're not following steps provided by your website hosting provider.
Third party hosting solutions usually require you setup your remote IP
From the error message you specified it looks like you're trying to do this from your own home XAMP web service.
First - the basics
:
localhost - won't work from home - because that's going to look for your own MySQL database not the hosted db
Second - logging in from home (or remotely)
read the docs (always a good idea) Remote access to Bluehost MySQL
Use the following configuration settings for connecting to your database
Host name = (use the server IP address)
Database name = (cpanelUsername_databaseName)
Database username = (cpanelUsername_databaseUsername)
Database password = (the password you entered for that database user)
MySQL Connection Port = 3306
TCP or UDP, either is fine.
Allowing a Remote Server to Access Your Database
Before connecting to MySQL from another computer, the connecting computer must be enabled as an Access Host.
Log into cPanel and click the Remote MySQL icon, under Databases.
Type in the connecting IP address, and click the Add Host button.
Note: You can find and add your IP address directly from this tool. Look for Your IP is: 123.123.12.123 [Add]. Clicking the [Add] link will input your IP into the field box below.
Click Add, and you should now be able to connect remotely to your database.
To troubleshoot this, strip your code down to the basics. Make yourself a little testMyDb.php file, containing only the minimal stuff. For example:
$dbhost = "127.0.0.1";
$dbname = "xxxxxxxx";
$dbuser = "xxxxxxxxx";
$dbpassword = "xxxxxxx";
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
Once you have that working, you can proceed to debug your php class and make sure you are setting things up correctly.
Your file named public_html.php contains some code that's part of a php class implementation (for example __construct()), but I don't see a class ClassName { line to set up a class definition. It's possible you've copied some fragments of code from somewhere without getting it all.
If your simple test DOESN'T work, check with bluehost's tech support krewe. You may need some special credentials or database name to connect to MySQL from one of their Windows hosts.
If you're using the MySQL server on a bluehost machine, and trying to connect to it from your local machine, that will not work (especially not with 127.0.0.1). You'll need to configure bluehost to allow remote MySQL connections, and you'll have to use the actual MySQL hostname.

IIS to Apache connection issue

I am moving what looks like a pretty simple php/MSQL app from a Windows IIS server to a Cpanel/ Apache server. The app works fine on existing IIS/MySQL server using this format to connect:
<?php
class Connection {
var $conn;
var $user = 'XX0000XX_XXadmXX';
var $pass = 'XXXpasswordXXXX';
var $dbname = 'XXckbXXX_XXX13XX';
var $host = '74.0.0.0';
function Connection() {
$this->conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->dbname,$this->conn) or die( "db-connect". mysql_error());
}
function closedb() {
//mysql_close(($this->host, $this->user, $this->pass ) or die( "db-close". mysql_error() );
mysql_close($this->conn)or die( "db-close". mysql_error() );
}
}
?>
I imported the DB into CPANEL/MySQL using PhpMyAdmin. 1st test failed due to $host not accepting IP address value. After changing IP to localhost - tested the connection with a DB connection script and that passed. The error is that at I can connect with the same username and PW as Windows server. But after logging in any link I click in the admin kicks me out.
Any clues would be helpful - let me know if I can provide any information that would help. I am thinking that there may be a windows specific change that needs to happen or something to do with the the DB host.
Here are some values in a settings file - before the Ip addresses were set with domain name - set with IP to test:
define('ADMIN_MANAGEMENT', "sitename");
define('FROM_NAME', "Admin");
define('SITE', "sitename");
define('SITE_NAME', "http://74.0.0.0/");
define('ADMIN_ID', "dwayne#fair.com");
define('KEYWORD', "sitename");
define('SITE_URL',"http://74.0.0.0/");
define('SITEIMGPATH', "http://74.0.0.0/public/images/");
define('ITEMS_PER_PAGE', "15");
define('PRODUCTS_PER_PAGE', "5");
define('UPLOAD_FILES', "uploads/files/");
define('UPLOAD_IMAGES', "uploads/images/");
define('SMALL_IMAGE_WIDTH',190);
define('SMALL_IMAGE_HEIGHT',77);
define('PROFILENAME', "Welcome to domain.com");
define('CARID',1000);
define('RESIZE_IMAGE_WIDTH',400);
define('RESIZE_IMAGE_HEIGHT',300);
/*Live path*/
define("ROOT", "http://" . $_SERVER["SERVER_NAME"] . "/admin/");
define("ROOTS", "http://" . $_SERVER["SERVER_NAME"] . "/admin/");

Mongodb PHP driver: how to create database and add user to it?

so using the mongodb shell, I was able to create a database and add a username and password to it. How can I do the same in php? I have everything installed and able to connect to mongodb server.
However, I can't find any information in the doc.
I do not believe addUser() is implemented in the PHP driver.
However, there is an execute that should allow you to do an addUser() the same way you would from the mongo shell:
EDIT: After testing, I couldn't get execute to do what you wanted, but I did find that the following works:
<?php
// open connection
$mongo = new Mongo("mongodb://" . MONGO_USER . ":" . MONGO_PASS . "#" . MONGO_HOST, array("persist" => "abcd1234"));
$db = $mongo->selectDB("admin");
// user info to add
$username = "testUser";
$password = "testPassword";
// insert the user - note that the password gets hashed as 'username:mongo:password'
// set readOnly to true if user should not have insert/delete privs
$collection = $db->selectCollection("system.users");
$collection->insert(array('user' => $username, 'pwd' => md5($username . ":mongo:" . $password), 'readOnly' => false));
?>
That adds a new user to the admin db on my server - checked via mongo shell after executing PHP script.
That said - why do you want to add a Mongo user from a PHP script?
A way to create a new mongodb user from PHP is via MongoDB::command:
//info to add
$db_name = 'db_name';
$db_user = 'new_user';
$db_pass = 'new_pass';
//autenticate with a user who can create other users
$mongo = new MongoClient("mongodb://root:root#localhost/admin");
$db = $mongo->selectDB( $db_name );
//command to create a new user
$command = array
(
"createUser" => $db_user,
"pwd" => $db_pass,
"roles" => array
(
array("role" => "readWrite", "db" => $db_name)
)
);
//call MongoDB::command to create user in 'db_name' database
$db->command( $command );
Tested with mongo 3.0 and PHP mongo driver 1.6

Categories