Connect Laravel on ubuntu to Azure SQL Server - php

I am developing an laravel app using ubuntu as OS and my database is in a remote Azure Server.
After long a research I'm about to give up. Installed freetds, php5-sybase etc etc.
here is my connection file: (the default is set to sqlsrv)
....
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'myhostname:myport',
'database' => 'mydatabasename',
'username' => 'myusername',
'password' => 'mypassword'
),
....
and the error that I am getting is this one:
PDOException
SQLSTATE[01002] Adaptive Server connection failed (severity 9)
Any sugestions? If you guys need more details please ask :)
Thanks in advance

I solved my problem. It was missing #domain in my query string example:
$pdo = new PDO("dblib:host=xxxx:1433;dbname=yyyy", 'username#domain', 'password');

I get solution in Read from the server failed when trying to connect to sql-azure from tsql
Edit /etc/freetds/freetds.conf file and use 8.0 TDS version.
If haven't this file install FreeTDS with
sudo apt-get install freetds-bin

Check these:
port separator is "," on windows and ":" on linux/Mac. Since you have 1433, the default one, it is better probably not to use it at all
definitely test first from command line using one of these :
$ tsql -S sectionNameInFreetdsconf -U user -P pass
$ tsql -H hostname -p port -U user -P pass
locate freetds.conf on your disk. It is possible it exists in several places and tsql uses one while PHP used another one. Best is to symlink them into one common file and test on that. Note that a common place for that file is ~/.freetds.conf beside /etc/ or /usr/local/etc/
there should be a [global] section on your freetds.conf file. Put there these lines :
tds version = 8.0
text size = 20971520
client charset = UTF-8

Related

Docker php8.1 cant connect to MySQL [duplicate]

I am trying to use a PHP connection to connect MySQL Database which is on phpmyadmin. Nothing fancy about the connection just trying to see whether the connection is successful or not. I am using MAMP to host the database, the connection I am trying to use is this:
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "root";
try {
$conn = new PDO("mysql:host=$servername;dbname=AppDatabase", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
I have been using postman to test to see if the connection is working, but I keep receiving this error message:
Connection failed: SQLSTATE[HY000] [2002] Connection refused
Before I was receiving an error message of:
Connection failed: SQLSTATE[HY000] [2002] No such file or directory
This was because I had set the servername to localhost, through changing this to the IP address it has given me connection refused and I have no idea what is wrong.
Any help regarding this would be appreciated.
I found the reason why the connection was not working, it was because the connection was trying to connect to port 8888, when it needed to connect to port 8889.
$conn = new PDO("mysql:host=$servername;port=8889;dbname=AppDatabase", $username, $password);
This fixed the problem, although changing the server name to localhost still gives the error.
Connection failed: SQLSTATE[HY000] [2002] No such file or directory
But it connects successfully when the IP address is entered for the server name.
In my case MySQL sever was not running. I restarted the MySQL server and issue was resolved.
//on ubuntu server
sudo /etc/init.d/mysql start
To avoid MySQL stop problem, you can use the "initctl" utility in Ubuntu 14.04 LTS Linux to make sure the service restarts in case of a failure or reboot. Please consider talking a snapshot of root volume (with mysql stopped) before performing this operations for data retention purpose[8]. You can use the following commands to manage the mysql service with "initctl" utility with stop and start operations.
$ sudo initctl stop mysql
$ sudo initctl start mysql
To verify the working, you can check the status of the service and get
the process id (pid), simulate a failure by killing the "mysql"
process and verify its status as running with new process id after
sometime (typically within 1 minute) using the following commands.
$ sudo initctl status mysql # get pid
$ sudo kill -9 <pid> # kill mysql process
$ sudo initctl status mysql # verify status as running after sometime
Note : In latest Ubuntu version now initctl is replaced by systemctl
I spent quite a few hours in a docker environment where all my containers are docker containers and I was using Phinx for migrations. Just to share different responses with different configurations.
Working solutions
"host" => "db", // {docker container's name} Worked
"host" => "172.22.112.1", // {some docker IP through ipconfig - may change on every instance - usually something like 172.x.x.x} Worked
Non-working solutions
"host" => "127.0.0.1", // SQLSTATE[HY000] [2002] Connection refused
"host" => "docker.host.internal", // SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not resolve
"host" => "localhost", // SQLSTATE[HY000] [2002] No such file or directory
I was running Phinx in following way.
docker compose --env-file .env run --rm phinx status -e development
Using MAMP I changed the host=localhost to host=127.0.0.1. But a new issue came "connection refused"
Solved this by putting 'port' => '8889', in 'Datasources' => [
Using MAMP ON Mac, I solve my problem by renaming
/Applications/MAMP/tmp/mysql/mysql.sock.lock
to
/Applications/MAMP/tmp/mysql/mysql.sock
1. server cert verify flag
I was required to use SSL to connect, and needed to set PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT to false in the new PDO options array, besides the entry PDO::MYSQL_ATTR_SSL_CA for the CA file.
Without it, the mysql log on the server helpfully mentions
2021-07-27 17:02:51 597605 [Warning] Aborted connection 597605 to db: 'unconnected' user: 'unauthenticated' host: '192.168.10.123' (This connection closed normally without authentication)
where I was definitely passing the right db and username and such in the DSN. An empty options array will show the db and user in the error log, at least. I am sure there is a valid, technical reason for these things.
I am adding this information so I can more easily find it, the next time I end up on this page..
2. host in connection string
In the context of SSL, I've also seen the error when using the IP address instead of the hostname to connect, if the hostname was used as CN (Common Name) in the certificate.
For me was php version from mac instead of MAMP, PATH variable on .bash_profile was wrong. I just prepend the MAMP PHP bin folder to the $PATH env variable. For me was:
/Applications/mampstack-7.1.21-0/php/bin
In terminal run vim ~/.bash_profile to open ~/.bash_profile
Type i to be able to edit the file, add the bin directory as PATH variable on the top to the file:
export PATH="/Applications/mampstack-7.1.21-0/php/bin/:$PATH"
Hit ESC, Type :wq, and hit Enter
In Terminal run source ~/.bash_profile
In Terminal type which php, output should be the path to MAMP PHP install.
I had the same issue on a docker container from php:8.0-fpm-alpine image. I just added the following line in the Dockerfile and it fixed the issue:
RUN apk add mysql-client
I had a similar problem once, turned out the User in the database was created with something like:
CREATE USER 'webpage'#'localhost' IDENTIFIED BY 'password';
worked fine when the connection details php script had localhost, but not when the IP address was there. A quick swap (ip address when creating user and localhost in connection details) revealed those two things have to match.
For everyone if you still strugle with Refusing connection, here is my advice. Download XAMPP or other similar sw and just start MySQL. You dont have to run apache or other things just the MySQL.

Cannot find save handler 'redis' - Ubuntu

I am running LAMPP on ubuntu with PHP 5.6.23.
I decided to store my sessions in the Redis and I installed it. I also installed Predis too.
As a searched in the web I changed my php.ini to :
session.save_handler = redis
session.save_path = "127.0.0.1:6379"
But when tried create a session I am taking this error:
Warning: session_start(): Cannot find save handler 'redis' - session startup failed in /path/to/the/Untitled.php
You can think that there is a problem in Redis but it's not. It's working properly I can set something and I can check it with Redis-CLI :
My redis 'set' PHP code is :
<?php
require "predis/autoload.php";
$redis = new Predis\Client([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
]);
$redis->set('x', '42');
$redis->set('name','test');
?>
Results in telnet:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
MONITOR
+OK
KEYS *
+1471853424.389215 [0 127.0.0.1:36912] "KEYS" "*"
*2
$4
name
$1
x
Session php code:
<?php
session_start();
$count = isset($_SESSION['cont']) ? $_SESSION['cont'] : 1;
echo $count;
$_SESSION['cont'] = ++$count;
?>
It must increase the number every refresh but It just displays an error.
I re-Installed Redis again (v 3.2.3) but still no difference.
Is there any way to solve it?
Could LAMPP be the reason of problem?
EDIT:
I started to use phpredis instead of predis.
You try different things. Predis is a direct client to your Redis Server. If you want to use them as your session handler you have to install the php module.
sudo apt-get install php5-redis
or for PHP7
sudo apt-get install php7.0-redis
Then "redis" should be available as session handler.

Laravel 5 with Postgres SQL

I’m working on Laravel 5 with postgres as database. I’ve configured postgres 9.4 and pgAdmin III and these are working normally. When I try to run migrate it is giving me error:
[PDOException]
could not find driver
This is my database.php
'default' => 'pgsql',
'pgsql' => [ 'driver' => 'pgsql',
'host' => '127.0.0.1',
'database' => 'fms',
'username' => 'postgres',
'password' => 'root',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public', ],
Initially I though, it was due to configuration of postgres on windows 7 but I tried with plain php it works perfect
<?php
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=fms";
$db = pg_connect( "$host $port $dbname user=postgres password=root" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
?>
I’ve enabled php_pgsql and php_pdo_sql in wamp as well. I’m not sure how to fix this on laravel 5.
As you said you already choosed Default Database as Postgres SQL
'default' => 'pgsql',
It is a must that you need to uncomment the pdo and postgres shared object in your php configuration settings (php.ini)
i.e., You need to uncomment the following lines in your php.ini
extension=pdo_pgsql.so
extension=pgsql.so
Note :
Don't forget to stop and start your apache after doing this changes (or php-fpm if using that instead).
I had the same problem with Laravel-WAMP-PostgreSql driver not found exception. I even successfully established direct connection to Postgre as you have, but with no luck with the "php artisan migrate" command.
After long research I found out that there are multiple php.ini files in which "extension=php_pdo_pgsql.dll" and "extension=php_pgsql.dll" are comented out.
The solution is (of course) to uncoment the extensions in the following files:
~/wamp/bin/php/php5.5.*/php.ini
~/wamp/bin/php/php5.5.*/phpForApache
~/wamp/bin/php/php5.5.*/php.ini.install
~/wamp/bin/php/php5.5.*/php.ini-development
~/wamp/bin/php/php5.5.*/php.ini-production
and
~/wamp/bin/apache/apache2.4.9/php.ini
** You can leave off the "php.ini-development" and "php.ini-production" (don't need to uncoment these files).
Like #jeff said, this is probably caused by not setting DB_CONNECTION=pgsql in the .env-file. The .env-file has MySQL preconfigured, so you must edit this file.
You have to make DB related changes in
config/database.php
.env file
and other settings in
php.ini settings
If you are still getting error, then clear cache and config
php artisan cache:clear
php artisan config:clear
It should work now!
Run this command to easily uncomment the lines extension=pdo_pgsql.so and extension=pgsql.so from php.ini
sed -ri -e 's!;extension=pdo_pgsql!extension=pdo_pgsql!' $PHP_INI_DIR/php.ini
sed -ri -e 's!;extension=pgsql!extension=pgsql!' $PHP_INI_DIR/php.ini
If you do not have the drivers already installed, or it gives error like unable to load dynamic library PGSQL run this:
apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pgsql pdo_pgsql
This will install the pgsql and pdo_pgsql drivers.
you gotta modify .env file , then go to config>database.php and change the default to pgsql, and also in all your models file you need to change $protected $connection= 'pgsql'.
I'm using PHP7.3 and this worked for me, I forgot this:
apt-get install php7.3-pgsql
Everything runs smoothly afterwards. Just use whatever version of PHP you are using.

PDO dblib cannot connect with ip address as "host" parameter

I am trying to connect to a mssql db with dblib using the IP of the server as the host in the DSN.
With tsql, this works without any problem
$ tsql -S 192.168.56.101 -U .\user -P user
but in PHP this does not work.
$pdo = new PDO("dblib:host=192.168.56.101,1433;dbname=OtherC", '.\user', 'user');
The error I get is :
Error : SQLSTATE[01002] Adaptive Server connection failed (severity 9)
Check these:
port separator is "," on windows and ":" on linux/Mac
I never saw username as '.\user' but 'user'
locate freetds.conf on your disk. It is possible it exists in several places and tsql uses one while PHP used another one. Best is to symlink them into one common file and test on that. Note that a common place for that file is ~/.freetds.conf beside /etc/ or /usr/local/etc/
there should be a [global] section on your freetds.conf file. Put there these lines :
tds version = 8.0
text size = 20971520
client charset = UTF-8

beanstalkd driver configuration error in laravel app

I am trying to use beanstalkd as the queue driver for my laravel app. I am getting this error when I am trying to push something on the queue.
Pheanstalk_Exception_ConnectionException
Socket error 111: Connection refused (connecting to localhost:11300)
Please note that in 'queue.php' I have just changed the default driver to 'beanstalkd' but haven't changed any settings for the driver. The settings for the driver are as follows-
'beanstalkd' => array(
'driver' => 'beanstalkd',
'host' => 'localhost',
'queue' => 'default',
),
Please help me with this.
Figured it out. Did these two things -
$ sudo vim /etc/default/beanstalkd
> START yes # uncomment
$ sudo service beanstalkd start
# Alternatively: /etc/init.d/beanstalkd start
Got it from this link - http://fideloper.com/ubuntu-beanstalkd-and-laravel4
The answer by #halkujabra is correct. The error is because beanstalkd is not running. To fix it you just have to start beanstalkd. For MacOS use this script https://gist.github.com/finger-berlin/1942295 to to that.
Download and save it as script.sh
Make it executable: chmod a+x script.sh
Run ./script.sh start

Categories