Two laravel projects,laravel_a and laravel_b,they have their respective database database_a and database_b.
Now there is some common data,I created the third database database_common,
How do the two projects read the third database?
You need to create a extra database connections. Try like this:
In config/database.php:
'connections' => [
'common_db' => [
'driver' => 'mysql',
'host' => env('COMMON_DB_HOST', ''),
'port' => env('COMMON_DB_PORT', '3306'),
'database' => env('COMMON_DB_DATABASE', 'forge'),
'username' => env('COMMON_DB_USERNAME', 'forge'),
'password' => env('COMMON_DB_PASSWORD', ''),
'unix_socket' => env('COMMON_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
In .env:
COMMON_DB_CONNECTION=mysql
COMMON_DB_HOST=127.0.0.1
COMMON_DB_PORT=3306
COMMON_DB_DATABASE=yourDatabaseName
COMMON_DB_USERNAME=yourDatabaseUsername
COMMON_DB_PASSWORD=yourDatabasePassword
Then you can write the query like this:
$users = DB::connection('common_db')->select(...);
Ref: Database: Getting Started. Read this page carefully.
Related
I use Laravel
When I try to access Localhost:8000 I get an error
Illuminate\Database\QueryException
SQLSTATE[HY000] [1049] Unknown database 'my_data' (SQL: select `title`,`id` from `table_8` where `id` = 24)
I have a database called my_data in phpmyadmin
XAMPP\volumes\root\htdocs\project\.env
_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= my_data
DB_USERNAME=root
DB_PASSWORD=
XAMPP\volumes\root\htdocs\project\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', 'my_data'),
'username' => env('DB_USERNAME', 'root'),
'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'),
]) : [],
],
I changed the DB_DATABASE to my_data in the .env file
And I ran the command
$php artisan cheat:clear
And yet the error is still the same error
Illuminate\Database\QueryException
SQLSTATE[HY000] [1049] Unknown database 'my_data' (SQL: select `title`,`id` from `table_8` where `id` = 24)
I am use Mac
i think your username and password of phpmyadmin is wrong. please check it out. of different. fix it. and try again
I have my laravel app running and i'm trying to connect it to a Mysql database. I'm using XAAMP for this. After getting Apache and Mysql is running (Mysql is running on port 3308). I reconfigured my .env file and database.php file and I've run php artisan config cache. However when i try to create a database from my terminal using create database xxx, xxx database is created but when i check phpmyadmin on my browser, i can't find the xxx datatbase.
Here's my database.php file:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3308'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'test123'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
And here's my .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3308
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=test123
Solved it but i had to clear up port 3306 so that XAAMP mysql could use it. Cleared it by ending all mysql tasks under task manager.
I had a laravel 7 app I developed on my local (MAMP). I now want to set it up on my website hosting account (apache2, php7.4, mysql server). I did a git pull to pull down all the files and now when I do a
php artisan migrate
I get an error that says
SQLSTATE[HY000] [2002] No such file or directory (SQL: create table `migrations` (`id` int unsigned not null auto_increment primary key, `migration` varchar(191) not null, `batch` int not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
I am able to login to the phpmyadmin on the hosting server and see my empty database there.
My env file looks like this:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=commandcenterdb
DB_USERNAME=****
DB_PASSWORD=******
and my config/database.php file looks like this
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE',' commandcenterdb'),
'username' => env('DB_USERNAME', '*****'),
'password' => env('DB_PASSWORD', '********'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
I got the same error when using MAMP on a mac
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = php-laravel and table_name = migrations)
I solved it by adding DB_SOCKET on .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your-database-name
DB_USERNAME=your-username
DB_PASSWORD=your-password
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
if it doesn't work try to modify the charset and collation on config/database.php file
'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' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
]
its also worth to cross check ,i had UNIX_SOCKET instead of DB_SOCKET in the .env
How to configure Laravel 5.5 with master slave MySQL replication ?
I want to make write operations and read operations in master and slave respectively .
Optional: Is there any way to do connection pooling and max / min no of open connections in ideal conditions. ?
Just change your config/database.php file to include read (slave) and write (master) hosts like so like the Laravel docs suggest:
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'sticky' => true,
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
I have a web app I built using Laravel 5.2 , my question is how do I store then show emoji's. At the moment when I use an emoji from my iPhone , fire for instance, after saving the database returns ???? . Ive set the
'charset' = 'utf8mb4';
'collation' = 'utf8mb4_unicode_ci'
but still get the same result. Do i need to install a package or something?
Make sure you have the charset and collation correctly setup in the database config file as well, to ensure Laravel actually knows what charset to use when saving:
// config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],