Laravel 'could not find driver (SQL: insert into...' [duplicate] - mysql

This question already has answers here:
Laravel 5 PDOException Could Not Find Driver [duplicate]
(11 answers)
Closed 2 years ago.
I'm trying to set up a development environment after not having done web dev in over a year. I've installed php 7.2.7 on my computer, then installed composer and WAMP.
I'm using php artisan serve to set up a local server. I'm trying to create a new user in my database's 'users' table, using the following code in my web.php (routes) file.
Route::get('/new', function(){
User::create([
'password' => Hash::make('anything'),
'firstname' => 'Nick',
'lastname' => 'xyz',
'email' => 'xyz#ph.com',
'roleflag' => 0
]);
});
But am getting the following error:
This seems to be a pretty common error, and I have found help on other stackoverflow/laracasts posts such as:
Laravel: Error [PDOException]: Could not Find Driver in PostgreSQL
and
https://laracasts.com/discuss/channels/general-discussion/cant-connect-to-sql-server-could-not-find-driver
I've thus uncommented the two lines from my php.ini file, changed my .env and config/database.php file to have appropriate settings/connection values, etc. but still receive this error.
Relevant config.php code:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('venturebreeder', 'forge'),
'username' => env('root', 'forge'),
'password' => env('', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
I'm a bit slower at troubleshooting since it has been a while since I've done this -- can anyone see what I'm doing wrong? Help much appreciated.

What lines did you uncomment in php.ini file?
Do you have the pdo_mysql extension installed?
Remove the ; from ;extension=pdo_mysql.so and restart your WAMP server
Source: https://stackoverflow.com/a/35240511/6385459

Related

Connect external Database to Laravel Vapor

I need a little help connecting an external MySQL database to Laravel Vapor. The database is located on a Hetzner Server and it seems like there is a failure using a tls encrypted connection:
==> Executing Function...
Status Code: 1
Output:
In Connection.php line 712:
SQLSTATE[HY000] [2002] (SQL: SELECT * FROM KURSE_planung )
In Exception.php line 18:
SQLSTATE[HY000] [2002]
In PDOConnection.php line 40:
SQLSTATE[HY000] [2002]
In PDOConnection.php line 40:
PDO::__construct(): SSL operation failed with code 1. OpenSSL Error message
s:
error:1416F086:SSL routines:tls_process_server_certificate:certificate veri
fy failed
I already tried to disable SSL by using the following params in the database URL without any luck:
MYSQL_DATABASE_URL=mysql://username:password#sql168.your-server.de/databasename?charset=utf8mb4&sslmode=disabled&ssl-mode=disabled&useSSL=false
Does anyone know how to disable TLS when connecting to the database or what else I can do about it?
Edit:
I managed to connect to the database. My Hoster Hetzner provides a certificate which needs to be provided to the connection configuration in database.php:
'mysql' => [
'driver' => 'mysql',
'url' => env('MYSQL_DATABASE_URL'),
'host' => env('MYSQL_DB_HOST', '127.0.0.1'),
'port' => env('MYSQL_DB_PORT', '3306'),
'database' => env('MYSQL_DB_DATABASE', 'forge'),
'username' => env('MYSQL_DB_USERNAME', 'forge'),
'password' => env('MYSQL_DB_PASSWORD', ''),
'unix_socket' => env('MYSQL_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => base_path(env('MYSQL_ATTR_SSL_CA')),
]) : [],
],
Then in the .env file one needs to set the correct path to the certificate using MYSQL_ATTR_SSL_CA.
One question remains: does anyone know how to disable SSL for MySQL on Vapor?
Best regards
Clemens
I don't know if this might help. But In my case Laravel Vapor was hosted with RedHatOS, then I defined the env with the following:
MYSQL_ATTR_SSL_CA=/etc/pki/tls/certs/ca-bundle.crt
In my case, the database is hosted on planetscale this configration a specified on the documentation

Laravel 8 - How to solve Connection timed out while using remote MYSQL database?

In one of my Laravel 8 applications, I need to use two databases where one is a remote database. In my local development environment, the remote database is working fine (I can fetch data), but the problem arises only when I host the application in production. Every time it responds connection time out even for a simple query like SELECT * FROM users WHERE email = 'my_target_email_address'
Here is my .env file code:
# Local DB
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=bint_gt
DB_USERNAME=root
DB_PASSWORD=
# REMOTE DB
REMOTE_DB_CONNECTION=mysql
REMOTE_DB_HOST=REMOTE_DB_HOST
REMOTE_DB_PORT=3306
REMOTE_DB_DATABASE=REMOTE_DB
REMOTE_DB_USERNAME=REMOTE_DB_USER
REMOTE_DB_PASSWORD=REMOTE_DB_PASSWORD
Here is the updated code for the remote database in the config/database.php file:
'remote_mysql' => [
'driver' => 'mysql',
'url' => env('MFO_DATABASE_URL'),
'host' => env('MFO_DB_HOST'),
'port' => env('MFO_DB_PORT', '3306'),
'database' => env('MFO_DB_DATABASE', 'forge'),
'username' => env('MFO_DB_USERNAME', 'forge'),
'password' => env('MFO_DB_PASSWORD', ''),
'unix_socket' => env('MFO_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Finally, run the following MySQL query to fetch data:
$userEmail = Auth::user()->email;
$userInfo = DB::connection('remote_mysql')->select("SELECT * FROM users WHERE user_email='$userEmail'");
if( !empty($userInfo) ) {
$uid = $userInfo[0]->uid;
# do rest of the work
} else {
# return JSON response of user not found
}
Everything works fine in my local development environment (XAMPP), although the speed is a little bit slow, it does not work on the production server.
N.B: I am using Plesk rather cPanel.
Can anyone help me to figure out how to fix the problem?

Laravel 5: "Base table or view not found" Table X "doesn't exist" - laravel is confusing virtual hosts

Getting this error message RANDOMLY while nav'ing to my view:
QueryException in Connection.php line 636:SQLSTATE[42S02]: Base table or view not found: 1146 Table 'salesspacetv.devices' doesn't exist (SQL: select count(*) as aggregate from devices inner join statustypes on devices.status = statustypes.num inner join aggservers on devices.aggserver_num = aggservers.num left join tickets on devices.id = tickets.device_id group by devices.id) in Connection.php line 636
My server uses multiple virtual hosts, defined in c:\apache24\conf\extra\httpd-vhosts.conf.
"c1.[ourcompanyname].net" is the name of the site whose view I'm nav'ing to.
"salesspacetv.[ourcompanyname].net" is another virtual host (running laravel) on our server.
The word "salesspacetv" absolutely does not exist anywhere in c1's code.
"devices" IS a table used by the c1 site, and there is no "devices" table used by the salesspacetv site.
It seems like laravel's base code is making use somewhere of what it thinks is the subdomain name and that Apache (fyi... running on Windows, on this server) is somehow not getting the correct subdomain name to laravel.
Again, this is a random error. If I simply refresh the page, the error goes away. Also note, whether important or not, that this view is using pagination. I don't imagine that has any importance, but I figured it was worth mentioning.
The databases are MySQL databases.
Lastly, the c1 virtual host definition does actually appear before the salesspacetv virtual host definition in httpd-vhosts.conf.
Thanks.
I believe I ran into this error in the past when I was dealing with multiple projects in my local environment.
Let me get this straight, you are using 1 project then whenever it is involving a database table call it sometimes references to a table or column name that the current project does not contain but contains in another project?
If that is what's happening to you, how I fixed it on my end was name your database environments (.env file) different per project.
#1 .env Method
.env
Project 1:
DB_PROJECT1_HOST=0.0.0.0
DB_PROJECT1_DATABASE=dbname
DB_PROJECT1_USERNAME=dbuser
DB_PROJECT1_PASSWORD=dbpass
Project 2:
DB_PROJECT2_HOST=0.0.0.0
DB_PROJECT2_DATABASE=dbname
DB_PROJECT2_USERNAME=dbuser
DB_PROJECT2_PASSWORD=dbpass
app\config\database.php
Project 1:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_PROJECT1_HOST'),
'database' => env('DB_PROJECT1_DATABASE'),
'username' => env('DB_PROJECT1_USERNAME'),
'password' => env('DB_PROJECT1_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Project 2:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_PROJECT2_HOST'),
'database' => env('DB_PROJECT2_DATABASE'),
'username' => env('DB_PROJECT2_USERNAME'),
'password' => env('DB_PROJECT2_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Hopefully this is the same issue happening to you so you can quickly test this and see if it applies to you.
#2 Alternative (hardcode method suggested by Tezla):
You may also edit the database configuration file (app\config\database.php) and hardcode the database information directly to avoid configuration leaks:
'mysql' => [
'driver' => 'mysql',
'host' => '0.0.0.0',
'database' => 'dbname',
'username' => 'dbuser',
'password' => 'dbpass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]

Laravel 5.1 - Connecting to MySQL Database (MAMP)

There are topics online that are discussing this problem however, I couldn't find any tidy explanation of the problem or any solid answers for the question. What I am trying to achieve is connecting Laravel 5.1 to MySQL Database of MAMP.
In my config>app.php:
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost:8889',
'database' => 'test',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'prefix' => '',
'strict' => false,
],
In my .env:
DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root
I also have .env.example: (which I believe has no functionality)
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
I also have create_users_table.php and create_password_resets_table.php in my database>migrations (even though I did not run any migration:make)
MAMP is directing and running the server successfully as it loads the project on localhost.
Here is my MAMP settings:
And the test database is created (with tables in it which I have previously created and used in my other projects, not Laravel.)
Even though everything seems correct to me, when trying to submit Auth form, I am getting this error:
PDOException in Connector.php line 50:
could not find driver
in Connector.php line 50
at PDO->__construct ('mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=test', 'root', 'root', array('0', '2', '0', false, false)) in Connector.php line 50
at Connector->createConnection('mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=test', array('driver' => 'mysql', 'host' => 'localhost:8889', 'database' => 'test', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, false)) in MySqlConnector.php line 22
and so on...
On mac or unix you have to include the socket path in the configuration database.php file
i.e 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
It was pretty simple for me, I added :8889 to the localhost in the .env file.
DB_HOST=localhost:8889
This is because in the MAMP preferences, :8889 is the default port.
The most important thing for me was defining the UNIX socket. Because I have another MYSQL on my machine - Laravel was trying to connect to a database in that MYSQL process.
Defining the UNIX for the MAMP database to be used worked perfectly. Try adding this to your MYSQL configuration in database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
As far as I am concerned it doesn't make any sense to set in database.php as many of them suggested.
Since this change would be mostly required in the development mode. So the proper way of setting the unix_socket is as below
file: .env
DB_SOCKET='/Applications/MAMP/tmp/mysql/mysql.sock'
By doing the above way already .env is included in .gitignore and won't create any other problem while your project is remotely deployed.
NOTE: I have tested this setting in Laravel 5.7 and above versions
Found my answer. Here is a way to fix it:
Start MAMP
On the top left, go to "MAMP" -> "Preferences"
Go to the "PHP" tab
Tick PHP 5.5.17 (or whatever you have) instead of the one which is ticked by default (5.6.1 -> 5.5.17 with he latest version of MAMP)

lavaral 5 ERROR{ (SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)}

'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database.sqlite'),
'prefix' => '',
],
**JUST CHANGED THE CODE FOR LOCALHOST AND CHANNGED THE NAME TO ROOT AND SET THE PASSWORD OF PHPMYADMIN . **
**WHEN WRITTING THE COMMAND IN XAMPP SHELL ((Php artisan migrate:install)) **
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'password'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'password'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
it is showing error SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)
By default laravel assumes that you will want to have different configurations for different environments. E.g. in a testing environment, you might wish to have a different username and password and in a production environment different. Since laravel has so many configuration files, it quickly becomes a nightmare to manage all those. Hence laravel makes use of PHP's environment variables.
See the docs here.
What is basically says is that if you wish to use the "environment" variables, which laravel uses by default, you have to place all your configurations in the env() method as already mentioned.
If you do not wish to do this, e.g. for simple projects, simply remove the env from your code, like this.
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laravel',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Note that you can mix and match. i.e you can have some of the variables in env and some stand-alone.
So why use env at all?
Lets say your application has 100 testers all placed in different locations. In laravel you have to code approximately 8-10 configuration files. Also you need to version-control those files. So you have two problems at hand:
You do not wish to send all 100 users the same credentials. Also they might use different database, cache server, etc which means that they will have different configurations. So every user has to maintain those 8-10 configuration files by hand.
You do not wish to send these configuration files to version control. Because if you do, whole world will know your API secrets and possibly will take advantage of that (just like password). Also if you look at laravel conf files, you will notice that there are other information such as timezone, debug property, etc that are also in conf files, and you do want to version-control them. So how do you version-control such configuration files and still hide your sensitive information.
The answer is env variables. Laravel uses dotenv whose documentation can be found here. Basically these are variables that live in one file called .env in a key-value pair. E.g.
Sample contents of .env file
APP_DEBUG=false
APP_KEY=ABCDEFGH
...
Once you define your .env file as this, you can get the value using the key as such env('APP_DEBUG').
So this solves the above mentioned problem in following ways:
you keep the .env file to yourself. And you also declare another file called .env.example which is an exact replica of original file except the fact that it contains sample values, not your sensitive values. Then you pass this new example file to everyone. They will replace the sample data with their own sensitive information.
Since you are version-controlling the example file, you can version control all your conf files because they don't contain the secret. The secret is in .env files. All those conf files contain is values like these env('APP_KEY') and the actual value is replaced at run time using your .env file.
Make sure you have to set up right server credentials into .env file on your Laravel project:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myapp_db
DB_USERNAME=root
DB_PASSWORD=pass
Try to clean up artisan cache and restart the artisan,
php artisan config:clear
restart php artisan