How to Skip Secure Auth - MySQL 5.6.15 using php? - php

I am trying to access a third party database and having the following error:
Warning: mysql_pconnect(): mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication.
but I am able to access the database through command prompt by using following command:
mysql -uTheUseerNAme -pThePassword DbName -h HostName --skip-secure-auth
Note: here in the above command I am using --skip-secure-auth to access the database. But now my question is can we do the same thing in php while making connection with database in config.php file?

With newer versions of PHP (5.6) and mysqli drivers, the default for connecting to MySQL server is to use secure authorization (TLS). It appears that when using an older MySQL server, this will fail.
It doesn't seem that there is a way from within PHP to disable the authentication on connection.
The current workaround is to add this to your MySQL server config (typically found in /etc/mysql/my.cnf or /etc/my.cnf).
[mysqld]
skip-secure-auth
This will disable secure auth from the server side, forcing newer clients (case in point newer versions of PHP/mysqli) to skip the secure auth when connecting, effectively replicating the --skip-secure-auth on all connecting clients.
As a possible workaround without changing the MySQL server config, it appears there is a MYSQLI_OPT_SSL_VERIFY_SERVER_CERT option that can be set on the mysqli connection instance via mysqli_options().
mysqli_options($conn, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, false);
Setting this to false should yield the behavior you are looking for, although there are reports that this does not work and in our testing it did not in fact work.
There appears to have been an update which was pushed to PHP 5.6.16 and later which resolves this issue. My guess is that you are running something between PHP 5.6.0 and 5.6.15.

Related

MySQL connection fast through mysql cli, but slow using PDO

I have a MySQL database running on Google Cloud. It has SSL enforced, and so I use certificates and keys to connect to it:
On the command line, I use: mysql --ssl-ca=/path/to/server-ca.pem --ssl-cert=/path/to/client-cert.pem --ssl-key=/path/to/client-key.pem --host=ip-address --user=username --password
With PDO, I use the PDO::MYSQL_ATTR_SSL_CA, PDO::MYSQL_ATTR_SSL_CERT and PDO::MYSQL_ATTR_SSL_KEY options to indicate the required files.
Both connections work fine, except that the PDO connection in PHP is very slow. The CLI method takes milliseconds to connect and execute a queries, while the PDO method takes 5-10 times longer for the same amount of connections. I tried both methods from the same machine, so it doesn't seem to be a hardware/network issue. Could PDO be causing issues here?
I'm using Laravel in case that might be relevant.
Update: things I've tried
Run any other PHP script (that doesn't include a MySQL connection) on the same server: perfectly fast.
Run a PHP script that connects to a database on 127.0.0.1 / localhost and performs a query: perfectly fast.
Connect and query using MySQL CLI (as already mentioned in the question): perfectly fast - although hard to verify how fast so I could be imagining it.
Connect and query though PHP/PDO from different machines using all the same settings: very slow, just like the original machine I tried it on.
So the only thing I haven't tried yet is turning off SSL/TLS. Unfortunately, I cannot do that with this instance for security reasons. Also, based on the fact that a SSL/TLS connection using the CLI is very fast, I'm concluding that it must be related to something PHP- or PDO-specific.
I'm going to do some debugging myself and will add any relevant results once I have them.
I ended up opting for a Google Cloud Compute VM to host my PHP and then connect through a private IP. It appears that this is working.
I'm not sure if PDO was actually slower than the MySQL CLI anymore, it may have just seemed so.

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'(13)

my WordPress site is not loading, it's getting this error:
Could not successfully run query (SELECT name, value FROM wp_simple_shortcodes WHERE 1) from wp_simple_shortcodes: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'(13)
I am told it is because MySQL extension is now deprecated.
My WordPress (and plugins) are up to date so should be running MySQLI already. My hosting company said I need to update my code from my MySQL to MySQLI to fix the issue. But I never wrote any custom code for this site at all and I am a complete beginner with PHP.
What files do I need to update? How can I fix this?
It's more likely this has something to do with the database server running on the webserver of your hosting company. Maybe they've updated the MySQL server version or they changed some PHP settings.
You should ask them to check the PHP and MySQL config settings and to restart both the webserver and the database server. That should fix it.

Ubuntu Lamp php script with mySQL connection not using Apache

Hi I got a strange problem with my LAMP installation
I'm using an Ubuntu Server with PHP5 Apache and mySQL, all runs great when accessing via http.
But the thing is, I also want to trigger some scripts using bash, basically so I can trigger them at will and prevent them being used via Apache/HTTP
The scripts themselves run when called with php5, all includes are found.
But for some unknown reason mySQL does not seem to initiate the Database the way it does via Apache. I just get a message back saying no database is connected.
Is there some special setup for bash related php calls?
Figured it out. There are no special DB settings for PHP run from Bash, However because I work on several Servers I was using the $_SERVER['HTTP_HOST'] variable to determin which login credentials should be used. Therefore it only worked on the server where the default credentials were correct.
For now I just put in an extra option in the code that uses credentials based on the value of the dirname(__FILE__) variable. Works great.

Can I set a query timeout when using Zend_Db_Adapter_Pdo_Mssql?

We have a PHP application (hosted on Linux) which uses Zend Framework components to query a Microsoft SQL Server 2008 database. The PHP application is hosted in a datacenter with reliable internet connection, but the SQL Server database is at the far end of a VPN connection that drops out regularly.
The issue we have is that VPN drop outs occasionally occur while queries against the SQL server are in progress. When this occurs our application can wait up to 2 hours before finally raising the following exception:
SQLSTATE[HY000]: General error: 20004 Read from the server failed [20004] (severity 9) [(null)]
What I'd like to do is set an overall query timeout and/or read timeout of around 2-3 minutes so that the application gets an exception much earlier and con recover from it without blocking for 2 hours.
We're using the pdo_dblib extension to connect to SQL Server, and I've been through the php.net docs and I can't find any timeout options either for the connection of php.ini.
Try using the PDO::ATTR_TIMEOUT attribute. I'm not sure what the default value is for pdo_dblib, it may differ between the drivers.
PHP works with the Unix library FreeTDS which interacts with MSSQL/Sybase servers.
FreeTDS is an open-source implementation of the TDS (Tabular DataStream) database client access protocol and related libraries used by Sybase and Microsoft. FreeTDS supports all versions of the protocol from both vendors, and includes DB-Lib, CT-Lib, and ODBC libraries.
Settings for a timeout must be set therefore in the freetds.conf file.
Please see http://freetds.schemamania.org/userguide/freetdsconf.htm

Connecting to external MySQL DB from a web server not running MySQL

While I've been working with MySQL for years, this is the first time I've run across this very newbie-esq issue. Due to a client demand, I must host their website files (PHP) on a IIS server that is not running MySQL (instead, they are running MSSQL). However, I have developed the site using a MySQL database which is located on an external host (Rackspace Cloud). Obviously, my mysql_connect function is now bombing because MySQL is not running on localhost.
Question: Is it even possible to hit an external MySQL database if localhost is not running MySQL?
Apologies for the rookie question, and many thanks in advance.
* To clarify, I know how to connect to a remote MySQL server, but it is the fact that my IIS web server is not running ANY form of MySQL (neither server nor client) that is giving me trouble. Put another way, phpinfo() does not return anything about MySQL. *
Yes, you can use a MySQL database that's not on the same machine as Apache+PHP.
Basically, you'll connect from PHP to MySQL via a network connection -- TCP-based, I suppose ; which means :
MySQL must be configured to listen to, and accept connections on the network interface
Which means configuring MySQL to do that
And given the required privileges to your MySQL user, so he can connect from a remote server
And PHP must be able to connect to the server hosting MySQL.
Note, though, that habing MySQL on a server that's far away might not be great for performances : each SQL query will have to go through the network, and this could take some time...
If phpinfo is not returning anything about MySQL you need to install the MySQL plugin for PHP, easiest way to do that probably is to just upgrade PHP to the latest version. If not there is a .DLL file that you will need.
http://www.php.net/manual/en/mysql.installation.php
you will need to install the mysql extensions. this link should help: http://php.net/manual/en/install.windows.extensions.php
The MySQL server has nothing to do with PHP itself. What "mysql support" in PHP means is that it's been compiled with (or has a module loaded) that implements the MySQL client interface. For windows, it'd be 'mysql.dll', and on Unix-ish systems it'd be 'mysql.so'. Once those are loaded, then the various MySQL intefaces (mysql_xxx(), mysqli_xxx(), PDO, MDB2, etc...) will be able to access any MySQL server anywhere, as long as you have the proper connection string.

Categories