Megandi
Megandi

Reputation: 104

laravel connect to online database

I have offline application (installed in localhost) but database in online server.. because i want to make something like synchronizer...

I've tried to set my database.php like this

       'mysqlserver' => array(
            'host'      => '103.38.103.142',
            'port'      => '212',
            'driver'    => 'mysql',
            'database'  => 'lgspsb',
            'username'  => 'xxxxxxx',
            'password'  => 'xxxxxxx',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

but error like this...

SQLSTATE[HY000] [2013] Lost connection to MySQL server at 'reading initial communication packet', system error: 0 

How I can't fix that? thanks before... and sorry for my bad english

Upvotes: 2

Views: 5576

Answers (1)

num8er
num8er

Reputation: 19372

I'll put answer here for people that came here when looking for solution by Your question.

After doing:

telnet 103.38.103.142 212

I saw that the app that listens on 212th port is not mysql rdbms.

here is screenshot: http://joxi.ru/Vm6kkLCxn16Q2Z

I also saw that it's ubuntu.

So quick I'll make process of fixing Your issue step by step.

1) switch to root:

sudo su

2) edit /etc/mysql/my.cnf file and make sure it has binding under mysqld section (screenshot: http://joxi.ru/zANQQJhlqXn329):

bind-address = 0.0.0.0

3) restart Your mysql server:

service mysql restart

4) get in mysql console in terminal as mysql root user (screenshot: http://joxi.ru/a2X77bSyOM3Rmg):

mysql -u root -p

5) create remote user for Your database and flush privileges:

GRANT ALL ON lgspsb.* TO 'lgspsb_remote'@'%' IDENTIFIED BY 'somehardpassword';
FLUSH PRIVILEGES;

6) change database connection params:

   'mysqlserver' => array(
        'host'      => '103.38.103.142',
        'port'      => '3306',
        'driver'    => 'mysql',
        'database'  => 'lgspsb',
        'username'  => 'lgspsb_remote',
        'password'  => 'somehardpassword',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

That's all (:

p.s. use .env file to avoid changing database config every-time. screenshot: http://joxi.ru/ZrJEEJh17ROwAj

p.s. of course it's for dev purposes. In production it's better to listen on concrete network interface that has more secure environment (for example to have pptp vpn connection and bind on pptp0 interface's ip)

For digitalocean lovers:
in digitalocean I have droplets.
I do have separate database servers that listen on internal network interface between droplets
and app servers that connect to them using same internal network.

Upvotes: 1

Related Questions