I have three PCs that I connect over LAN.
- The first PC to database;
- The second to web service CodeIgniter RESTful;
- The third to client.
Now how can I connect web service CodeIgniter server to database, I've set the IP on aplication/config/database.php,
$db['default']['hostname'] = 'localhost';
I've changed localhost
with the IP that is used by the first pc:
$db['default']['hostname'] = '192.168.1.10';
But, the result :
A database error occurred
Unable to connect to your database server using the provided settings.
If this is MySQL, networking of the database server is not enabled by default. To enable networking in MySQL you need to find the active MySQL configuration file named
my.cnf
. And edit it.I am going to explain how to do this on Ubuntu 12.04, but the instructions are similar for most any Linux install.
Enable MySQL Networking
First, open the file using an editor like
nano
. You might need to run the command viasudo
:Then look for the area in your configuration file with the
bind-address
option:Now change that
bind-address
setting to the following:With that done save the file & restart MySQL like this:
And now your MySQL database will be able to take non-localhost connections from remote machines.
Make Sure the MySQL Port
3306
is OpenThat said, even with networking enabled, you should still check if you are able to connect to the remote machine from the command line using a networking tool such as
nmap
. You might have a firewall on192.168.1.10
blocking MySQL port3306
so you need to check if it is open or closed like this:And if port
3306
is open, this will be the response; note theopen
underSTATE
:But if port
3306
is closed, you will get this; note theclosed
underSTATE
:Check Your MySQL User Grants
Now with that all done, your MySQL database server should be accessible on the network. But that said, you need to make sure your database user you are using is allowed to connect from a remote machine on your LAN. So login to MySQL & run this command to see what grants the user might have:
This will show you a list of users & hosts connected to those users in MySQL. The thing is most users are only granted access privileges to
localhost
or127.0.0.1
. Some are granted the wildcard host of%
. You need to look at that list and see if the user you want to use has a wildcard host (%
) or a specific IP address connected to them. You can check a user’sGRANT
s by running a line like this; of course change[your_database_user]
and[hostname]
to match your settings:That should show you a list of available
GRANT
s for a user on a specific host. If they haveGRANT
s that would allow remote access—such as using wildcard%
host—then you are all set. If they don’t have anyGRANT
s you can run the following MySQL commands; of course change[your_database]
and[your_database_user]
to match your settings:Both
GRANT
lines apply a fairly standard set of access rights to a database. The first line applies them to any connection in the LAN network range of192.168.x.x
. The second line applies them to any connection in the LAN network range of10.x.x.x
. I like to do that to cover all bases on internal networks. The lastFLUSH PRIVILEGES;
line basically tells MySQL to reload the user privileges tables which allow those grants to take effect.