Hey there,
I’m trying to use a database with kirby. But I might have misunderstood the docs.
Configuring the db-connection in site/config/config.php is enough, I thought. Like the example from the docs:
return [
'db' => [
'host' => '127.0.0.1',
'database' => 'kirby',
'user' => 'root',
'password' => 'root',
]
];
But no connection was established. Then I tried
- Db::connect(); no reaction
- Db::connect([]); (the docs say: Pass
[]
to use the default params from the config)
This time I get an error saying The mysql connection requires either a “host” or a “socket” parameter"
My last try is putting the db-credentials into the array of db::connect() in the page-template.
Now I get connected!
But does that mean I have to put the db-credentials in every page-template or -controller where I need database-access? Where have I gone wrong in the first place?
kind regards,
Ralf
How did you put that first return statement into the config? As a separate return array (won’t work)?
No, just one return-statement.
I have my mysql-db on a raspberry pi, it is not localhost. But I can access it, for instance via phpmyadmin, from a different pc. And I tested the credentials by logging into mysql via ssh. So from this side everything is fine.
This is my config.php:
return [
'debug' => true,
'panel' => [
'install' => true,
],
'routes' => [
[
'pattern' => 'logout',
'action' => function() {
if ($user = kirby()->user()) { $user->logout();}
go('login');
}
]
],
'hooks' => [
// empty for now
],
'db' => [
'host' => '192.168.178.53',
'database' => 'somedb',
'user' => 'someuser',
'password' => 'somepw'
]
];
If your database is accessible, ie. the data you provide is correct and you have the rights to access a database on that server from PHP, then this is the way to go.
Mhm, changed the database to localhost, now it works.
Maybe I made a mistake in the db-configuration on the raspi?
Have to look into that later …
Thank you for the help!
BTW, in fact in MySQL authorization it makes a difference if you connect via localhost
, a dedicated IP (even 127.0.0.1
), or via a socket.
of course. But as I said, I access the database (on 192.168.178.53) with phpmyadmin from my laptop, using the IP, and it works fluently …
I have to look deeper into that later on…