Here is the config/database.php file that I modified using this tutorial. It says that I need to attach the $DATABASE_URL=parse_url("mysql://####"); code block above the said php page. (the tutorial is using postgreSQL, while my system needs to use MySQL).
// attached $DATABASE_URL=parse_url("mysql://####") on the topmost part of the page
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => $DATABASE_URL['host'], // edited this line
'port' => $DATABASE_URL['port'], // edited this line
'database' => ltrim($DATABASE_URL['path'], '/'), // edited this line
'username' => $DATABASE_URL['user'], // edited this line
'password' => $DATABASE_URL['pass'], // edited this line
'unix_socket' => env('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'),
]) : [],
],
below is the original mysql code
// 'mysql' => [
// 'driver' => 'mysql',
// 'url' => env('DATABASE_URL'),
// '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' => env('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'),
// ]) : [],
// ],
error ouput when running the system with the said problem
SQLSTATE[HY000] [1045] Access denied for user '{{redacted username}}'#'{{redacted ip address}}' (using password: YES) (SQL: select * from video_links where home_video = true and video_links.deleted_at is null limit 1)
Another problem I encounter while running the migration command in the Heroku CLI is this:
PHP Warning: Undefined array key "port" in /app/config/database.php on line 51
It seems like the $DATABASE_URL=parse_url("mysql://####") is not fetching the port value in its array.
How should I approach this?
You can leave config/database.php unchanged.
And add an ENV var DATABASE_URL:
In the settings or via CLI:
heroku config:set DATABASE_URL=YOUR_URL
Related
I am working on a cms project. I was created a portal and thenn new database connection created. So,there is a table users i wan that if someone create new user in that portal then it will be added to the master database that is another database. So, in laravel how can we make connection with another database and save the data of the portal to our master database table also.
Trying to make a sync connection that if in cms portal if admin create a user then that data must be submitted to the master table.
You can do it like this:
In .env add the following: note that you can customize your connection to another database, like postgres/mariadb etc.. check the documentation here for supported databases: https://laravel.com/docs/9.x/database#introduction
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=user
DB_PASSWORD=password
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=localhost
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=db2
DB_USERNAME_SECOND=user2
DB_PASSWORD_SECOND=password2
In config/database.php: note that you can customize your connection to another database, like postgres/mariadb etc.. check the documentation here for supported databases: https://laravel.com/docs/9.x/database#introduction
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => env('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'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_SECOND', '127.0.0.1'),
'port' => env('DB_PORT_SECOND', '3306'),
'database' => env('DB_DATABASE_SECOND', 'forge'),
'username' => env('DB_USERNAME_SECOND', 'forge'),
'password' => env('DB_PASSWORD_SECOND', ''),
'unix_socket' => env('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'),
]) : [],
],
In the sample model:
class Job extends Model
{
protected $connection = 'mysql2';
}
I want to sync my local database to online database when i click on a button in blade page. I looked online for the solution but did not found anything that worked for me.
I have updated my database .php file I have added local and server database connection credentials.
I have found a solution but that is not completed.
solution that i found
database.php
'local' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('LOCAL_DB_HOST', '127.0.0.1'),
'port' => env('LOCAL_DB_PORT', '3306'),
'database' => env('LOCAL_DB_DATABASE', 'forge'),
'username' => env('LOCAL_DB_USERNAME', 'forge'),
'password' => env('LOCAL_DB_PASSWORD', ''),
'unix_socket' => env('LOCAL_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'),
]) : [],
],
'server' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('SERVER_DB_HOST', '127.0.0.1'),
'port' => env('SERVER_DB_PORT', '3306'),
'database' => env('SERVER_DB_DATABASE', 'forge'),
'username' => env('SERVER_DB_USERNAME', 'forge'),
'password' => env('SERVER_DB_PASSWORD', ''),
'unix_socket' => env('SERVER_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'),
]) : [],
],
controller code
// Connect to local database
return $local_database = DB::connection('local-db')->table('table_name');
// Get table data from production
foreach ($local_database->table('table_name')->get() as $data) {
// Save data to staging database - default db connection
DB::table('table_name')->insert((array) $data);
// This query can do only one time for multiple time we have to do
DB::table('table_name')->insertOrUpdate((array) $data);
// This query also update column only, if you do some changes in
//previous data it will not sync for that i dont know this query is
//correct or not but it worked.
DB::table('table_name')->delete((array) $data);
DB::table('table_name')->insertOrIgnore((array) $data);
}
env
SERVER_DB_CONNECTION=server
SERVER_DB_HOST=127.0.0.1
SERVER_DB_PORT=3306
SERVER_DB_DATABASE=sync_db
SERVER_DB_USERNAME=root
SERVER_DB_PASSWORD=
LOCAL_DB_CONNECTION=local
LOCAL_DB_HOST=127.0.0.1
LOCAL_DB_PORT=3306
LOCAL_DB_DATABASE=sync_db
LOCAL_DB_USERNAME=root
LOCAL_DB_PASSWORD=
I'm currently using the spatie laravel-backup for backup my database.
But when I execute the command
php artisan backup:run
I get this error :
Backup failed because The dump process failed with exitcode 1 : General error : The system cannot find the path specified.
After my research, I tried to change my dump_binary_path to 'D:\xampp\mysql\backup\mysql' but it still not working. Therefore I'm thinking that I might have entered the wrong path .. is there a way to find the path?
Here is my code in config/database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'dump' => [
'dump_binary_path' => 'D:\xampp\mysql\bin',
'use_single_transaction',
'timeout' => 60 * 5, // 5 minute timeout
],
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Any answer and suggestion are higher appreciated
I have same problem. But I solve it from this step. you can also try.
follow this step
https://spatie.be/docs/laravel-backup/v5/installation-and-setup#dumping-the-database
then add
'dump' => [
'dump_binary_path' => 'F:\xampp\mysql\bin',
],
Then click your comment link.
I have a database which runs on local that I created with TablePlus. I can access it without any problem. I tested with Insomnia and I can get the info I need. I search how to connect to a second database that is live. I changed my .env file and my database.php to create another access to the second database. But I have an error message:
SQLSTATE[HY000][1045] Access denied for user 'root'#'host' (using passowrd: YES)
where host is the name I found in PHP MyAdmin in the variables tab.
How can I acccess the second DB ? What am I doing wrong ? Do I need to change the env('DATABASE_URL') for the second connection in database.php ?
.env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=first_db
DB_USERNAME=root
DB_PASSWORD=root
DB_SECOND_CONNECTION=mysql2
DB_SECOND_HOST=host //(again, PHP MyAdmin -> variables tab)
DB_SECOND_PORT=3306
DB_SECOND_DATABASE=second_db
DB_SECOND_USERNAME=*username*
DB_SECOND_PASSWORD=*password*
database.php:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'first_db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'unix_socket' => env('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'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_SECOND_HOST', 'host'), //(again, PHP MyAdmin -> variables tab)
'port' => env('DB_SECOND_PORT', '3306'),
'database' => env('DB_SECOND_DATABASE', 'secondd_db'),
'username' => env('DB_SECOND_USERNAME', 'username'),
'password' => env('DB_SECOND_PASSWORD', 'password'),
'unix_socket' => env('DB_SECOND_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'),
]) : [],
],
],
As suggested by #Collin above, the root user I was using didn't have the permissions. Creating a new user with all privileges in PHP MyAdmin solved the issue.
If you want to check if the database connection is actually accesable you could try the following.
First clear your config using the following command:
php artisan config:clear
Then check if the connection is accesable using the following code:
try {
DB::connection('mysql2')->getPdo();
} catch (\Exception $e) {
die("Could not connect to the database. Please check your configuration. error:" . $e );
}
If that doesn't work check if the user has the right permissions.
I am struggling in connecting Laravel app with MYSQL database. I can connect to database over MySQL Workbench and also with PDO connection custom string but with Laravel I cannot. Not sure what is wrong.
Custom connection string: It works file is inside laravel app. I put this just for checking
$conn = mysqli_init();
mysqli_ssl_set($conn,NULL,NULL, "BaltimoreCyberTrustRoot.crt.pem", NULL, NULL) ;
mysqli_real_connect($conn, 'sever.host', 'server-username', 'server-password', 'database', 3306, MYSQLI_CLIENT_SSL);
if (mysqli_connect_errno($conn)) {
die('Failed to connect to MySQL: '.mysqli_connect_error());
}
else{
echo "Connected";
}
Then I added in .env file following, beside defining username password host and db name.
BaltimoreCyberTrustRoot.crt.pem file is on same path as .env file
MYSQL_SSL_KEY=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CERT=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CA=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CIPHER=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL=true
In config/database.php looks like this
'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' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'options' => (env('MYSQL_SSL')) ? [
PDO::MYSQL_ATTR_SSL_KEY => env('MYSQL_SSL_KEY'), // /path/to/key.pem
PDO::MYSQL_ATTR_SSL_CERT => env('MYSQL_SSL_CERT'), // /path/to/cert.pem
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_SSL_CA'), // /path/to/ca.pem
PDO::MYSQL_ATTR_SSL_CIPHER => env('MYSQL_SSL_CIPHER')
] : [],
Not sure what is wrong, any idea?
I get these errors:
"error":"SQLSTATE[HY000] [2002] An attempt was made to access a socket in a way forbidden by its access permissions.
[{"file":"D:\home\site\wwwroot\vendor\laravel\framework\src\Illuminate\Database\Connection.php","line":624,"function":"runQueryCallback","class":"Illuminate\Database\Connection","type":"->","args":["select * from backend_token limit 1",[],{}]},
It helps to know which version of Laravel you are on to be absolutely sure, but on Laravel 8 my database config has:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => env('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'),
]) : [],
],
Don't change your database.php config but in your .env add MYSQL_ATTR_SSL_CA=/path/to/BaltimoreCyberTrustRoot.crt.pem
FYI sometimes if you are using private IP addresses then edit your hosts file
10.10.10.10 hostname.domain.com
Where of course the ip number matches the ip of the host.
If that doesn't work, add the cipher line from you config PDO::MYSQL_ATTR_SSL_CIPHER => env('MYSQL_SSL_CIPHER'),