How to connect to MySQL on a different port - mysql

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.

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

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.

Laravel Docker Container not connecting to local mysql

I have an issue connecting to mysql running in the local machine in my DockerFile i have mentioned
FROM php:7
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo mbstring pdo_mysql
WORKDIR /home
COPY . /home
RUN composer install --ignore-platform-reqs
CMD php artisan serve --host=0.0.0.0 --port=8081
EXPOSE 8081
and this in my .env configuration
DB_HOST=localhost
DB_DATABASE=databasename
DB_USERNAME=root
DB_PASSWORD=testpassword
I have very less clue about where it is failing. Do i need to install mysql for Docker container also?
A much simpler solution (for Mac OSX & Docker for Windows) is to replace the host address from localhost
to host.docker.internal
DB_HOST=host.docker.internal
DB_DATABASE=databasename
DB_USERNAME=root
DB_PASSWORD=testpassword
Basically the DNS namehost.docker.internal will resolves to the internal IP address used by the host.
NB: If you have changed your address to host.docker.internal but you still receive connection refused error, it’s most probably because MySQL is currently configured to only listen to the local network.
To resolve that, please update the value of the bind_address to 0.0.0.0 in your my.cnf configuration file.
you are trying to connect to mysql in localhost, which is (surprisingly) the reference to the local host. since its a relative address, inside the container it is being resolved as the container own address, and no mysql is awaiting you there...
so to solve it - just give it your real host ip instead of localhost or 127.0.0.1.
step 1 - fix .env file:
DB_HOST=<your_host_ip> #run `ifconfig` and look for your ip on `docker0` network
DB_DATABASE=databasename
DB_USERNAME=laravel_server #not root, since we are going to allow this user remote access.
DB_PASSWORD=testpassword
step 2 - create dedicated user:
open your mysql: mysql -u root -p, give your root password, and run the following:
CREATE USER 'laravel_server'#'%' IDENTIFIED BY 'testpassword';
GRANT ALL PRIVILEGES ON databasename.* TO 'laravel_server'#'%';
FLUSH PRIVILEGES;
we created the user and gave it the permissions.
step 3 - open mysql to remote access:
we have to make it listening on all interfaces and not just localhost and therefore we run:
sudo sed 's/.*bind-address.*/bind-address=0.0.0.0/' /etc/mysql/mysql.conf.d/mysqld.cnf
(you will be prompted for password. this command is just replacing the line in mysql configuration file)
step 4 - updating:
in the project directory: php artisan config:cache
service mysql restart
then docker build and run a new container again. it should work for you.
I see two options -
Use the private IP of your docker host i.e where mysql server is running.
Use the host network mode while running container in case you want to use localhost.
docker container --net=host ...
In my case (Ubuntu 20.04 Desktop), I had MariaDB already running and using port 3306. So when the app inside the docker container was trying to start MySQL that was inside the container it failed because it was trying to listen to an already used port. I switched off the already-running MariaDB using the command below :
sudo systemctl stop mariadb.service
Then tried starting the docker app. It ran successfully because port 3306 was now free and used by MySQL inside the container. But since I intend to use both of them, a much more permanent solution would be to configure either of the Database systems i.e the one inside the docker container or the one outside the docker container to use a different port other than the default 3306.

Can't migrate database( laravel)

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