Laravel/MySQL database connection - php

I have an unusual problem with my Laravel 4.2 project connecting to MySQL database. This is my database.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database_name',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
I use XAMPP for Ubuntu.
First, I've created a database in phpMyAdmin. When I tried to run php artisan migrate, it gave me an error of unknown database 'database_name'. I thought it was silly since I've just created a database in phpMyAdmin. Then I tried creating a database through the command line and tried running the migration again. It worked. Tables were created under the database I specified when I run show tables in the command line. It seems to me that the Laravel project is connected to a different mysql server than the phpMyAdmin. Is that even possible? How can I solve this problem?
I've been trying to solve this for hours but still, no luck. Hope someone can help me with my problem.

I solved the problem by following the solution in the link provided by Bulk. I just have to run sudo /opt/lampp/bin/php artisan migrate instead of the plain php artisan migrate. With this, artisan will use the MySQL version provided by XAMPP.

Related

PDOException with message 'could not find driver' Error for Laravel and Postgresql

First of all, the problem is about Laravel not postgresql or PHP. I can connect postgresql with a simple PHP file. But Laravel can't do it somehow.
When I try to connect postgresql server in my computer with laravel I get "PDOException with message 'could not find driver'" error. I am getting this error when I run DB::connection()->getPdo(); command at artisan tinker.
If I run php artisan migrate command, the error is Illuminate\Database\QueryException : could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')
My configuration is below:
Windows 10
Wamp Server 3.1.4
Apache 2.4.35
PHP 7.2.10
Laravel Framework 6.0.3
Related lines of Laravel .env file:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel_dnm1
DB_USERNAME=postgres
DB_PASSWORD=mrd.BE.265
Related lines of Laravel database.php file:
'default' => env('DB_CONNECTION', 'pgsql'),
...
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
When I run print_r(PDO::getAvailableDrivers()); at my server I get below output:
Array ( [0] => mysql [1] => pgsql [2] => sqlite )
Related lines of php info is below:
NOTE: There is no problem when I use mysql instead of postgresql.
NOTE2: I can connect the DB when I use regular PHP. Only Laravel gives this error.
install postgresql
sudo apt-get install php-pgsql
then uncomment pgsql and pdo-pgsql extensions in etc/php/$PHP_VERSION/apache2/php.ini file
then restart apache2
Verify your PHP version with php -v
Install php7.2-pgsql when needed.
I solved the issue. It is a bug at WAMP I think. When I edit php.ini from WAMP's menu it opens a different php.ini than active PHP version's. So I opened php.ini from file system and edited it from there.
There are different php version installed on your WAMP SERVER. when you click on WAMP icon it will show that pgsql and pdo_pgsql are active but for different php version. You need to check your php version using php -v command and then go to the WAMP folder and find that php version and edit php.ini file and enable pgsql extensions there.

Laravel 5 + PostgreSQL: "Database [postgres] not configured." Error

I'm trying to get started with Laravel + PostgreSQL and been following the database tutorial.
Unfortunately, after updating the database configuration file and running php artisan migrate, the following error appears:
[InvalidArgumentException]
Database [postgres] not configured.
What puzzles me is that I didn't specify the "postgres" database in the configuration, but another database I set through cPanel, say "example_database".
Here's some relevant parts of my /config/database.php configuration:
'default' => env('DB_CONNECTION', 'postgres')
And inside the connections array of the same file:
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'example_database'), // This seems to be ignored
'username' => env('DB_USERNAME', 'example_username'),
'password' => env('DB_PASSWORD', 'example_password'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public'
],
The actual database credentials I'm using are working perfectly on my SQL Workbench client, so this seems to be a Laravel config problem. Any ideas? I have searched around for at least an hour to no avail.
You have to enter your configuration in the .env file.
The configuration you made will only be loaded if they are not already defined in .env
You need to use pgsql instead of postgres.
DB_CONNECTION=pgsql
DB_HOST=localhost
DB_DATABASE=DB_NAME
DB_USERNAME=USER
DB_PASSWORD=PW
Laravel sometimes caches your configurations. If you run into this problem while everything looks alright try running
php artisan config:cache
I know this is an old question and it already has an answer; however, here is an small explanation why:
If you check your database.php in the config directory, you will see that you have few connections types, including pgsql. So, the key have to match to the DB_CONNECTION in .env file. You can definitely replace pqsql connection key with postgres, and it will work on the same way.
However, I would recommend replacing the value DB_CONNECTION, instead of modifying the config.
DB_CONNECTION=pgsql

Laravel: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

I am using schema builder to create a Mysql table. I am using Ubuntu 14.04.
My connection for Mysql in config/database.php is :
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'firstapp'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
And Code for creating table with schema builder on routes.php
Route::get('/', function () {
Schema::create('art', function($newtable)
{
$newtable->increments('id');
$newtable->string('artists');
$newtable->string('title',500);
$newtable->text('discription');
$newtable->date('created');
$newtable->date('exhibition_date');
$newtable->timestamps();
});
return view('welcome');
});
Thanks
As far a as i know , you must create a migration file using
php artisan migrate:make yourtable
Then yo must go to database folder you will find a new migration file created , then you must open it , and fill it with your schema , once you finish , then run migration
php artisan migrate
And that's all you will see your new table created.
Migrations laravel 5
First i was using xampp mysql and this was also started /etc/init.d/mysql from terminal, Since i am using Ubuntu 14.04.
I solved the problem which i posted above then another problem came up Saying SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES).
I changed the .env file which is placed in the root directory of laravel and i changed the Database Name name and password which i was using for mysql.
It solved that problem too and i am up and running.

Laravel Artisan Migrate connection refused

My website works, its clearly accessing the database. I can log in and i can create records, etc...
Ive created a new migration, and I'm trying to insert it, but when i run
php artisan migrate
i get the error
[PDOException]
SQLSTATE[HY000] [2002] Connection Refused
My database config has the passwords hidden in the environment so my config looks like this
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOSTNAME'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
my environment variables are loading correctly. What am i missing?
I figured it out. Apparently the environment that artisan was seeing was different from the environment i wanted it to see, so it was using the wrong host
php artisan migrate --env=production

Changing database name in Laravel/Homestead

I started learning Laravel just an hour ago and following tutorials.
My settings for database are as follow (File: app/config/database.php):
'default' => 'mysql',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laraveltest',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In mySQL on homestead, I already created laraveltest database. I also created migration file in database/migration directory with following code:
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('name');
$table->timestamps();
});
}
and migration commands were:
vagrant#homestead:~/Code/Laravel$ php artisan migrate
Migration table created successfully.
Migrated: 2014_08_14_195103_create_users_table
As displayed, table users created but in homestead database, not in laraveltest database. Can someone please tell where I went wrong and how to force laravel to use laraveltest database?
Edit after first comment
File: bootstrap/start.php
$env = $app->detectEnvironment(array(
'local' => array('homestead'),
));
It is most likely an issue with environments. Check that you have a database configuration file in app/config/local/, and check that it has the proper settings.
If anyone finds this looking for the answer for Laravel 5 or later: It's the same as #camroncade says, but in this case it's your .env file in the project root you want to look at.
I am currently dealing with this issue as well. Changed the .env file numerous times as well as the config.php file. Nothing seemed to work.
After having done some deep digging into the framework I have found an acceptable temporary workaround to the problem that does not conflict with ANY of Laravel 5.0.2's Exceptions.
NOTE: I'm currently using Ampps 3.4 with php 7.0.2 phpmyadmin 4.4.15.1 on a Windows 8.1 OS.
Inside of the file "ConnectionFactory.php" (located at "laravel\vendor\laravel\framework\src\Illuminate\Database\Connectors"),
scroll down to the public function make located at line 41.
Within this function you can do the following after this statement $config = $this->parseConfig($config, $name); which should be on line 43:
$config['database'] = 'Insert the name of your database here';
This change will allow Laravel to continue with its normal process having no negative effect anywhere with the framework or your project. That is until this bug is fixed by the makers of Laravel or the community.
Till then happy coding! =)

Categories