Remote MySQL not working with Laravel Config - php

Our database server has grant access to all local ip's with user root and it works well to access them via cli in all local IP. But in our webserver (different machine), Laravel still can't connected to the database, here's the error:
I've tried to clear cache, reinstall the Laravel (install composer, generate new key etc.) change db config drive to sqli, it still error connecting the remote db, but why the DB works when I querying the DB via tinker on that machine, this is so unusual.

If your MySQL database is on a remote server it may well be one of the following issues:
Firewall Block, the server with the MySQL service may be behind a firewall that is set to block external access to the port that MySQL operates on.
MySQL User Permissions, if the MySQL service is not sat behind a firewall then the next cause may be that the user has only localhost access permissions.
You should try to log in to your remote server and, from there, connect to your database via some shell command to verify you actually can.

Security-Enhanced Linux (SELinux) is a Linux kernel security module that provides a mechanism for supporting access control security policies, including United States Department of Defense–style mandatory access controls (MAC).
httpd_can_network_connect (HTTPD Service):: Allow HTTPD scripts and modules to connect to the network.
Run as root:
# setsebool -P httpd_can_network_connect 1

Solved: php can't connect to mysql with error 13 (but command line can)
Now Laravel can connect the DB, By executing: setsebool httpd_can_network_connect=1 in Database Server. But I still don't understand why. Is there any reasonable explanation?

Related

Laravel Create mysql SSH Tunnel on windows10 programmatically?

Trying to setup a local Windows10 environment for Laravel 6 and need to connect with SSH to a remote linux mysql server.
Standard configuration doesnt seem to support a way to connect with SSH tunnels, but I did find a workaround that I need help finishing.
It seems the only way to get the connection going is through port forwarding, which I am able to do with gitbash/windows terminal
ssh -i /c/Users/MyUserName/MyKeyFolder/sitemaster_ssh -fNg -L 3307:127.0.0.1:3306 linuxUser#remoteHost.com
After running that manually in terminal / gitbash, I am able to use localhost with port 3307 successfully for my database configuration in Laravel and visit my site locally
Ideally, I would like to programatically check this port before laravel tries to access port 3307 on startup as well as some type of detection for later calls and reopen only if necessary.
I used this article which lays out the basics but is unclear as to whether the command line code can only be run manually
or if can be run with a script somewhere, somehow.
Laravel MySql DB Connection with SSH
I have tried doing things like
exec('ssh -i /c/Users/MyUserName/MyKeyFolder/sitemaster_ssh -fNg -L 3307:127.0.0.1:3306 linuxUser#remoteHost.com');
as well as putting in its own script nameed port_fwd_3307.sh and then running
exec('port_fwd_3307.sh');
I can navigate to the sh file from terminal and confirm it runs and the script is calling it from the proper directory, but still, I get no connection and no feedback as to whats going on other than server actively refused (but I know its a port issue, since it resolves successfully when run manually).
I have placed that command in the bootstrap/app.php file, as well as tried it in the config/database.php and public/index.php
before laravel even starts and none of this works.
Not sure if this is possible at all, but if it is, what specific part of the framework would it need to be located in to ensure the port is forwarded prior to the database connection?

Connection Firebird DB via SSH

I am creating an application with Laravel and Firebird 2.5 and wants to run it on a DigitalOcean server structure. We have 2 servers for the application, one for the web services (lets call it www) and one for the database services. I did a successful setup with Mysql and it works well but we all know Firebird is a rough one. So this is what we did so far:
Install a LEMP stack in www and Firebird 2.5 SuperServer. As I said these are digital ocean servers, both runs Ubuntu 14.04.
We created an SSH tunnel between the two server with the following structure:
ssh -L 9500:127.0.0.1:3050 username#db_server_ip_address (Private address)
But when we tried to hook up the application on www with the database server got the following error:
unavailable database
In the Laravel Configuration file used the following set up:
DB_HOST=127.0.0.1
DB_DATABASE=/home/username/database.gdb
DB_USERNAME=username
DB_PASSWORD=password
The credentials are correct, we can use it on the remote (db) server.
What do you think what could be the problem? Is it the SSH tunneling?
Warning: I don't use SSH that much, and have never used SSH tunneling, my answer is based on looking at documentation.
The ssh -L 9500:127.0.0.1:3050 does not do what you think it does:
Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side. This works by allocating a socket to listen to either a TCP port on the local side, optionally bound to the specified bind_address, or to a Unix socket. Whenever a connection is made to the local port or socket, the connection is forwarded over the secure channel, and a connection is made to either host port hostport, or the Unix socket remote_socket, from the remote machine.
(from ssh(1))
In other words, as far as I can tell the proper command would be:
ssh -L 9500:<ip-address of the Firebird server>:3050
The second problem seems to be that your Laravel config does not specify a port, so it is likely still trying to connect to port 3050 (the Firebird default port), instead of port 9500 that you configure. I don't know Laravel, but a property DB_PORT=9500 seems logical (but maybe these properties are specific to your own deployment, in which case you may need to do some more work).

Is the database created in MySQL with terminal different from the database that show in the PhpMyAdmin?

I have created a database in MySQL with terminal, and I tried to find it in PhpMyAdmin I couldn't find it. My question is, are those two databases different from each other or did I do something wrong that is why it didn't show up?
PHPMyAdmin is just a web GUI that acts as a client for interacting with the MySQL Server. In the terminal, you access the server using the mysql command-line client, and create a database on the server.
Now, both of these clients can be configured to use any MySQL server that they can access over the network. The command-line client defaults to connecting to 127.0.0.1 on port 3306 (or /var/run/mysql.sock but that's unimportant in this case). In PHPMyAdmin,config.php defines what server you connect to. I don't remember the defaults, but servers are configured via $cfg['Servers']. This document explains the configuration. So, you should be able to look at config.php and determine whether or not you're connecting to 127.0.0.1 port 3306.
Another possible reason you might not see the DB in PHPMyAdmin is if only_db is set in config.php. Look for $cfg['Servers'][$i]['only_db'].
Also ensure that you actually created the database and that it shows in SHOW DATABASES; in the mysql command-line client.
Hi #mayleficent Your phpMyAdmin comes as a package with php, mysql and extra apps. So if you have installed any mysql server explicitly that would be totally differant from mysql in xamp/wamp package. So if you remove that explicitly installed mysql you can access the mysql linked with phpmyadmin. To goto terminal associated with phpmyadmin, then you have to browse to the folder in your xamp/wamp. the path appears as shown below.
C:\xampp\mysql\bin>mysql -u root -p admin

Is it possible to use the same MySQL database on Windows (XAMPP) and Linux (LAMP)?

I have dual boot mode with Windows 7 and Ubuntu 10.
On Windows 7 I have XAMPP installed, on Linux I have LAMP installed.
Is it possible to force the MySQL DB servers installed onto different operating systems (even though they would be of identical versions, they are different) to use the same physical files?
So in dual boot mode, independently from the fact that I ran another operating system, I would be able to use the same physical data. Sometimes I'd like to switch OS but it would be great to be able to use the same databases.
in your my.ini (in Windows it's located somewhere like C:\Program Files\MySQL\MySQL Server 5.1. It's the main configuration file for MySQL) file, you should have this line:
datadir="C:/ProgramData/MySQL/MySQL Server 5.1/Data/" for example
change it in both Windows and Linux Ubuntu to point to one single physical folder (on a partition with a file system which Windows could recognize). It will work. File formats are identical.
Whether you boot from Ubuntu, or Windows 7, it won't matter, 2 different builds of MySQL will be looking for data in the same place. Once data is modified in Windows environment, you boot up from Ubuntu and the data is there, modified.
Whilst not ideal, this should be fine as long as:
You're using identical versions of MySQL on both operating systems.
You shutdown mysqld before you copy the data files across. (If you're going to be copying the data files between partitions rather than keeping them on a shared fat32 partition).
In essence, as long as MySQL is running on an architecture of the same "endianness", then the file formats should be transferable.
As a suggestion, you could just close ICQ, etc. and use the free memory to run Ubuntu within a VirtualBox virtual machine on top of Windows 7 - hence ensuring you can trivially access your development environment without having to restart, etc.
This is actually quite a nice set up as it means you can use a Windows development environment if you want and simply host the site web site data on a Samba mount on the Ubuntu virtual machine.
As long as the data is sharedable/reable/writeable on both OS, and both file format are identical on both OS, it should be doable
The first problem I can imagine is the case insensitivity in windows.
so, convert your database/table ti camel_case (or camelcase) if you are always using CamelCase.
More information to read up - http://dev.mysql.com/doc/refman/5.0/en/limits-windows.html
Of course you can.
But I dont think, it is possible for your dual boot system.
Lets assume, your MySQL server is installed under Win7 machine. You can perfectly access it from Win7 enviroment, and even from Linux enviroment (if you do not deny access in your MySQL server settings).
The system, which is your MySQL installed under, has to be running! Then you can access it from multiple systems if allowed ;-)
I goggled a lot of time and found that running a Linux kernel is a solvable way and feasible solution.
Vagrant
It is a tool for building and managing virtual machine environments in a single workflow. The main reason why I argue you using vagrant is that it is not too heavy and does not swallow much of your computer resources. I believe you get through Vagrant documentation that will allow you to launch a Linux based machine on your physical machine.
Let's assume that the host machine is assigned IP 192.168.1.2 and the virtual machine has the IP address as 192.168.1.10, and make sure that the host and guest machine could see each other. Please read thoroughly Networking section to customise the network configuration.
Verifying connection between host and guest machine
Install MySQL Server
MySQL is a database management system. Basically, it will organise and provide access to databases where our site can store information.
Open a terminal in the machine has been set up from the above step. Run the following command:
sudo apt-get install mysql-server-5.6
Notes: It's up to the version of Linux distribution installed, the above command would be adjustable to suit your need. For install, I used the core of Ubuntu 14.04, see the link.
During the installation, your server will ask you to select and confirm a password for the MySQL root user. This is an administrative account in MySQL that has increased privileges.
Verifying the installation
From a terminal in the guest machine (i.e. the virtual machine), run the following command:
mysql -u root -p
will ask the MySQL password, then provide the one you have set up during MySQL Server installation. The following is the screenshot if you feed correct information to MySQL server.
Turn MySQL Server remotely accessible
Because we need a centralised database server where other computers could access and connect to the database of interest. Again, open a terminal and run the following MySQL commands:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'192.168.1.2' IDENTIFIED BY PASSWORD '*4ACFE3202A5FF5CF467898FC58AAB1D615029441' WITH GRANT OPTION;
GRANT PROXY ON ''#'' TO 'root'#'192.168.1.2' WITH GRANT OPTION;
FLUSH PRIVILEGES;
which 192.168.1.2 is the IP address of the host machine and hashed string of password gets from user table in mysql database.
Okay. You might take a rest and enjoy your drink if there is no issue so far.
Verifying the remote access
From a terminal on the host machine, fire up the following command:
mysql -h192.168.1.2 -uroot -p
which is asking you to enter the password. If the root credential is correct, you would see a screenshot like the above one. One thing needs to be paid attention is to add -h192.168.1.2 following mysql command because we are not in the machine where MySQL server is installed.
All in all, we have set up a MySQL server used for both host and guest machine. In reality, if I have another machine assigned 192.168.1.3 can also connect to the database server and exchange data between server and client.

Connect to remote MySQL database with PHP using SSH

I have a remote database I'd like to connect to from PHP running locally. The database doesn't allow remote connections so ordinarily I SSH into the box and use it from the command line, but that's not really a long term solutions.
I have SSH access, I have MySQL access once I SSH in, but I don't know how to get PHP into that workflow. If I could make this work within MAMP, that would be great, too.
For developing or testing, you can use ssh command to setup tunnel first and then access the remote database as a local one. The steps are:
1) setup tunnel with ssh command. command format: ssh -L [local port]:127.0.0.1:[remote mysql port, by default, it is 3306] [user]#[remote mysql server ip]. sample:
ssh -L 3307:127.0.0.1:3306 ford#134.11.21.89
2) keep the command window alive
3) You can access the remote database by mysql string: mysqli://[user]:[password]#127.0.0.1:3307/[database name]
Connect to a MySQL server over SSH in PHP
You could set up a SSH tunnel and then point your php connection code to a local port which is forwarded through the tunnel. Under Windows you might use putty; for Mac there will be similar solutions.
If this is for development, the suggested solution by alex is the way to go; set up a ssh-tunnel.
The tunnel will redirect your 127.0.0.1:3306-requests to the remote machine. The remote machine will also belive the requests will come from 127.0.0.1 (locally).
However, you may encounter problems if your server (shared host? please specify) doesn't allow mysql-connections from 127.0.0.1 (quite commonly only localhost are allowed). There's a tiny difference in those, and it will inhibit your tunnel from reaching the remote mysqld.
Just google tunneling, set it up, and use 127.0.0.1 from your php-connection strings.
regards,
//t

Categories