Connecting to MySQL Server on Ubuntu not possible - php

I have set up a raspberry PI 4 with Ubuntu. Apache, PHP, mysql and phpmyadmin are installed and working fine. So when I am trying to connect from another network with my windows device over MySQL Workbench there is an error occuring.
error
Checked: port forwarding on port 80 is open. checked binding ip in mysqld.conf (maybe I made a mistake there) when I try to change the port in this configuration file to 80 and restart "sudo service mysql restart" this error occure "Job for mysql.service failed because the control process exited with error code"
So maybe mysql is not listening to port 80. I have checked sudo lsof -i :80. Only found records from apache listening to port 80.
I am trying for three days now to connect to the database.
Thank you for your help guys!

By default, mysql listens on port 3306, so if you are trying to connect directly to mysql, that could be the issue. Port 80 and 443 are for http/https respectively.
You can't set the port to port 80 because apache needs that port to listen for http requests.
Try setting the port back to 3306 on the mysql host machine and seeing if that works. You may also need to open that port on the host machine as well.

Related

local server can't connect to remote postgresql database using PHP, but local terminal can connect

The title might make this seem like a duplicate of some existing questions here on SO, but its kind of not in a way, in my opinion.
8140863, 30617357, 27749691
But I can't for the life of me make the answers to these threads work, instead of asking questions on those threads, I would like to create my own question, I'd like to discuss my setup and then my problem, and then what I've tried
My server setup is this, I have a local(an actual physical machine) and a VPS I'm renting, both are running at Centos7.x, PostgreSQL10, Php 7.x, and Python-2.x, BOTH can ping and ssh each other through a VPN just fine.
Both can connect to their own respective databases just fine through PHP, Python, PSQL Terminal
On my VPS Server, I can connect to my Local Server's PostgreSQL database using Python(psycopg2), PHP(php-pgsql) and Terminal(psql) totally fine.
On my local server, I can connect to my VPS PostgreSQL using Python(psycopg2) and Terminal(psql) EXCEPT through PHP(php-pgsql)
Whenever I connect to VPS's PostgreSQL through PHP, I'm getting this error like everyone else
pg_connect(): Unable to connect to PostgreSQL server:
could not connect to server: Permission denied
Is the server running on host "123.456.0.789" and accepting TCP/IP connections on port 5432?
Here are what I've done.
On postgresql.conf set listen_address = "*" -- this is OK
Added pg_hba entries for both -- this is OK (python and terminal can connect on both)
Port 5432/tcp is both enabled and allowed -- this is OK
Selinux is disabled -- this is the answers for the thread referenced above, but it doesn't work for me even after reboot of course.
these are the result of my netstat
#netstat -na | grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
tcp6 0 0 :::5432 :::* LISTEN
tcp6 0 94 ::1:55110 ::1:5432 ESTABLISHED
tcp6 0 0 ::1:5432 ::1:55060 ESTABLISHED
tcp6 0 0 ::1:5432 ::1:55110 ESTABLISHED
tcp6 12 0 ::1:5432 ::1:55108 ESTABLISHED
tcp6 0 12 ::1:55108 ::1:5432 ESTABLISHED
tcp6 0 0 ::1:55060 ::1:5432 ESTABLISHED
unix 2 [ ACC ] STREAM LISTENING 31102 /var/run/postgresql/.s.PGSQL.5432
unix 2 [ ACC ] STREAM LISTENING 31104 /tmp/.s.PGSQL.5432
iptables
#iptables-save | grep 5432
-A IN_public_allow -p tcp -m tcp --dport 5432 -m conntrack --ctstate NEW,UNTRACKED -j ACCEPT
getenforce
#getenforce
Disabled
Selinux
#/usr/sbin/sestatus | grep SELinux
SELinux status: disabled
#sudo setsebool -P httpd_can_network_connect_db 1
setsebool: SELinux is disabled.
postgresql.conf
listen_addresses = '*'
port = 5432
pg_hba.conf (i just changed the ip for posting here) I tried using trust, but its still the same
# "local" is for Unix domain socket connections only
local all all password
# IPv4 local connections:
host all all 127.0.0.1/32 password
host all all 123.456.0.1/32 password
host all all 123.456.0.11/32 password
host all all 123.456.0.20/32 password
# IPv6 local connections:
host all all ::1/128 password
edit start: My PHP pg_connect code (I just changed the IP just for this post, but it points to the VPS IP)
$pgcon = pg_connect("dbname=database1 user=some_user password=some_password host=123.456.0.789");
edit end : ==
I would like to emphasize that BOTH servers can connect on each others and their own PostgreSQL database through other means, with the exception of my Local Server, it couldn't connect to VPS PostgreSQL using PHP.
VPS To Self(localhost connection)
VPS to Self via PHP is OK
VPS to Self via Python is OK
VPS to Self via PSQL(Terminal) is OK
VPS To Local Server(Connecting using Local Server's VPN IP)
VPS to Local Server via PHP is OK
VPS to Local Server via Python is OK
VPS to Local Server via PSQL(Terminal) is OK
Local Server to Self(localhost connection)
Local Server to Self via PHP is OK
Local Server to Self via Python is OK
Local Server to Self via PSQL(Terminal) is OK
Local Server to VPS(Connecting using VPS's IP by VPN and Public IP)
Local Server to VPS via Python is OK
Local Server to VPS via PSQL(Terminal) is OK
Local Server to VPS via PHP Is NOT OK
I'm really sorry for the long thread, I wanted to give as much information as I can.
Let's split error by types. I'd made several tests with your case and what I found:
If we mess with connection parameters (i.e. wrong port, or IP address) we get the error "could not connect to server: Connection refused" or "Operation timed out
Is the server running on host "128.0.0.1" and accepting")
If we mess with credentials, we get the error "FATAL: password authentication failed for user ..."
Permission denied can be thrown only if you have not permissions to open a TCP connection from your PHP library.
Pls, double-check if you disable SElinux on your Local Server as for me, this is still the closest workaround for your problem.

Centralised Mysql DB

I need solution for Centralized DB.
We have DB in one Hosting server (Azure Redhat virtual server) and we need to use the same db in other hosting server too. So it will be centralized DB for both server. I can not use it in navicat also if I use IP of Mysql DB.
I have tried using ( bind-address= IP address of the server where db is hosted ) in My.cnf file but it doesn't allow remote access. to use it in other hosting server nor in any mysql client like navicat / Heidi sql.
Also I have added the port 3306 rule in azure server for inbound security.
Thanks in advance for help.
As Azure VMs disable ICMP and we can use SSH tunnels to allow outside access to internal network resources.
Please try to follow this post, created a SSH tunnel in VM which hosted the MySQL server:
Open port 3306, so a remote client can connect to your MySQL Server. Run the following command to open TCP port 3306
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
Now let’s check if the port 3306 is open by running this command:
sudo netstat -anltp|grep :3306
Create a SSH tunnel for port 3307
sudo ssh -fNg -L 3307:127.0.0.1:3306 azurevmuser#servername
Create an endpoint for the port 3307 rule in azure server for inbound security. Now your Database host name is <VM_ip>:3307
Any further concern, please feel free to let me know.

MySQL startup error - port is already in use

I get the following error while trying to start XAMPP:
12:34:26 PM [mysql] Attempting to start MySQL service...
12:34:38 PM [mysql] Problem detected!
12:34:38 PM [mysql] Port 3306 in use by ""C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld" --defaults-file="C:\Program Files\MySQL\MySQL Server 5.1\my.ini" MySQL"!
12:34:38 PM [mysql] MySQL WILL NOT start without the configured ports free!
12:34:38 PM [mysql] You need to uninstall/disable/reconfigure the blocking application
12:34:38 PM [mysql] or reconfigure MySQL and the Control Panel to listen on a different port
12:34:38 PM [mysql] Attempting to start MySQL service...
The log cleraly states that another application is listening on port 3306, so it is not possible to start MySQL since 3306 is the configured port for it (default port for MySQL connections). This application that already listens on por 3306 is another instance of MySQL that's probably configured to start automatically.
If you are using Windows, click the Start button, then Execute, then type services.msc and press [Enter]. Look for the running MySQL service, stop it, and configure it to prevent its launching on system startup. After that you should start XAMPP normally.
open xampp control panel.
click on "config" button.
click on "service and port settings".
click on "mysql" tab.
change the value labeled "Main Port" to 3307
So, mysql is listening 3306 port by default. According to your error,some service has already used it.
Depends on your OS,check who are using it. For example for netstat -lp | grep 3306, you can close this service and restart your mysql or change to another port OR you an change mysql default port to listen in your my.cnf file.
may be you have previously installed xampp app and left in before complete installation and then reinstalled it before uninstalling previous one just uninstall privious one or uninstall both and reinstall
Well my friend told me to reinstall xampp in C drive and it did worked but after some time i was facing same problem again so i followed this steps hope it will be helpfull.
Open cmd as administrator
If there's nothing important running on port number 3306 kill that process.
enter image description here
Follow this steps:
(In case photo have any error)
after opening cmd as administer run command
netstat -ano |findstr 3306
Then observe the PID at last column if there is only one process running shut it down.
taskkill /F /PID portidnumber
In case you get error even after doing that try to do follow the steps here:
I experienced that if we try to stop mysql before apache it is giving error.
So when you go to stop process or server stop apache first and then mysql.
And don't shutdown xampp without stopping any server
(I found this by web search and some try and error and it's working for me at least)

Can't connect Websocket when using vagrant envirionment

I'm using Vagrant box using puphpet, the environment is PHP 5.5 + ubuntu 12.04 + apache + mysql. My Vagrant VM ip: 192.168.11.11, local machine hosts points to 192.168.11.11 reactphp.dev, and it works.
And, I'm using this code: https://github.com/muuknl/phprealtimechat to test websocket.
Then I visit reactphp.dev in my Chrome browser, and I start server script using: php bin/server.php, and after I type in the username I just got the error:
WebSocket connection to 'ws://192.168.11.11:2000/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
It works well when I changed the websocket ip to `ws://127.0.0.1:2000/', but why it does not work if I change to my VM's ip address?
It's been a while I haven't used PuPHPet, but when I had troubles connecting through the port (minus port 22), I've had to issue the command sudo ufw disable in the vagrant machine.
If uwf isn't the managing firewall of your vm, try sudo service iptables stop, or sudo iptables -F.
Perhaps they've changed the behavior since then, but it's still good to try.
The SSH tunnel (over the opened port 22) might allow you to access the local port 2000, which is proxified over. The actual port 2000 of the VM might be limited due to firewall.
Try it, and tell me if this works. If not, I'll dig in deeper.

Remote connection to MySQL Linux

I opened up ports on my server
(results of netstat -L)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:mysql *:* LISTEN
...
but i can't connect to my server on that port via telnet, or remotely to my MySQL server. I am at my wit's end. How can I allow remote access on this port?
mysql -u -h 'my-server-ip' gives me an error that says '(HY000): Can't connect to MySQL server on 'my-server-ip' (10061)
Any help would be appreciated!
EDIT: SOLVED see my answer below
Are you sure you have the correct port open in iptables? MySQL usually listens on port 3306, but above you have port 3389 (which is usually used for RDP).
I rebooted my server and it worked :S
for any future readers, i opened up port 3306 on my firewall, inbound and outbound. then i commented out the binding_address line in my my.cnf file. i also granted access to remote users using phpmyadmin.

Categories