CodeIgniter: Connecting two mysql instances - php

I want to use two database instances in my CodeIgniter application. I have set up db1 and db2 in the application but it always uses the socket of the first database.
$this->db2 = $this->load->database('db2', TRUE);
Message: mysqli::real_connect(): (HY000/2002): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
but the second server runs on '/var/run/mysqld/mysqld2.sock'
I can not see where to tell it to use the second socket.
Please note that I still want to connect to the first database too, I need to merge two replications, so setting defalut socket in php.ini is not an option.

You'll also need to edit your database config file to add the additional settings. Example:
$db['default']['hostname'] = 'localhost';
..
..
..
$db['second_db']['hostname'] = 'localhost';

It seems it was something CodeIgniter was missing, at least for mysqli driver: The socket parameter was always NULL in the driver code. With a modification of the code and the configuration array, it is possible to connect to any given socket for the server(s).
Add this to your config
'socket' => '/tmp/mysql.sock', // if no socked uses set it to NULL
And change this in your /system/database/mysqli/mysqli_driver.php
return $mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $this->socket, $client_flags)
But keep in mind that editing core files could conflict with framework updates, causing errors in your database implementation.
http://forum.codeigniter.com/thread-486.html

Related

Error connecting a remote server from a PHP page

I have a .php file which should receive and show data from a remote database. I run my program from PHPStorm (which is connected to the remote database through the "Database" right-hand pane) and a browser. Both ways I get an error which depends on the number of arguments I pass to pg_connect() function.
If I use
$dbconn = pg_connect("host=pg hostaddr=server.address.ru port=5432 dbname=studs user=... password=...")
than the error is
Unable to connect to PostgreSQL server: could not parse network address "server.address.ru": Unknown host in...
But I am sure that I wrote the address correctly (there are no typos in it). This way I am not sure about the correctness of the format of the passed arguments.
If I use
$dbconn = pg_connect("host=server.address.ru dbname=studs user=... password=...")
command, the error is
pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection timed out
I found a lot of information about this errors, but it mostly refers to localhosts and doesn't solve my problem. I guess, the problem can be in the way this connection is set in the function, but I do not know why it doesn't work properly. How can I solve it?
Thanks to #TangentiallyPerpendicular, I got on a right way of setting the connection. But since I have PostgreSQL remote connection, it wasn't just up to this answer.
What I did and how I set the connection (I work from PHPStorm so all the actions are based on this platform):
Open PuTTY and set an SSH connection (an SSH tunnel) between the server's DB port (for PostgreSQL it's usually 5432) and your local computer's PostgreSQL port (5432 most often too). You can do the same from a command line.
Open PHPStorm and in "Database" section (an icon on the right-hand side of the environment or "Data Sources and Drivers" in Settings) set general information ("General" section) and set another SSH tunnel ("SSH/SSL"). In SSH Configurations (the same "SSH/SSL" section) set a local port - it will be important in our connection! Let's say, this port is 20000. The port of the server you're connecting to is a usual one (usually 22 or 2222).
In the program the right use of function is $dbconn = pg_connect("host=localhost port=20000 dbname=studs user=... password=...") or die('Error: ' . pg_last_error());
The connection is set.
For those who has troubles setting an SSH tunnel with a remote PostgreSQL from PHP this can be useful too.

How to connect a PHP app to MySQL via pipes?

I have found glimpses of the fact that it is possible to connect PHP to MySQL via pipes in their documentation of MySQLi, but I cannot, for the life of me, find anyone explaining what is needed.
The host parameter claims:
When possible, pipes will be used instead of the TCP/IP protocol.
But when is it "possible"? I have my own machine, and I definitely have the necessary privileges to achieve this, I just don't know how. Connecting to the host localhost reports "Localhost via UNIX socket" when examining the host_info.
Trying to follow one (downvoted) comment from that page, and connecting to host ., with socket parameter set to mysql, causes a 2002 connection error.
How do I tell it to (always) connect via a pipe instead?
Today I had the same issue and it required much time to solve this on Windows.
I can establish a named pipe connection with the following code:
$connection = new mysqli('.', 'user', 'pass', 'database', null, '\\\\.\\pipe\\name_of_pipe');
The server, where I want to connect to, has the following configuration:
[mysqld]
skip-networking
enable-named-pipe
socket=name_of_pipe
Using '127.0.0.1', 'localhost' or NULL as hostname doesn't work. Also you must specify a path to the named pipe and not just the name of the pipe.
Unfortunately the PHP documentation is a little bit weak here...
Named Pipes only work under Windows.
Also
Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank.
It is not mentioned in the actual PHP documentation, but it should be still valid.

How can I access PDO in MAMP?

I'm setting up a PDO connection in a test script:
use app\SomeDAO;
class SomeTest extends \PHPUnit_Framework_TestCase
{
protected $db;
public function setUp()
{
$dsn = "mysql:host=localhost;dbname=baseball;user=root;password=root";
$this->db = new PDO($dsn);
}
I'm getting an error:
PDOException: SQLSTATE[HY000] [2002] No such file or directory.
How can I use PDO here?
In Unix based (like linux, bsd, or OS X) systems, with MySQL localhost is secret code for try-to-use-a-socket, unless a you force it via a protocol flag to not do this (and no one ever does this). Just remember localhost usually equals socket file.
If Mysql in your MAMP is running in non-socket mode, you can try replacing the host value with 127.0.0.1 which connects via TCP on via port to the local machine--you'll need to figure out which port it's running on if it's not the default port.
If you look at the MAMP start script
less /Applications/MAMP/bin/startMysql.sh
You can see if it's starting in socket mode, and what file it's using if it has a config param like:
--socket=/Applications/MAMP/tmp/mysql/mysql.sock
You can also investigate what open socket mysql might be using by following the answer in this question: error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
If you're running in socket mode, you need to make sure PDO knows the path to the socket file. One way to do this is specify the unix_socket instead of host in the dsn:
$dsn = "mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=baseball;user=root;password=root";
I had problem like that at MAMP. My decided it, when I connected with PDO I used next line code:
$this->pdo = new \PDO("mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;port=8889;dbname=mydatabase;charset=utf8", 'root', 'root');

SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql1.alwaysdata.com:3306' (2)

I have this error when i try to access to some web pages of my project:SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql1.alwaysdata.com:3306' (2)
alwaysdata is the phpmyadmin website I use for my database.
I noticed that it's when I try to access in some pages in relation with the database (create user for example... etc) and there's no problems with other pages like 'contact'.
I'm on mac OSX and I use MAMP server, always data, laravel and netbeans IDE. all configurations required to make the connection between my project and the database is correct.
Here it is:
SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql1.alwaysdata.com:3306' (2)
open:
/Applications/MAMP/htdocs/lesenfantsdurhone/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php
*/
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');
$password = array_get($config, 'password');
return new PDO($dsn, $username, $password, $options);
}
If someone have an idea...
Thank you.
The port has it's own parameter in the DSN:
'mysql:host=mysql1.alwaysdata.com;port=3306;dbname=xxx'
Alternatively just omit the port, 3306 is the default port.
I had this issue while using Laravel with MAMP and Sequal Pro.
Inside the .env file, I set the
DB_HOST = "localhost"
DB_PORT = "3306"
and this worked.
Before, I had
DB_HOST = "localhost:8889"
I kept getting this error because of the failing DNS service due to the time on my Virtualbox being off - it does that after you put your host machine to sleep for example.
Hope this helps someone in a similar situation.
In my case I had double definition of DB_HOST variable in '.env' file
First one is correct:DB_HOST=127.0.0.1
And last one was
DB_HOST=127.0.0.1:3306
with unwanted 3306
That was the cause.

Trouble Connection to phpMyAdmin MySQL database

I'm pretty new at php and I'm getting an error whenever I try to load a php page that requires access to the database I set up on phpMyAdmin
Here is the error:
Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)
The code I'm using for the connection is this:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
I set up phpMyAdmin on my server by following the directions from the site that hosts my website, so I'm pretty sure I did that correctly.
I've been able to get databases to work before on my computer by using WAMP, but this is the first time I've actually tried getting everything working online, so I don't know if this is a stupid error on my part, or if it's something bigger.
I don't know what a socket is either so I'm not sure how to troubleshoot this.
Thanks for your help!!
You have to find a way to connect to your MySQL instance. There are two ways, TCP (networking, even if that network address is 127.0.0.1) and sockets (a file such as /var/run/mysqld/mysql.sock). Generally, MySQL is configured out of the box to listen on both, and again in general there's little difference between the two. You have to be careful when creating or editing permissions that they match the connection type, user bob#localhost is different from user bob#127.0.0.1. I tend to use sockets and that's also what your script is trying to do by default. You could tell it to connect via TCP, but it's just as easy to tell it the proper path to the socket.
Anyway, for me the quickest way to figure it out is to try the command line tool. If you can do mysql -u root -p at the command line and get a MYSQL> shell prompt, type in "STATUS" and look for the Connection line, which might read Localhost via UNIX socket; and a bit further down you might see a line like "UNIX socket: /var/run/mysqld/mysql.sock" in which case you just tell your PHP script or global PHP configuration about the socket path, because right now it's looking in /tmp/mysql.sock which doesn't exist.
You can also see this when you log in to phpMyAdmin, it should display on the right hand side of the page some "Database Server" information -- look here for the "Server" line which might read something like "phpMyAdmin demo - MySQL (192.168.30.23 via TCP/IP" (a clear indication you're connecting over TCP).
Anyway, whichever method you use to find the path to the socket or deciding to use TCP networking, see the PHP manual page for information about using that to connect. You can also just set the global path in php.ini for the socket (which is what I'd suggest you do), then you don't have to set it manually in each script as it just uses the php.ini setting as a default. You may need to set each of the values for pdo_mysql.default_socket, mysql.default_socket, and mysqli.default_socket (though in your code you're only using mysqli, so technically you only need to set the last value...but why risk confusing your future self with questions like "why would mysqli work but pdo fail?" -- just set them all now and don't worry about it).
Hope this helps.
That either means you don't have the privileges to access phpMyAdmin or that socket isn't there. Try reinstalling phpMyAdmin.

Categories