Can't migrate database( laravel) - mysql

I am facing problem migrating database using php artisan migrate command .
Can anybody help me figure out what I did wrong here?
Please note I am fairly new with laravel.

Set proper database connection in the .env file then restart your server. Then try to migrate. Hope it will helpful.

you DB_CONNECTION is a localhost put mysql
and your config/database 'default' => env('DB_CONNECTION', 'mysql'),
and your .env DB_CONNECTION=mysql

Your installation isn't pulling your username/password from your .env file and it's using the defaults. Assuming this is a new install of Laravel and you just edited .env.example then you need to rename that file to just .env:
You can run:
mv .env.example .env

set these in .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=fresher
DB_USERNAME=root
DB_PASSWORD=
give a try to these commands to cleanup things
1 - run composer dumpautoload
2 - run php artisan clear-compiled
after that run the php artisan migrate command

Related

Connect to mySQL in Laravel 9 installed using Docker

Recently, I installed Laravel 9 by following the steps below:
First, I downloaded and installed Docker Desktop and after running it,
I ran the following command in the directory where I wanted to install my Laravel project:
curl -s "https://laravel.build/example-app" | bash
After Laravel was successfully installed, I ran the following lines of code to activate sail:
./vendor/bin/sail up
cd example-app
Now when I check Docker to see the services that are being run for example-app, I see:
example-app_mysql_1 mysql/mysql-server:8.0
But when I click on the "OPEN IN BROWSER" button to see a page similar to phpmyadmin, I get one of the following errors:
Error 1: This error is thrown when I enter the URL as http://localhost:3306:
localhost sent an invalid response. ERR_INVALID_HTTP_RESPONSE
Error 2: And this error is thrown when I enter the URL as http://localhost:3306:
localhost sent an invalid response.
ERR_SSL_PROTOCOL_ERROR
As I need to create a new database with the same name defined in my .env file (shown below), I need to have access to mysql wizard. Besides, in the developing phase, sometimes, I may need to manipulate the database manually (with using migrations and other artisan commands).
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=example_app
DB_USERNAME=sail
DB_PASSWORD=password
How can I solve this?
mysql is not available through the browser.
You must either install phpmyadmin or connect to mysql as a wizard through softwares such as tableplus or sequel pro or etc.
But in any case you can install phpmyadmin by adding the following code to docker-compose.
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8080:80
environment:
MYSQL_USERNAME: "${DB_USERNAME}"
MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
PMA_HOST: mysql
networks:
- sail
Do not forget after adding this code use sail build --no-cache to rebuild services

How to connect to MySQL on a different port

I ran php artisan serve artisan command on a Laravel app, it runs on port 8000. However, I get a MySQL error when hitting a table: No connection could be made because the target machine actively refused it.
My .env
DB_HOST=locahost
DB_PORT=3306
Try changing to DB_HOST=127.0.0.1
Sometimes this problem occurs because of cache, run the below command and try to clean your configuration cache:
php artisan config:clear
And if your problem is not solved try to change your DB_HOST=localhost in your .env file to DB_HOST=127.0.0.1.

SQLSTATE[HY000] [2006] MySQL server has gone away laravel mysql and xampp

i am using laravel 7 with a xampp installation. php artisan migrate fails and gives the error: SQLSTATE[HY000] [2006] MySQL server has gone away
my .env file:
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=****_db
DB_USERNAME=root
DB_PASSWORD=*******
i have tried localhost in the env file isntead of 127.0.0.1. Worth mentioning that my xampp application launches at this address: http://192.168.64.2/
i have started and restated the xampp server and i have been php artisan cache:clear each time i make changes to the .env file
I ended up ditching connecting to my local xampp installation and connected to an AWS RDS instance and it worked.

Keep getting Undefined variable: DATABASE_URL error on Laravel - MySql

I keep getting
PHP Notice: Undefined variable: DATABASE_URL in ....../config/database.php`
error when running
php artisan migrate:fresh --force`
My .env file consist of
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=xxxxx
DB_USERNAME=xxxxx
DB_PASSWORD=xxxx
DATABASE_URL=mysql://xxxxx:xxxxx#127.0.0.1:3306/xxxxx
I'm not sure why this is showing up.
Do use only one
Either
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=xxxxx
DB_USERNAME=xxxxx
DB_PASSWORD=xxxx
Or,
DATABASE_URL=mysql://xxxxx:xxxxx#127.0.0.1:3306/xxxxx
DATABASE_URL contains all the database config, i.e. DB_CONNECTION, DB_HOST, etc.

Illuminate\Database\QueryException : SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'forge'

I have downloaded a repo on github and created a database. I am trying to do migrations and am getting the below error
Error:
Illuminate\Database\QueryException : SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'forge' (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations and table_type = 'BASE TABLE')
This is my .env file
APP_NAME=Tafuta
APP_ENV=local
APP_KEY=base64:0IDLuBYdTssK55SeeQTmCJ3GHlJXsR6BahTeenXaf90=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=TafutaM
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Your .env is not taken into consideration for the variables.
It might be cause by something in your Vhost configuration like
A direct declaration of the variable in there that would override .env variables.
Another environment file is declared it it to be read. like .env.prod or .env.dev
Many people have this common error: It's because your database is not set.
Error say: '#localhost', then username = ''
database: forge, then database is named = 'forge'
If you dont see that parameters into your .env, its because file is not read, then you have to refresh your configuration:
php artisan config:cache
if you are using php artisan serve cancel it, and run again.
Now, if problem persists, you have to log with terminal or a database tool to your database, and make sure that the next parameters are the same:
DB_CONNECTION=
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
Sometimes, people set incorrect port, or username or any(dont forget refresh configuration if you edit .env file).
You have to run the following command if you downloaded the source from GitHub:
composer install
cp .env.example .env
php artisan key:generate
php artisan config:clear
php artisan cache:clear
php artisan migrate