Vagarant/homestead does not create database unless - laravel

I upgraded laravel/homestead box to version 11.1.0. Then a surprising happened: My DBs disappeared. How? I have no idea.
But then when I tried to reinstall the databases using Homestead.yaml, it did not work eventhough I added the list of databases in Homestead.yaml.
Here the what I did in Homestead.yaml:
databases:
- db00
- db01
Then I ran "vagrant provision" but nothing changed in MySQL. "show databases;" displayed default tables only.
Then I activated the features:
features:
- mysql: true
- mariadb: false
Then I ran "vagrant provision" and it worked. I had both databases (db00 and db01) in MySQL.
Here are the problems:
Based on laravel/homestead documentation for laravel v8.x, there is no feature option for mysql. That caused the following error during provisioning:
homestead: Invalid feature: mysql
homestead: Ignoring feature: mariadb because it is set to false
Once I added the option "- mysql" to the Homestead.yaml, I received the following errors for MariaDB
homestead: Running: script: Creating MySQL / MariaDB Database: db00
homestead: We didn't find a PID for mariadb, skipping $DB creation
homestead: Running: script: Creating MySQL / MariaDB Database: db01
homestead: We didn't find a PID for mariadb, skipping $DB creation
Now here my question: What is going on? Without the feature option "- mysql" no database is created; no matter if I define the list under the databases. Once I add it, things work, databases are generated however, I get error that the feature is not valid. Also it is not included in the documentation.
Where am I going wrong? or if anyone has an explanation or a better solution?

The answers to both of your questions is: because the documention is a bit misleading
For your first question, mysql is technically not an optional feature since is installed by default. However, as of v11.3.0 in order to provision custom databases it must be listed under the features section in the Homestead.yaml:
features:
- mysql: true
due to only running the provisioning script if mysql is specifically enabled:
https://github.com/laravel/homestead/blob/v11.3.0/scripts/homestead.rb
For your second question, the error message is displayed due to the way that the create-mysql.sh script was implemented in v11.0.0:
https://github.com/laravel/homestead/blob/v11.0.0/scripts/create-mysql.sh
if [ -z "$mariadb" ]
then
# Skip Creating MariaDB database
echo "We didn't find a PID for mariadb, skipping \$DB creation"
else
mysql -e "CREATE DATABASE IF NOT EXISTS \`$DB\` DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci";
fi
With the configuration you are using the if [ -z "$mariadb" ] condition will always be true because the process id of mariadbd will never be found since it is not enabled. Therefore the We didn't find a PID for mariadb, skipping $DB creation message will always be displayed even though the databases are succesfully created for mysql. Technically that is correct based on the way the script is coded, but it is also a bit confusing.
Also as for why your databases disappeared when upgrading, assuming you were following these instructions:
https://laravel.com/docs/8.x/homestead#updating-homestead
The vagrant destroy command will completely remove the homestead box and rebuild it. Therefore any data or other custom configuration will be lost unless it is configured to backup the data:
https://laravel.com/docs/8.x/homestead#database-backups

I ran into this same issue recently with Homestead 12.3.0 and solved it by editing my Homestead.yaml with the following:
...
databases:
- homestead
- my_database
features:
# - mysql: true
- mariadb: false
- postgresql: false
- ohmyzsh: false
- webdriver: false
services:
- "mysql"
...
Then I ran vagrant reload --provision, and did not see the MariaDB error anymore.
To confirm you can SSH into the vagrant box, and log into MySQL (homestead / secret). In my case I found the database had been created correctly.

Related

Why Homestead is running apache by default even if not specified?

I have a Homestead VM with multiple nginx sites and a couple of apache sites too, everything configured through the Homestead.yaml file (short example below):
sites:
- map: site1.local
to: /home/vagrant/site1
php: "7.1"
#type: "apache <= (commented on purpose, not an error)
- map: site2.local
to: /home/vagrant/site2
php: "7.1"
I've been working turning On and Off the type: "apache" setting so the VM starts running apache (instead of nginx) or not, depending on the site that I need to work on at the specific moment.
My Issue now is that, after upgrading Vagrant and Homestead, it always keeps starting apache by default, no matter if it is specified or not, ALWAYS!; so everytime I start the machine, I need to ssh-it and flip the server.
I even tried using the services config directive as follows, without luck:
services:
- enabled:
- "nginx"
- disabled:
- "apache2"
Any thoughts? Please help!
Versions I am using:
Vagrant 2.2.7
Homestead 10.8.0
I had a similar issue. It turned out that I needed to log in the VM through SSH, and run the following command:
sudo systemctl enable nginx.service
When you look at restart-webserver.sh in the Homestead scripts directory, you will see that it verifies whether nginx is enabled, otherwise it always tries to restart Apache.

Redis does not start on boot on homestead vagrant server

I know that we can directly ssh into the VM & enable the redis service.
But i think there must be a way to enable redis using homestead.yaml.
I tried to search for docs but i couldn't find anything.
EDIT
I'm posting my homestead.yaml file.
ip: "192.168.10.10"
memory: 1048
cpus: 2
provider: virtualbox
authorize: C:\Users\stack\.ssh\id_rsa.pub
keys:
- C:\Users\stack\.ssh\id_rsa
folders:
- map: W:\sites\project
to: /home/vagrant/project
sites:
- map: project.test
to: /home/vagrant/project/public
databases:
- homestead
features:
- mariadb: false
- ohmyzsh: false
- webdriver: false
I have installed predis so the connection with redis is not an issue.
Everytime i boot my vm, I manually have to go & start redis by typing this command systemctl start redis-server.
Which is the reason i was wondering that there must be a way to enable redis server from inside the homestead.yaml so i don't have to do it manually.
If you want homestead's redis server to start automatically whenever homestead is up...
Log into homestead via ssh.
sudo systemctl enable redis-server
You should only need to run this once.
There was a Bug with this version of Homestead 10.0.1
Redis does not start on boot.
However this has been fixed if you check out the issue i have linked.
Still an issue?
Here is a quick fix while waiting for homestead box update
sudo service redis-server start
Redis is already included/installed thus enabled in Homestead. See included softwares of Homestead.
To test type redis-cli and ping in your command line (inside vagrant)
For predis just run composer require predis/predis.
Try adding services section to your homestead.yaml after features section, but I don't know if order matters.
services:
- enabled:
- "redis-server"
Then vagrant reload --provision

Mongo DB: Failed to connect on Laravel Homestead

I need to create a new Laravel project and I need to use Mongo DB as a database server. Following the Homestead documentation I added this in my Homeasted.yaml file:
mongodb: true
From what I see in the logs the mongo database is created:
homestead-7: Running: script: Creating Mongo Database: homestead
But after this I received this message:
homestead-7: Running: script: Creating Mongo Database: homestead
homestead-7: MongoDB shell version v3.6.3
homestead-7: connecting to: mongodb://127.0.0.1:27017/homestead
homestead-7: 2019-06-03T10:01:52.103+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
homestead-7: 2019-06-03T10:01:52.104+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
homestead-7: connect#src/mongo/shell/mongo.js:251:13
homestead-7: #(connect):1:6
homestead-7: exception: connect failed
The SSH command responded with a non-zero exit status.
From what I found on the internet it can be that the mongo service is not started. I restarted the box without provisioning this time but with the same result. Command:
vagrant#homestead:~$ mongo
Also, I found some solutions that involve changing of some files on an Ubutu O.S but in my case it will not work because the box will start as a fresh instance.
Any idea how to fix this? Thanks in advance!
Laravel version: 5.8.
Homestead: 8.4.0
MongoDB shell: v3.6.3
LATER EDIT
After the VM has started I executed this command:
sudo apt-get install mongodb
After installation I can execute the "mongo" command:
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-use
Strange, so actually Mongo DB isn't installed?! Even if I added the flag. Now I need to figure how to add it every time when the VM is started.
I managed to fix my problem after hours of searching so I will post the fix.
Because I didn't find anything that could help me I started to check the Homestead scripts in order to understand how Mongo is installed and in homestead.rb I found this line:
# Install MongoDB If Necessary
if settings.has_key?('mongodb') && settings['mongodb']
config.vm.provision 'shell' do |s|
s.name = 'Installing MongoDb'
s.path = script_dir + '/install-mongo.sh'
end
end
So I searched were "install-mongo.sh" is called and I found this condition:
if [ -f /home/vagrant/.mongo ]
then
echo "MongoDB already installed."
exit 0
fi
So Mongo DB is not installed every time only if the "/home/vagrant/.mongo" file doesn't exist. At this point I realized that maybe Mongo failed to be installed but this file was written.
So the solution was to destroy the Vagrant box and recreate it from scratch:
vagrant destroy
vagrant up --provision
In Homestead.yaml under features: add -mongodb: true
and run vagrant reload --provision, that is same as what #NicuVlad has suggested but little bit easier.

Unable to access the database of homestead environment (laravel) through MYSQL workbench

Unable to access the homestead database through MYSQL workbench Please help me out to solve this issue
error messages at time of testing the connection - "Failed to Connect to MySQL at 127.0.0.1:33060 with user homestead Lost connection to MySQL server at 'reading initial communication packet', system error: 0"
please take a note that
Bind address in my.cnf file is 127.0.0.1 (my.cnf file located at /etc/mysql/)
hosts file contains: 127.0.0.1 localhost projectname (which is located at /etc/)
I have tried changing bind address to 0.0.0.0 but it was also not worked.
Actually I was keep trying to setup the database connectivity by just considering the homestead environment ... But on Keep searching I found one link http://www.acnenomor.com/171821p1/laravel-homestead-vagrant-box-database-problems from where I got idea and I tried once again with the data shown in attached image and it worked for me ... so I am sharing this answer in general .. it may help someone...
Please take a note that I have also tried to connect the database of Laravel homestead environment by installing all the stuffs in other system too and it was worked fine as described in the official document.. so in my case it might be the problem of some changes in configuration file of mysql or any other related environment... so please first go through the official document... http://laravel.com/docs/4.2/homestead#daily-usage
The first thing you need to do is
vagrant up
and then use these connections below for a default homestead installation.
Connection Method: Standard (TCP/IP)
Hostname: 127.0.0.1
Port: 33060
Username: homestead
Password: secret
Try the alternate IP of 192.168.10.10
hostname = 192.168.10.10
port = 3306
username = homestead
password = secret
This works for me on Windows 10 Home Version 20H2 OS Build 19042.1165 (To find this information navigate to Start, Type 'About' and click 'About your PC').
ran into a similar problem while trying to use SQLyog on my windows machine to connect to mySQL on a Homestead environment. I was able to resolve this issue by adding
skip-name-resolve
to the VMs /etc/mysql/my.conf file.
Apparently (according the mysql error logs) connections from the local machine were coming from '10.0.2.2', which caused the warning "IP Address '10.0.0.2' could not be resolved".
You will need to re-add this line every time you spin up the environment.

Salty Vagrant Master hostname: salt not found

I am trying to load my vagrant box with salt, asking it to install Apache.
I am using salty-vagrant in masterless mode.
The vagrant box gets loaded, but it gets stuck in the console with the following message:
[default] Running provisioner: salt...
Checking if salt-minion is installed
salt-minion found
Checking if salt-call is installed
salt-call found
Salt did not need installing or configuring.
Calling state.highstate... (this may take a while)
When I check the vagrant salt log, the following is found:
[salt.utils ][ERROR ] This master address: 'salt' was previously resolvable but now fails to resolve! The previously resolved ip addr will continue to be used
[salt.minion ][WARNING ] Master hostname: salt not found. Retrying in 30 seconds
Has anyone faced this issue before?
You need to make sure you are passing a minion config with the following option set:
file_client: local
Read all the steps in the Masterless Quick Start: https://github.com/saltstack/salty-vagrant#masterless-quick-start

Resources