Can't connect to MySQL server on '192.168.x.yyy' - mysql

I am trying to connect to the database remotely.
I changed the mysqld.cnf to
[mysqld]
bind-address = 192.168.x.yyy
mysqlx-bind-address = 192.168.x.yyy
skip-networking
I checked the ports with iptables -S
-A INPUT -p tcp -m tcp --dport 3306 -c 10 600 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 3306 -c 0 0 -j ACCEPT
I tried to run telnet 192.168.x.yyy 3306
telnet: Unable to connect to remote host: Connection refused
Of course, I called
sudo service mysql restart
The command mysql -u root -p is OK but mysql -h 192.168.x.yyy -u root -p returns
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.x.yyy' (111)
The table mysql.user contains:
mysql> select Host,User from user;
+---------------+------------------+
| Host | User |
+---------------+------------------+
| 192.168.x.yyy | root |
| ...
| localhost | root |
+---------------+------------------+
The ifconfig returns:
enp2s0f0: ...
inet 192.168.x.yyy netmask 255.255.255.0 broadcast 192.168.x.255
I am running Ubuntu 20.04.1 LTS and mysqld 8.0.23

In your quoted mysqld.cnf, you have included the skip-networking rule which disabled all external network access to the MySQL server.
If remote access is required, you should remove this rule and restart your mysql server process.
See the documentation for further details.

Related

Connecting to MariaDB via TCP returns socket error

I'm trying to connect to a local installation of MariaDB 10xx via the mysql command line application but despite specifying a host and a port I get an error the client cannot connect via the unix socket.
This is my my.cnf:
⟩ sudo vi /opt/local/etc/mariadb-10.1/my.cnf
# Use default MacPorts settings
!include /opt/local/etc/mariadb-10.1/macports-default.cnf
[mysqld]
socket=/private/tmp/mysqld.sock
port=3306
log_error=/private/tmp/mysql_error.log
log_warnings=3
general-log
general-log-file=/private/tmp/mariadb_queries.log
log-output=file
This is the command line I use:
mysql -u root --host localhost --port 3306 --password
And this is the error:
⟩ mysql -u root --host localhost --port 3306 --password
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mariadb-10.1/mysqld.sock' (2 "No such file or directory")
I'm trying to check whether MariaDB is listening to TCP connections.
So I asked in the MariaDB IRC channel and apparently passing localhost to MariaDB causes it to use the unix socket. Replacing --host localhost with --host 127.0.0.1 did the trick.

cannot connect to remote mysql server

I have installed mysql lampp on my server.
When i try to connect remotely using this command:
mysql -h SERVER_IP -u USER -p
it returns me error :
ERROR 2003 (HY000): Can't connect to MySQL server on 'SERVER_IP' (110)
I have setting bind-address to 0.0.0.0 in /opt/lampp/etc/my.cnf file,
I have also give this iptables rule to allow access to mysql port on 3306
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
Nmap on local server give the following output:
PORT STATE SERVICE
#nmap -p 3306 localhost
PORT STATE SERVICE
3306/tcp open mysql
but when nmap from outside the server network i.e using external ip, nmap give the following output:
#nmap -p 3306 SERVER_IP
PORT STATE SERVICE
3306/tcp filtered mysql
output from netstat -ntulp |grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 17946/mysqld
the port is listen according to netstat but can't accept connection from outside the network (remotely)
What is wrong here?
try one thing that go to your mysql server and execute below command-
service iptables stop
Now check your connection.

Granting remote access to MySQL database

I am trying to grant remote access to a mysql database. However I think I am missing a step somewhere.
Server is a VM - Ubuntu 12.04.5 - inet addr:134.226.38.147
mysql Ver 14.14 Distrib 5.5.38, for debian-linux-gnu (x86_64) using readline 6.2
Firstly I create the database on the remote server. I then grant wildcard access to all databases and tables for the user brendan. By using '%' I should have no problem connecting from my computer in college.
mysql> CREATE DATABASE foo;
mysql> GRANT ALL ON *.* TO 'brendan'#'%' IDENTIFIED BY 'mypassword';
I then open the port using
iptables -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
iptables-save | tee /etc/sysconfig/iptables
From what I read the above should work, however when I try to test the connection from my desktop this is what I get
localhost:~ brendan$ mysql -u brendan -h 134.226.38.147 -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '134.226.38.147' (61)
localhost:~ brendan$
or
localhost:~ brendan$ echo X | telnet -e X 134.226.38.147 3306
Telnet escape character is 'X'.
Trying 134.226.38.147...
telnet: connect to address 134.226.38.147: Connection refused
telnet: Unable to connect to remote host
localhost:~ brendan$
What am I missing?
Any help is much appreciated.
EDIT
my.cnf
I was unsure If I should comment out the bind-address = 127.0.0.1 which was already there
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
# ---- You added the below line ----------
bind-address = 134.226.38.147
I then restart with
sudo service mysql restart
Open your my.cnf file:
sudo vim /etc/mysql/my.cnf
Comment out the bind-address in your my.cnf.
like so: #bind-address = 127.0.0.1
Then restart mysql server so that the changes to the my.cnf file will take affect.
sudo service mysql restart
or
sudo /etc/init.d/mysql restart
You can read more here at DigitalOcean or rtcamp.
Now, a user will be able to connect to the Mysql database server remotely as long as they have proper user credentials.

Connect to mysql on Amazon EC2 from a remote server - ERROR 2003

cant get it to work. this is what i've done so far:
mysql> CREATE USER 'admin'#'localhost' IDENTIFIED BY 'xxxxxx';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'#'localhost' WITH GRANT OPTION;
mysql> CREATE USER 'admin'#'%' IDENTIFIED BY 'xxxxxx';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'#'%' WITH GRANT OPTION;
in AWS Console -> EC2 -> Network & Security -> Security Groups -> quick-start-1 -> Inbound tab -> Choose 'MYSQL' from drop down -> Add Rule -> apply
edit /etc/my.cnf and added bind-address, then
sudo /etc/init.d/mysqld restart
sudo /sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT
sudo service iptables save
and finally from my computer:
[nir#dhcppc4 ~]$ mysql -h xxx.xxx.xxx.xxx -u admin -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (113)
EDIT:
running netstat -lp | grep mysql (I dont see port number in the output):
tcp 0 0 *:mysql *:* LISTEN -
unix 2 [ ACC ] STREAM LISTENING 16003 - /var/lib/mysql/mysql.sock
added in /etc/my.cnf
port=3306
and now netstat -lp | grep mysql is
tcp 0 0 *:mysql *:* LISTEN -
unix 2 [ ACC ] STREAM LISTENING 37757 - /var/lib/mysql/mysql.sock
/etc/my.cnf file:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
port=3306
bind-address = 0.0.0.0
#skip-networking
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
plus i tried to connect through other computer and i could not connect, so i guess its not a problem on the connecting computer
and i got access denied my telnet:
[nir#dhcppc4 ~]$ telnet xxx.xxx.xxx.xxx 3306
Trying xxx.xxx.xxx.xxx...
telnet: connect to address xxx.xxx.xxx.xxx: No route to host
** i'm also trying to solve it in amazon ec2 forum
SOLVED
the problem:
i have in the iptables this line
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
and this line only append the rule to the end of the list so the reject catch it first
sudo iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
to solve it i needed to put the rule higher in the chain, i.e. use -I switch like this:
sudo iptables -I INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT

How to test which port MySQL is running on and whether it can be connected to?

I have installed MySQL and even logged in there as a user.
But when I try to connect like this:
http://localhost:3306
mysql://localhost:3306
Neither works. Not sure if both are supposed to work, but at least one of them should :)
How can I make sure that the port is indeed 3306? Is there a linux command to see it somehow?
Also, is there a more correct way to try it via a url?
To find a listener on a port, do this:
netstat -tln
You should see a line that looks like this if mysql is indeed listening on that port.
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
Port 3306 is MySql's default port.
To connect, you just have to use whatever client you require, such as the basic mysql client.
mysql -h localhost -u user database
Or a url that is interpreted by your library code.
Using Mysql client:
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
grep port /etc/mysql/my.cnf ( at least in debian/ubuntu works )
or
netstat -tlpn | grep mysql
or
mysql -u user_name -puser_pass -e "SHOW variables LIKE 'port';"
verify
bind-address 127.0.0.1
in /etc/mysql/my.cnf to see possible restrictions
netstat -tlpn
It will show the list something like below:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1393/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1859/master
tcp 0 0 123.189.192.64:7654 0.0.0.0:* LISTEN 2463/monit
tcp 0 0 127.0.0.1:24135 0.0.0.0:* LISTEN 21450/memcached
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 16781/mysqld
Use as root for all details. The -t option limits the output to TCP connections, -l for listening ports, -p lists the program name and -n shows the numeric version of the port instead of a named version.
In this way you can see the process name and the port.
Try only using -e (--execute) option:
$ mysql -u root -proot -e "SHOW GLOBAL VARIABLES LIKE 'PORT';" (8s 26ms)
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
Replace root by your "username" and "password"
Both URLs are incorrect - should be
jdbc:mysql://host:port/database
I thought it went without saying, but connecting to a database with Java requires a JDBC driver. You'll need the MySQL JDBC driver.
Maybe you can connect using a socket over TCP/IP. Check out the MySQL docs.
See http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
UPDATE:
I tried to telnet into MySQL (telnet ip 3306), but it doesn't work:
http://lists.mysql.com/win32/253
I think this is what you had in mind.
A simpler approach for some : If you just want to check if MySQL is on a certain port, you can use the following command in terminal. Tested on mac. 3306 is the default port.
mysql --host=127.0.0.1 --port=3306
If you successfully log in to the MySQL shell terminal, you're good! This is the output that I get on a successful login.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9559
Server version: 5.6.21 Homebrew
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
3306 is default port for mysql. Check it with:
netstat -nl|grep 3306
it should give this result:
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
For me, #joseluisq's answer yielded:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
But it worked this way:
$ mysql -u root#localhost -e "SHOW GLOBAL VARIABLES LIKE 'PORT';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
you can use
ps -ef | grep mysql
On a mac os X, there are two options. netstat or lsof
Using netstat will not show the process on Mac OS X. so using netstat you can only search by port.
Using lsof will show the process name.
I did the following as I was encountering port conflicts (docker containers):
netstat -aln | grep 3306
Outputs:
tcp46 0 0 *.3306 *.* LISTEN
sudo lsof -i -P | grep -i "LISTEN" | grep -i 3306
Outputs:
mysqld 60608 _mysql 31u IPv6 0x2ebc4b8d88d9ec6b 0t0 TCP *:3306 (LISTEN)
If you are on a system where netstat is not available (e.g. RHEL 7 and more recent Debian releases) you can use ss, as below:
sudo ss -tlpn | grep mysql
And you'll get something like the following for output:
LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=5307,fd=14))
The fourth column is Local Address:Port. So in this case Mysql is listening on port 3306, the default.
I think the most appropriate way of finding the port associated with mysql server using lsof command line tool.
lsof -i -n -P | grep mysql
mysqld 1556 prince 10u IPv4 0x6ad3fd78a9051969 0t0 TCP 127.0.0.1:3306 (LISTEN)
I agree with #bortunac's solution. my.conf is mysql specific while netstat will provide you with all the listening ports.
Perhaps use both, one to confirm which is port set for mysql and the other to check that the system is listening through that port.
My client uses CentOS 6.6 and I have found the my.conf file under /etc/, so I used:
grep port /etc/my.conf (CentOS 6.6)