Laravel Migration - Says unknown database, but it is created - mysql

when I use php artisan migrate, i get the error SQLSTATE[42000] [1049] Unknown database 'databaseName'.
But the database DOES exists! I even tried going back into terminal, logged into mysql and created the database again, and it said that database already exists!
Why is this giving me this error?

I have the same problem. When I run: php artisan migrate.
In my case I use Vagrant with scotch box.
To solve this, enter:
vagrant ssh
cd /var/www/yourproject
php artisan migrate
And all work well.

In your app/config/database.php file change the default value from databaseName to a real database name that you are trying to use in your application, something like this (for mysql driver):
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'your database name', //<-- put the database name
'username' => 'your user name',
'password' => 'your password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),

One thing could be your casing. If you read the docs on case sensitivity it says:
the case sensitivity of the underlying operating system plays a part
in the case sensitivity of database and table names. This means
database and table names are not case sensitive in Windows, and case
sensitive in most varieties of Unix. One notable exception is Mac OS
X, which is Unix-based but uses a default file system type (HFS+) that
is not case sensitive.
Then go and check your application database setting found # app/config/database.php.
Something that looks like this:
'mysql' => array(
'read' => array(
'host' => '192.168.1.1',
),
'write' => array(
'host' => '196.168.1.2'
),
'driver' => 'mysql',
'database' => 'databaseName',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
And double check everything.
Also try using snake_case rather than camelCase for you database name.

In my case I had MySQL installed on this port: 3308, and in Laravel env file there was 3306.

Related

OctoberCMS Builder MySQL Invalid default value for the integer column

I built an OctoberCMS site on a local VirtualBox LAMP server and moved it and the MySQL database to A2 Shared Hosting.
Error
The live site is working fine, but when I use the Builder plugin to add a new database column and save, I get the error:
Invalid default value for the integer column 'sort_order'. The allowed formats are '10', '-10'.
But the value is 10.
Servers
My Local VirtualBox Server is running MySQL 5.7.29-0ubuntu0.18.04.1.
The A2 Hosting Server is running MySQL 10.3.22-MariaDB-cll-lve.
Database Connection
The database.php config file looks like this, with the database name, username, and password filled in.
'mysql' => [
'driver' => 'mysql',
'engine' => 'InnoDB',
'host' => 'localhost',
'port' => 3306,
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'varcharmax' => 191,
],
Question
Why does saving the new column not work?
Does it have something to do with MySQL on the local server and MariaDB on the A2 server?

Laravel 5: "Base table or view not found" Table X "doesn't exist" - laravel is confusing virtual hosts

Getting this error message RANDOMLY while nav'ing to my view:
QueryException in Connection.php line 636:SQLSTATE[42S02]: Base table or view not found: 1146 Table 'salesspacetv.devices' doesn't exist (SQL: select count(*) as aggregate from devices inner join statustypes on devices.status = statustypes.num inner join aggservers on devices.aggserver_num = aggservers.num left join tickets on devices.id = tickets.device_id group by devices.id) in Connection.php line 636
My server uses multiple virtual hosts, defined in c:\apache24\conf\extra\httpd-vhosts.conf.
"c1.[ourcompanyname].net" is the name of the site whose view I'm nav'ing to.
"salesspacetv.[ourcompanyname].net" is another virtual host (running laravel) on our server.
The word "salesspacetv" absolutely does not exist anywhere in c1's code.
"devices" IS a table used by the c1 site, and there is no "devices" table used by the salesspacetv site.
It seems like laravel's base code is making use somewhere of what it thinks is the subdomain name and that Apache (fyi... running on Windows, on this server) is somehow not getting the correct subdomain name to laravel.
Again, this is a random error. If I simply refresh the page, the error goes away. Also note, whether important or not, that this view is using pagination. I don't imagine that has any importance, but I figured it was worth mentioning.
The databases are MySQL databases.
Lastly, the c1 virtual host definition does actually appear before the salesspacetv virtual host definition in httpd-vhosts.conf.
Thanks.
I believe I ran into this error in the past when I was dealing with multiple projects in my local environment.
Let me get this straight, you are using 1 project then whenever it is involving a database table call it sometimes references to a table or column name that the current project does not contain but contains in another project?
If that is what's happening to you, how I fixed it on my end was name your database environments (.env file) different per project.
#1 .env Method
.env
Project 1:
DB_PROJECT1_HOST=0.0.0.0
DB_PROJECT1_DATABASE=dbname
DB_PROJECT1_USERNAME=dbuser
DB_PROJECT1_PASSWORD=dbpass
Project 2:
DB_PROJECT2_HOST=0.0.0.0
DB_PROJECT2_DATABASE=dbname
DB_PROJECT2_USERNAME=dbuser
DB_PROJECT2_PASSWORD=dbpass
app\config\database.php
Project 1:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_PROJECT1_HOST'),
'database' => env('DB_PROJECT1_DATABASE'),
'username' => env('DB_PROJECT1_USERNAME'),
'password' => env('DB_PROJECT1_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Project 2:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_PROJECT2_HOST'),
'database' => env('DB_PROJECT2_DATABASE'),
'username' => env('DB_PROJECT2_USERNAME'),
'password' => env('DB_PROJECT2_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Hopefully this is the same issue happening to you so you can quickly test this and see if it applies to you.
#2 Alternative (hardcode method suggested by Tezla):
You may also edit the database configuration file (app\config\database.php) and hardcode the database information directly to avoid configuration leaks:
'mysql' => [
'driver' => 'mysql',
'host' => '0.0.0.0',
'database' => 'dbname',
'username' => 'dbuser',
'password' => 'dbpass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]

Laravel 5.1 - Connecting to MySQL Database (MAMP)

There are topics online that are discussing this problem however, I couldn't find any tidy explanation of the problem or any solid answers for the question. What I am trying to achieve is connecting Laravel 5.1 to MySQL Database of MAMP.
In my config>app.php:
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost:8889',
'database' => 'test',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'prefix' => '',
'strict' => false,
],
In my .env:
DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root
I also have .env.example: (which I believe has no functionality)
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
I also have create_users_table.php and create_password_resets_table.php in my database>migrations (even though I did not run any migration:make)
MAMP is directing and running the server successfully as it loads the project on localhost.
Here is my MAMP settings:
And the test database is created (with tables in it which I have previously created and used in my other projects, not Laravel.)
Even though everything seems correct to me, when trying to submit Auth form, I am getting this error:
PDOException in Connector.php line 50:
could not find driver
in Connector.php line 50
at PDO->__construct ('mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=test', 'root', 'root', array('0', '2', '0', false, false)) in Connector.php line 50
at Connector->createConnection('mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=test', array('driver' => 'mysql', 'host' => 'localhost:8889', 'database' => 'test', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, false)) in MySqlConnector.php line 22
and so on...
On mac or unix you have to include the socket path in the configuration database.php file
i.e 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
It was pretty simple for me, I added :8889 to the localhost in the .env file.
DB_HOST=localhost:8889
This is because in the MAMP preferences, :8889 is the default port.
The most important thing for me was defining the UNIX socket. Because I have another MYSQL on my machine - Laravel was trying to connect to a database in that MYSQL process.
Defining the UNIX for the MAMP database to be used worked perfectly. Try adding this to your MYSQL configuration in database.php
'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' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
As far as I am concerned it doesn't make any sense to set in database.php as many of them suggested.
Since this change would be mostly required in the development mode. So the proper way of setting the unix_socket is as below
file: .env
DB_SOCKET='/Applications/MAMP/tmp/mysql/mysql.sock'
By doing the above way already .env is included in .gitignore and won't create any other problem while your project is remotely deployed.
NOTE: I have tested this setting in Laravel 5.7 and above versions
Found my answer. Here is a way to fix it:
Start MAMP
On the top left, go to "MAMP" -> "Preferences"
Go to the "PHP" tab
Tick PHP 5.5.17 (or whatever you have) instead of the one which is ticked by default (5.6.1 -> 5.5.17 with he latest version of MAMP)

lavaral 5 ERROR{ (SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)}

'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database.sqlite'),
'prefix' => '',
],
**JUST CHANGED THE CODE FOR LOCALHOST AND CHANNGED THE NAME TO ROOT AND SET THE PASSWORD OF PHPMYADMIN . **
**WHEN WRITTING THE COMMAND IN XAMPP SHELL ((Php artisan migrate:install)) **
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'password'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'password'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
it is showing error SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)
By default laravel assumes that you will want to have different configurations for different environments. E.g. in a testing environment, you might wish to have a different username and password and in a production environment different. Since laravel has so many configuration files, it quickly becomes a nightmare to manage all those. Hence laravel makes use of PHP's environment variables.
See the docs here.
What is basically says is that if you wish to use the "environment" variables, which laravel uses by default, you have to place all your configurations in the env() method as already mentioned.
If you do not wish to do this, e.g. for simple projects, simply remove the env from your code, like this.
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laravel',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Note that you can mix and match. i.e you can have some of the variables in env and some stand-alone.
So why use env at all?
Lets say your application has 100 testers all placed in different locations. In laravel you have to code approximately 8-10 configuration files. Also you need to version-control those files. So you have two problems at hand:
You do not wish to send all 100 users the same credentials. Also they might use different database, cache server, etc which means that they will have different configurations. So every user has to maintain those 8-10 configuration files by hand.
You do not wish to send these configuration files to version control. Because if you do, whole world will know your API secrets and possibly will take advantage of that (just like password). Also if you look at laravel conf files, you will notice that there are other information such as timezone, debug property, etc that are also in conf files, and you do want to version-control them. So how do you version-control such configuration files and still hide your sensitive information.
The answer is env variables. Laravel uses dotenv whose documentation can be found here. Basically these are variables that live in one file called .env in a key-value pair. E.g.
Sample contents of .env file
APP_DEBUG=false
APP_KEY=ABCDEFGH
...
Once you define your .env file as this, you can get the value using the key as such env('APP_DEBUG').
So this solves the above mentioned problem in following ways:
you keep the .env file to yourself. And you also declare another file called .env.example which is an exact replica of original file except the fact that it contains sample values, not your sensitive values. Then you pass this new example file to everyone. They will replace the sample data with their own sensitive information.
Since you are version-controlling the example file, you can version control all your conf files because they don't contain the secret. The secret is in .env files. All those conf files contain is values like these env('APP_KEY') and the actual value is replaced at run time using your .env file.
Make sure you have to set up right server credentials into .env file on your Laravel project:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myapp_db
DB_USERNAME=root
DB_PASSWORD=pass
Try to clean up artisan cache and restart the artisan,
php artisan config:clear
restart php artisan

Laravel Can't Connect to database - Migrations - Error 2002

I have searched for a couple of hours now, and still am unable to find this.
I get 2 errors, if I use the database host as 'localhost', I get this error:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
and if I change the database host to '127.0.0.1' I get this error:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Things I have tried:
changing where the apache / mysql server is run (either user or `josh (Apache) / josh (MySQL)
changing the port that MySQL runs on in MAMP, and putting that port in the mysql array in the database.php file
changing the host of the connection from localhost to 127.0.0.1 and back.
creating a new user in phpmyadmin
turning off the firewall
Any ideas how to fix this?
I figured it out, add this after the 'host' => '127.0.0.1':
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
So the connection would look like this:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'dbname',
'username' => 'josh',
'password' => 'pass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
fist clear your config file by typing
php artisan config:clear
Then modify your .env file
APP_URL = 127.0.0.1
DB_HOST = 127.0.0.1
Hope this works for you.
IF you use lampp . You can try this:
Open file mysql config. (you can open lampp control panel in /opt/lampp/manager-linux-x64.run,after that open Configure of Mysql Database or open /opt/lampp/etc/my.cnf).
Find and see "socket =/opt/lampp/var/mysql/mysql.sock",
Add 'unix_socket' => '/opt/lampp/var/mysql/mysql.sock' to file database.php in laravel:
'mysql' => [
'driver' => 'mysql',
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'luanvan'),
'username' => env('DB_USERNAME', 'huuthang'),
'password' => env('DB_PASSWORD', '123456'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
File mysql.sock depend on your install location of lampp. I let you see if you can't find it. Good luck
I had to do the following in ubuntu 15.10
copy the file or file contents from /opt/lampp/etc/my.cnf to /etc/mysql/my.cnf
Then open database.php file in your app/config folder and add the following line below 'host' => env('DB_HOST', 'localhost')
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'
That solved my problem, could save someone else's. Please note that it is VERY IMPORTANT you first verify the directory which contains mysql.sock file before configuring the path.