Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

The 'localhost' array is not being recognized. If I add :3307 to the '*' array's server attribute, it works. Here's the contents of my db.php:

return array(

  '*' => array(
    'server' => 'localhost',
    'user' => 'av01759',
    'password' => '*******',
    'database' => 'av01759_maltaministries',
    'tablePrefix' => 'craft',
  ),

  'localhost' => array(
    'server' => 'localhost:3307'
  )

);

This a fresh Craft install with no tweaks. Any ideas?

share|improve this question
    
See my answer. Instead of 'localhost' you should use the domain name to trigger the correct environment-specific settings. –  Alex Kendrick Jun 19 at 20:28
    
@serquhart Quick question... When you refer to your local machine as "localhost" in the /craft/config/general.php file, do those settings take effect? Trying to determine if this is a database connection error or an environment detection error. –  Lindsey D Jun 19 at 20:34
    
@LindseyD I haven't even touched general.php - this is a fresh install. It's not a database connection error, because if I make the '*' array's server setting match the setting from the 'localhost' array, it works. –  serquhart Jun 19 at 20:35
    
I understand... but we're trying to determine if your "localhost" block is even being picked up at all. Would you mind conducting a simple test? Duplicate this multi-environment configuration in general.php and add a simple config setting which would make it obvious that your "localhost" block is being recognized. Set a value like omitScriptNameInUrls to false and see if that change actually kicks in on your local site. –  Lindsey D Jun 19 at 20:41
    
Even better, try turning on devMode. You'll know right away if that yellow caution tape appears at the top of your CP. –  Lindsey D Jun 19 at 20:45
show 2 more comments

7 Answers 7

The environment-specific array that Craft uses is determined by the $_SERVER['SERVER_NAME'] variable (usuall your domain name), not the database hostname. Instead of using localhost to trigger the env-specific settings, you should use your domain. Your db.php should looks more like this:

return array(

  // default
  '*' => array(
    'server' => 'localhost',
    'user' => 'av01759',
    'password' => '*******',
    'database' => 'av01759_maltaministries',
    'tablePrefix' => 'craft',
  ),

  // production-specific (use your domain instead of example.com)
  'example.com' => array(
    'server' => 'localhost:3307'
  )

);

UPDATE: Since in this case, $_SERVER['SERVER_NAME'] actually is "localhost" for you and I understand you are connecting to a remote db, you could try using this for your db.php (swapped the value for server in my above example, this way you make the exception for production).

return array(

  // default
  '*' => array(
    'server' => 'localhost:3307',
    'user' => 'av01759',
    'password' => '*******',
    'database' => 'av01759_maltaministries',
    'tablePrefix' => 'craft',
  ),

  // production-specific
  'production-site-domain-here.com' => array(
    'server' => 'localhost'
  )

);
share|improve this answer
    
Localhost is the domain for my local environment. Environment vars work simply by checking the $_SERVER['SERVER_NAME'] variable. For local development on my machine, that variable contains 'localhost'. So for Craft's setup, the '*' array provides default settings for every environment, and wherever the SERVER_NAME variable partially matches 'localhost', it should override the default server with the server provided in the 'localhost' array. –  serquhart Jun 19 at 20:31
    
Yes, you're right I should have said use the value for $_SERVER['SERVER_NAME'] instead of "domain name" thanks (I fixed this in my answer). I see your point about your situation -- Are you able to create a virtual host for your site, like example.dev, so that you don't have to rely on "localhost" to trigger the environment specific settings? If not, see the update I added on my answer. –  Alex Kendrick Jun 19 at 21:19
add comment

This issue may be that your database name, prefix, password, etc are different on your local machine, than the ArcusTech server. If your local values don't match the values in the block defined with the asterisk, you won't be able to connect.

Most local databases with tools like MAMP, use a default user and password like 'root'. Try to look into some of these values, or post more info about your local setup.

share|improve this answer
    
I'm logging into the live database remotely - not dealing with dueling databases. In other words, if I add port 3307 to the '*' array, it works. –  serquhart Jun 19 at 20:06
    
Ah ok. I believe you would need the external IP to for where the MySQL DB is hosted. In this case, if you are using shared hosting on ArcusTech, this will not be your server IP. You might want to contact ArcusTech and ask how you might connect to MySQL with an external address. I don't think you are able to refer to their databases by anything but localhost, without that ext IP. Which in this case, would cause Craft to look at your machine for localhost. Hope this makes sense. –  Bill Columbia Jun 19 at 20:49
    
I'm already successfully connecting to the database via SSH tunneling. For local development, I need to add the port, hence the added port under 'localhost' in my db config. Craft wouldn't look at my machine for localhost - it simply reads the value of the SERVER_NAME constant and compares that string to the string entered in db.php, which in this case is an exact match. –  serquhart Jun 19 at 20:59
    
So you have it all working now by adding the port? –  Bill Columbia Jun 19 at 21:05
    
I wish. The issue was never the address, it's getting Craft to recognize the server name. –  serquhart Jun 19 at 21:13
show 4 more comments

If you're using MAMP you might also want to try using ".dev" or ".dv" for your array like so in your general.php config file:

'.dev' => array(
     'devMode' => true,
),

Versus using localhost.

UPDATED Local / Live General.php file setup:

return array(
    '*' => array(
        'cpTrigger' => 'admin',
        'omitScriptNameInUrls' => true,
        'defaultFolderPermissions' => 0755,
        'phpMaxMemoryLimit' => '1024M',
        'useCompressedJs' => true,
        'useEmailAsUsername' => true,
        'autominEnabled' => true,
        'autominCachingEnabled' => true,
        'autominMinifyEnabled' => true,
    ),
    '.dev' => array(
        'devMode' => true,
        'environmentVariables' => array(
            'siteUrl' => 'http://www.DOMAINNAME.dev/',
            'basePath' => 'LOCAL PATH',
            'autominPublicRoot' => basePath,
            'autominCachePath' => basePath.'cache',
            'autominCacheURL' => '/cache',
        )
    ),
    '.com' => array(
        'devMode' => false,
        'environmentVariables' => array(
            'siteUrl' => 'http://www.DOMAINANME.COM/',
            'basePath' => 'SERVER BASE PATH',
            'autominPublicRoot' => basePath,
            'autominCachePath' => basePath.'cache',
            'autominCacheURL' => '/cache',
        )
    )
);
share|improve this answer
    
Using WAMP - $_SERVER['SERVER_NAME'] contains "localhost", so nothing else will match. –  serquhart Jun 19 at 20:07
    
Check out my updated example. Are you setting up separate HOSTS in MAMP? –  Mark Busnelli Jr Jun 19 at 20:18
    
Just saw your update - using tld's is just one approach, it really comes down to unique partial matches, which can look a number of different ways. As I stated, my local environment registers as 'localhost', so nothing else would work. –  serquhart Jun 19 at 20:19
    
wamp, I'm using wamp. Hosts aren't an issue, I'm accessing the local site at 'localhost'. –  serquhart Jun 19 at 20:20
    
I see. I use MAMP and that's just how I approach builds in different environments. Maybe you might consider updating your question with your exact setup so someone else can better understand and assist you. –  Mark Busnelli Jr Jun 19 at 20:21
show 1 more comment

When you are accessing a database "from a distance", it won't be called "localhost". Having the port in there is probably helpful, but you'll want to specify the DB server by its IP address.

'localhost' => array(
  'server' => '(DB IP address):3307'
)

Also, be sure that your server database is allowing remote access. By default, most DBs will only allow access to their local files.

share|improve this answer
    
If I set the 'server' attribute in the '*' array to 'localhost:3307', it works. I just need to add the port, which I'm doing here for localhost, but Craft isn't recognizing it. –  serquhart Jun 19 at 20:23
    
It works on your local machine, or on the remote server? "localhost" is supposed to mean local, which a remote server would not be. –  Lindsey D Jun 19 at 20:24
    
It works everywhere, 'localhost:3307' for local development and 'localhost' for the live site. I'm using an SSH tunnel to access the remote database. –  serquhart Jun 19 at 20:28
    
Ah, ok... that makes more sense. –  Lindsey D Jun 19 at 20:30
add comment

Another option is to use named environments instead of using domain matching.

in your index.php file, just set (changing 'development' to whatever you like):

define('CRAFT_ENVIRONMENT', 'development');

Then your environmental config array can use "development" as a key, instead of localhost or .dev

share|improve this answer
    
Right, but if 'localhost' doesn't work, then assigning 'localhost' to an intermediate variable doesn't fix it. My initial setup attempted this and I bypassed it for troubleshooting. –  serquhart Jun 19 at 20:33
    
I don't follow. If you set the CRAFT_ENVIRONMENT constant, it bypasses any domain matching, so if you wanted to set that to 'localhost' you could, and it works. But generally, you'd set this something like development, production, or staging. buildwithcraft.com/docs/php-constants#craft-environment –  Tim Kelty Jun 20 at 21:26
    
Ah - I was using a script within index.php that dynamically assigns CRAFT_ENVIRONMENT based on the current environment. Statically assigning this variable seems to defeat the purpose of a multi-environment config, no? –  serquhart Jun 24 at 13:30
1  
@serquhart, I include an environment.php file at the top of my index.php that sets the CRAFT_ENVIRONMENT variable. This file stays out of source control, so, e.g. on my production server, I have an environment.php file that sets CRAFT_ENVIRONMENT to "production". Likewise for any other environment. –  Tim Kelty Jun 25 at 12:04
add comment

This boils down to being either one of two things...

  • A database connection error, or
  • An environment detection error.

You seem very convinced that the database connection info is perfect, so let's explore the possibility of an environment detection error.

Please conduct this test... Edit your craft/config/general.php file, and add this configuration:

return array(

  '*' => array(),

  'localhost' => array(
    'devMode' => true
  )

);

Now login to your Craft control panel... Do you see the yellow-striped "caution tape" at the top of your control panel?

If the answer is "yes", then your environment detection is working perfectly. Therefore, you have a database connection error.

If the answer is "no", then you have an environment detection error. If this is the case, please answer this question:

Exactly what URL do you use to view your website on your local machine?

share|improve this answer
    
Hi Lindsey - it's not a database connection error. You troubleshoot that by making the desired server address the default and seeing if it works, which it does. The issue is Craft correctly determining the environment. The exact URL for local development is "localhost", or more accurately, localhost –  serquhart Jun 20 at 1:33
    
Did you conduct the test with config/general.php? I've never heard of anyone else having trouble like this with their environment being detected. –  Lindsey D Jun 20 at 1:36
    
The database connection is precisely the same test that you're referring to, except instead of looking for yellow tape, I'm looking for a working database connection. The connection works perfectly fine if given the right server address. If the right address is under '*', it works. If it's under 'localhost', it doesn't. And I've never heard of anything like this either. –  serquhart Jun 20 at 1:38
    
It sounds like you haven't conducted the "yellow tape" test, and I encourage you to take 2 minutes and try it. Based on the results of that test, we can give you further guidance. –  Lindsey D Jun 20 at 1:41
    
Heh, you're persistent. I did it, no tape. –  serquhart Jun 20 at 2:00
show 1 more comment
up vote 1 down vote accepted

Well, I can't really mark any of these as the answer, and I have no clue what actually did it, the config simply started working. I restarted all services and my machine many times during this process, so it was nothing quite so simple. I guess this is what I get for developing on a Windows machine. I considered deleting the question, but there's some valuable troubleshooting in the answers, so better to leave it live.

share|improve this answer
    
Thanks for reporting back! It's too bad there wasn't a clear solution, but good to know the resolution anyway. –  Lindsey D Jun 24 at 16:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.