Related
I'm currently on a fresh install of Laravel 6 and trying to connect it to my Microsoft SQL Server, and I am serving it locally using Laragon.
I have downloaded the ODBC driver for SQL Server.
My database.php file contains:
'default' => env('DB_CONNECTION', 'sqlsrv'),
...
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'odbc' => true, // I added this line
'odbc_datasource_name' => '{SQL Server}', // I added this line
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
And my .env contains the valid login credentials. To test that I can connect to the DB, I did a check in welcome.blade.php:
#php
if (DB::connection()->getDatabaseName())
{
echo "connected successfully to database ".DB::connection()->getDatabaseName();
}
#endphp
Which echos onto my page as:
connected successfully to database testDatabase
I have gone into Tinker to see if I can get a Collection of all Employees from my model, i.e. Carer.php:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Carer extends Model
{
protected $table = "dbo.carer";
protected $primaryKey = 'CarerID';
}
...
$carer = App\Carer::all();
Which returns:
Illuminate/Database/QueryException with message 'could not find driver (SQL: select * from [dbo].[carer])'
Does this mean my check for connecting to the database is faulty if I cannot find a driver when attempting to retrieve model data? Do I have to do something specific in Laragon to get it to work for SQL Server?
Thank you.
UPDATE: Following the instructions from here and here, I have managed to add the PDO drivers to my php.ini file and get it running with Laragon. When I print out phpinfo() I can now see the sql servers.
The error I am now getting is:
Illuminate/Database/QueryException with message 'SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it.
(SQL: select top 1 * from [dbo].[carer])'
Answering this in case anybody else gets stuck on the same point.
My Microsoft SQL Server didn't have connection over TCP/IP enabled.
You want to:
Open SQL Server Configuration Manager
Under SQL Server Network Configuration, select Protocols for MSSQLSERVER
Right click on TCP/IP and click Enable
Restart SQL Server (MSSQLSERVER)
Source
Attempting to connect to an Oracle database with yajra/laravel-oci8, below is the configuration. Note that I've also installed the instant client 11g corresponding to the bit version of Windows Server. I've ensured that the 11g ext is enabled in the php.ini.
Spent 4 days on this now, does anybody have any idea what's going on?
I don't even get an error message, just a stack dump which is attached.
'oracle' => [
'driver' => 'oracle',
'tns' => 'LISTENER',
'host' => '192.168.39.73',
'port' => env('DB_PORT', '1521'),
'database' => 'QORA',
'username' => 'ETEST',
'password' => 'ETEST',
'charset' => env('DB_CHARSET', 'AL32UTF8'),
'prefix' => env('DB_PREFIX', ''),
'prefix_schema' => env('DB_SCHEMA_PREFIX', ''),
],
Your error messages are showing you are trying to connect on port 3306, which is the standard MySQL port. According to your config, you're looking at the DB_PORT value from the .env file, with a backup of 1521 if that doesn't exist.
My guess is that your .env file has the following line:
DB_PORT=3306
You either need to comment or remove that line, or change 3306 to 1521.
I have put PushChatServer dir in htdocs folder and create database puschat try to run #"http://localhost/PushChatServer/api/test/database.php"
Then I got following exception.
I want do same thing to explain this link http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2
I have done all that but I got this exception
Could not connect to the database. Reason: exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Applications/MAMP/htdocs/PushChatServer/api/test/database.php:17 Stack trace: #0 /Applications/MAMP/htdocs/PushChatServer/api/test/database.php(17): PDO->__construct('mysql:host=loca...', 'root', 'root', Array) #1 {main}
You need to change host from localhost to 127.0.0.1
Laravel 4: In your app/config/database.php try changing host from localhost to 127.0.0.1
Laravel 5: In the .env file, change DB_HOST from localhost to 127.0.0.1
Source: PDOException SQLSTATE[HY000] [2002] No such file or directory
Quick test (run in shell):
php -r "new PDO('mysql:host=localhost;dbname=test', 'username', 'password');"
SQLSTATE[HY000] [2002] No such file or directory means php cannot find the mysql.default_socket file. Fix it by modifying php.ini file. On Mac it is mysql.default_socket = /tmp/mysql.sock (See MySQL connection not working: 2002 No such file or directory)
SQLSTATE[HY000] [1044] Access denied for user 'username'#'localhost' CONGRATULATION! You have the correct mysql.default_socket setting now. Fix your dbname/username/password.
Also see Error on creating connection to PDO in PHP
I had the same error using PHP 5.6.30 (macOS Sierra) with the simple PDO code like:
$pdo = new PDO('mysql:host=localhost;dbname=db_php', 'root', '');
And I received the same error message. To fix, I changed "localhost" for IP (loopback) "127.0.0.1" in my code:
$pdo = new PDO('mysql:host=127.0.0.1;dbname=db_php', 'root', '');
To test the connection:
$ php -r "new PDO('mysql:host=127.0.0.1;dbname=db_php', 'root', '');"
This work's for me!
Restart your database and local web server:
sudo service mysqld restart
It should work!
I'm not sure if this will work for you; but I use CakePHP, and I get this error whenever I forget to put the port number at the end of the 'host'.
Hope this helps!
Before
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'mylogin',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_TEST_URL', null),
]
After
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost:8080',
'username' => 'root',
'password' => 'root',
'database' => 'mylogin',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_TEST_URL', null),
]
PDO treats localhost very particularly:
From http://php.net/manual/en/ref.pdo-mysql.connection.php:
Note: Unix only: When the host name is set to "localhost", then the
connection to the server is made thru a domain socket. If PDO_MYSQL is
compiled against libmysqlclient then the location of the socket file
is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled
against mysqlnd a default socket can be set thru the
pdo_mysql.default_socket setting.
That why PDO and mysql_connect will give different behavior for localhost.
So, if you want to use a TCP connection with PDO, never use localhost but 127.0.0.1.
I had the same error for Mysql PDO, this code works for me!
<?php
$dsn = 'mysql:host=127.0.0.1;dbname=testdb';
$username = 'username';
$password = 'password';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
?>
got this code from : http://php.net/manual/en/ref.pdo-mysql.connection.php
Run the following command in shell, I think it will help.
php artisan cache:clear
php artisan config:clear
php artisan view:clear
On Mac OSX/MAMP you may find a mysql.sock.lock file in your /Applications/MAMP/tmp/mysql folder.
You can safely remove the file mysql.sock.lock and you should be in good shape.
Just ran into this same issue and the problem is the password. In the tutorial the author lists the password as:
"d]682\#%yl1nb3"
So as per the suggestion given by #Marki555, I looked in the config file - api_config.php. and the password listed there is:
"d]682\#%yI1nb3"
The upper case 'I' is what caused the issue because the password you set for user on the db has the password with the lowercase l but it really is looking for the uppercase I. After I changed the pushchat user's password to have an uppercase i, it worked!
I am trying to set up migrations for the first time and when I run
php artisan migrate:install
from within the root folder of my project I get the following SQLSTATE error:
SQLSTATE[HY000] [2002] No such file or directory
I have tested running mysql to make sure it is working and referenced and I have ran php artisan help:commands to make sure artisan is working (it is).
The website itself is functioning and reading from the database fine.
This may be an issue with the sockets if you're using MAMP or XAMPP.
From http://forums.laravel.io/viewtopic.php?id=980...
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'database',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
'prefix' => '',
),
),
You can pass an optional "unix_socket" to the array and specify the MAMP socket instead of the default location.
Also, try and change 'host' => 'localhost', to 'host'=> '127.0.0.1', because unix does not recognise 'localhost'
I am currently reading "Beginning CakePHP:From Novice to Professional" by David Golding. At one point I have to use the CLI-command "cake bake", I get the welcome-screen but when I try to bake e.g. a Controller I get the following error messages:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2) in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 117
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 122
Warning: mysql_get_server_info(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 130
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 154
Error: Your database does not have any tables.
I suspect that the error-messages has to do with php trying to access the wrong mysql-socket, namely the default osx mysql-socket - instead of the one that MAMP uses. Hence I change my database configurations to connect to the UNIX mysql-socket (:/Applications/MAMP/tmp/mysql/mysql.sock):
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'connect' => 'mysql_connect',
'persistent' => false,
'host' =>':/Applications/MAMP/tmp/mysql/mysql.sock', // UNIX MySQL-socket
'login' => 'my_user',
'password' => 'my_pass',
'database' => 'blog',
'prefix' => '',
);
}
But I get the same error-messages with the new socket:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock:3306' (2) in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 117
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 122
Warning: mysql_get_server_info(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 130
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 154
Error: Your database does not have any tables.
Also, even though I use the UNIX-socket that MAMP show on it's welcome-screen, CakePHP loses the database-connection, when using this socket instead of localhost.
Any ideas on how I can get bake to work?
-- Edit 1 --
Thank you guys for helping me out! :)
I have a problem figuring out where in my.cnf to edit to get MySQL to listen to TCP/IP request. The only paragraph I can find where TCP/IP is mentioned is the following:
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
That allows me to turn off TCP/IP completely, which is the opposite of my intention. I don't know how to go about what you suggest, if you could be more elaborate it would be great. I am a total n00b on these matters :S
Reg. connecting to a local socket: I removed the leading colon in the host-parameter, same result.
I find the solution to this problem :
Add a socket config in the cakephp app/config/database.php file
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '/Applications/MAMP/tmp/mysql/mysql.sock', // here is the key !
'login' => 'you',
'password' => 'yourpass',
'database' => 'yourdb',
'prefix' => '',
);
From the error, it looks like it's trying to connect to an actual IP address and not a UNIX socket, look:
'/Applications/MAMP/tmp/mysql/mysql.sock:3306'
It's appending a port to the socket, which is wrong.
So, I'd first try to configure MySQL to listen to TCP/IP requests (edit the proper section in my.cnf) and try providing 127.0.0.1 instead of the socket.
In case you won't scroll down:
To fix it at CakePHP level, change host on database.php to 'localhost' and add a port directive with its value set to the socket name '/Applications/MAMP/tmp/mysql/mysql.sock'
For anyone running into this problem when using CakePHP 2.0: for me the above database configuration files didn't do the trick. Found the 'unix_socket' property though, that worked for me:
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'unix_socket' => '/tmp/mysql.sock',
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx',
'encoding' => 'UTF8',
'prefix' => ''
);
}
I had the same problem, when using MAMP and the Cake CLI. I'm running CakePHP 1.1xxx and MAMP 1.7.
The problem is, that the MySQL socket can't be found :D
Open Terminal and enter the following:
my-macbook:~ chris$ php -i | grep mysql.default_socket
mysql.default_socket => no value => no value
my-macbook:~ chris$ php -i -c /Applications/MAMP/conf/php5 | grep mysql.default_socket
mysql.default_socket => /Applications/MAMP/tmp/mysql/mysql.sock => /Applications/MAMP/tmp/mysql/mysql.sock
The catch is that without explicitly giving the php binary the path to its (read MAMP's) config file, the mysql.default_socket is not set.
Using that I did not need to change my database configuration whatsoever.
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
I had this exact same problem with mamp, and fixed it with the above command. I think you have to run this command every time you restart your computer. Maybe there is a better way to do it, but I use this with clix.app, so it is usually pretty fast. Also, change your host to localhost.
For me, I forgot to set the port on the host, since I was not using the default MySQL port in MAMP.
i.e. If your MySQL port is 8889, set host to localhost:8889.
Yeah had a similar issue, I think pointing to the the socket:portno as Vinko suggested might work - however if you use an ip address / localhost you'll be fine.
class DATABASE_CONFIG
{
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'account',
'password' => 'password',
'database' => 'database',
'prefix' => '',
'port' => '/var/mysql/mysql.sock'
);
}
This worked for me:
class DATABASE_CONFIG
{
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'account',
'password' => 'password',
'database' => 'database',
'prefix' => '',
'port' => '/Applications/MAMP/tmp/mysql/mysql.sock'
);
}
Try configuring your firewall... That was the case for me!
You can also make a symbolic link, bake is looking for mysql.sock in /tmp
like this:
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
Cheers, Ebbot.