I'm developing an application with laravel 5.2, I'm trying to connect a remote database, if I do that in the command line I have to write this:
mysql -u root -p -h 'remote_ip' --skip-secure-auth
if I write that in the command line I don't have any problem.
But now I have to do the connection with laravel, how should I write the options?
--skip-secure-auth on my database.php file
'estudiantesdb' => [
'driver' => 'mysql',
'host' => 'remote_ip',
'port' => '3306',
'database' => 'estudiantesdb',
'username' => 'root',
'password' => 'root_password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
mysql -u root -p -h 'remote_ip' --skip-secure-auth
Here, your password is empty. So in configurations too you have to set password as empty:
'password' => ''
Your config looks like this:
'estudiantesdb' => [
'driver' => 'mysql',
'host' => 'remote_ip',
'port' => '3306',
'database' => 'estudiantesdb',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
Related
I wish to hide critical server access information as Laravel recommended, so I have setup config/database.php and .env as below, but these couldn't connect each one. which line was wrong?
Laravel version: 5.4
config/database.php
'connections' => [
'master_db' => [
'driver' => 'mysql',
'host' => env('DB_HOST_MASTER', '127.0.0.1'),
'port' => env('DB_PORT_MASTER', '3306'),
'database' => env('DB_DATABASE_MASTER'),
'username' => env('DB_USERNAME_MASTER'),
'password' => env('DB_PASSWORD_MASTER'),
'unix_socket' => env('DB_SOCKET_MASTER'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'slave_db' => [
'driver' => 'mysql',
'host' => env('DB_HOST_SLAVE', '192.1.0.2'),
'port' => env('DB_PORT_SLAVE', '3306'),
'database' => env('DB_DATABASE_SLAVE'),
'username' => env('DB_USERNAME_SLAVE'),
'password' => env('DB_PASSWORD_SLAVE'),
'unix_socket' => env('DB_SOCKET_SLAVE'),
'charset' => 'latin1',
'collation' => 'latin1_general_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
]
.env
DB_CONNECTION=master_db
DB_HOST_MASTER=127.0.0.1
DB_PORT_MASTER=3306
DB_DATABASE_MASTER=db_master
DB_USERNAME_MASTER=user_master
DB_PASSWORD_MASTER=passwordmaster
DB_CONNECTION=slave_db
DB_HOST_SLAVE=192.1.0.2
DB_PORT_SLAVE=3307
DB_DATABASE_SLAVE=db_slave
DB_USERNAME_SLAVE=user_slave
DB_PASSWORD_SLAVE=passwordslave
If you want use like upper config/database.php file style, then .env file recommend as below as simple. :)
DB_CONNECTION=master_db
DB_HOST_MASTER=127.0.0.1
DB_PORT_MASTER=3306
DB_DATABASE_MASTER=db_master
DB_USERNAME_MASTER=user_master
DB_PASSWORD_MASTER=passwordmaster
// DB_CONNECTION=slave_db <------ no needed this line
DB_HOST_SLAVE=192.1.0.2
DB_PORT_SLAVE=3307
DB_DATABASE_SLAVE=db_slave
DB_USERNAME_SLAVE=user_slave
DB_PASSWORD_SLAVE=passwordslave
I have a local Laravel site installed and it was working fine and now it's throwing a DB connection error. Here is the function it's failing on:
My local database is set up, the username is root and there is no password (or the password is blank) so that should work fine.
I'm getting an error:
PDOException
SQLSTATE[HY000] [1045] Access denied for user ''#'localhost' (using password: NO)
It doesn't make sense because it was working and as you can see, it's not even recognizing the 'root' username anymore.
I have a file named '.env' in the root of my whole installation and it has the right credentials as far as I know:
DB_HOST=localhost
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=
And those are all correct.
Any ideas why this would stop working all of a sudden?
For more data, my app/config/database.php file is:
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'my_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'my_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'my_db',
'username' => 'root',
'password' => '',
'prefix' => '',
),
),
Just change the root password and do appropriate action in .env and database.php
Please dont use root without having password.after all this do a mysql restart and an apache restart.all the best. :)
So I have 3 mysql DB's defined in my database.php file. I had 2 for the longest time and everything worked fine. I have one titled mysql, one mysql1 and one mysql2. The first 2 work fine, I copied the second one, changed the name to mysql2 and added the proper parameters but it still always says "Database mysql1 not configured. Is there some sort of cache? or somewhere else I have to define it? I am calling the DB through a model like this.
protected $connection = 'mysql1';
This is my database.php for clarification
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql1' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
This was fixed by using the command
php artisan config:cache
I submitted a Laravel issue request to fix it since that absolutely should not be required to change the DB connections variable.
If you can not delete cache with php artisan config:cache because it throws error after you run it:
[InvalidArgumentException]
Database [] not configured
And if you are sure that your connections parameters are good then try to manually delete bootstrap cache config files which are placed in app/bootstrap/cache folder.
I configured the way I separate the database to read and write.
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
But now, I want to change the database driver in the session and I want to be able to read and write operations to a single host of the session value.
How can I only run on a single host in the session process without disturbing the above structure?
I think I found a solution.
We are adding a new connection into the first database.php connections.
'session' => array(
'host' => "HOST_NAME",
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
then we give the name of the connection a little bit before we install the connection value in session.php file.
'connection' => "session",
Thats it.
I am developing a project using laravel 4 framework. In my database.php file I get the following error:
Undefined index: driver
And my connection is as following:
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'mysql2' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
);
I am also using environments in order to set different mysql connections. What is wrong with the code?
In my case it was because I deleted
'default' => 'mysql',
by mistake from app/config/database.php.
Moving the 'driver' key up a level should fix the issue.
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'driver' => 'mysql'
),
Most of the other params that are shared can me moved as well
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
),
'write' => array(
'host' => 'localhost',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
This happens to me because I deleted
'default' =>env('DB_CONNECTION', 'mysql'),
From app/config/database.php. It's necesary have a default connection
Go to root directory .env
This values are taken first.
If you have multiple different connections (for example to multiple databases on the same host) and it doesn't make sense to set a default connection, you can specify a connection in the Model class.
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Character extends Model {
protected $connection = 'my_db_connection_name_here';
}
This would be on the of the connections defined in config/database.php.
I solved my problem by adding some permissions. My .env file was exists but wasn't readable. I just added 755 permission.