unable to access MySQL server remotely on centOS - mysql

I have centos 6.5 on which MySQL 5.6.21 is running, i am trying to access this MySQL server from my Linux 14.0.4 LTS server but every time it show ERROR 2003 (HY000): Can't connect to MySQL server on '192.192.4.86' (113).
I am able to access centOS using putty and winscp from my windows machine but not able to connect centOS mysql server from any other operating system.
I did following things.
skip-networking is also commented.
GRANT ALL PRIVILEGES ON . TO 'root'#'192.192.4.222' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON . TO 'root'#'127.0.0.1' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON . TO 'root'#'localhost' IDENTIFIED BY 'root';
Add following things in iptables
-A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT
-A INPUT -i eth0 -s 192.192.4.222 -p tcp --destination-port 3306 -j ACCEPT
-A INPUT -s 192.192.4.85 -m tcp -p tcp --dport 3306 -j ACCEPT
port 3306 is also in listing mode i already checked.
netstat -nat |grep 3306
tcp 0 0 :::3306 :::* LISTEN
but not able to access centos MySQL Server remotely.
If any one know where I am wrong or something missing please let me know.
Thanks in advance.

login to your mysql with root username and root password
mysql -u root -proot
Run Below commands to run mysql as remote host
GRANT ALL ON *.* TO mysqluser#'localhost' IDENTIFIED BY 'mysqluser';
GRANT ALL PRIVILEGES ON *.* TO 'mysqluser'#'localhost' WITH GRANT OPTION;
CREATE USER 'mysqluser'#'%' IDENTIFIED BY 'mysqluser';
GRANT ALL PRIVILEGES ON *.* TO 'mysqluser'#'%' WITH GRANT OPTION;
Now Check you are able to login to hostname with username and password
mysql -h yourhostname -u mysqluser -pmysqluser

You could try this out.
Configuration:
By default MySQL will listen for connections only from the local host. To enable remote connections like the one used by mysqlworkbench you will need to modify your /etc/my.cnf and change
bind-address 127.0.0.1
to
bind-address 0.0.0.0
or simply comment out the line completely. Once this is done, restart MySQL with the command.
service mysql restart
MySQL Privilege:
Or, like the ones mentioned on initial answers, you could grant privilege under MySQL itself. Like so,
grant all privileges on *.* to 'root'#'your-public-ip' identified by 'your-password'

Related

Not connecting to MySQL remotely

I've been trying to connect to my MySQL Server remotely and not getting any success. The terminal gets stuck and neither gives me an error message. If I insert the password wrong I get ERROR 1045 (28000): Access denied for user, so I believe MySQL/Server are getting my connection. Locally in the server I can connect with the same user without any issues.
I am connecting to an AWS Ubuntu 16.04, and got the same problem at Azure Debian 8.
Here are the steps I've taken so far:
Commented out #bind-address at /etc/mysql/mysql.conf.d/mysqld.cnf
Ran sudo ufw allow 'MySQL' and sudo ufw allow 'MySQLServer'
Allowed Inbound connection at port 3306
Ran iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
lsof -Pni :3306 returns
mysqld 12118 mysql 16u IPv6 38112 0t0 TCP *:3306 (LISTEN)
Create user with
CREATE USER 'user'#'localhost' IDENTIFIED BY 'password';
CREATE USER 'user'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'user'#'localhost';
GRANT ALL PRIVILEGES ON * . * TO 'user'#'%';
FLUSH PRIVILEGES;
When connecting I'm running mysql -h xxx.xxx.xxx.xxx -u user -ppassword
and the terminal process "freezes".
If anyone can help please.
Instead of commenting out bind-address, change it to:
bind-address = 0.0.0.0
And also drop the -ppassword and make it just -p, that errors out by default.
I was able to connect to a MySQL instance running on an Ubuntu 16.04 server on AWS after following the same steps you followed with that single difference.

Make MYSQL on Linux able to be accessed remotely

I have my database built in MYSQl on Linux.
I want to be able to connect to the server remotely.
General steps for connecting to MySQL remotely:
1.- Create an account for the remote user
GRANT ALL PRIVILEGES ON db_name.* TO 'remoteuser'#'%' IDENTIFIED BY 'secret-passwd'
2.- Open port 3306, this will very depending on your linux distribution, eg: in CentOS 6.5 will be:
iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
3.- Connect remotely, in this example I will use the mysqli client
mysql -h remote.mysqlserver.com -u remoteuser -p
Good luck!

Access mysql remote database from command line

I have a server with Rackspace. I want to access the database from my local machine command line.
I tried like:
mysql -u username -h my.application.com -ppassword
But it gives an error:
ERROR 2003 (HY000):
Can't connect to MySQL server on 'my.application.com' (10061)
What causes this error and how can I connect to the remote database?
To directly login to a remote mysql console, use the below command:
mysql -u {username} -p'{password}' \
-h {remote server ip or name} -P {port} \
-D {DB name}
For example
mysql -u root -p'root' \
-h 127.0.0.1 -P 3306 \
-D local
no space after -p as specified in the Using Options on the Command Line documentation
It will take you to the mysql console directly by switching to the mentioned database.
simply put this on terminal at ubuntu:
mysql -u username -h host -p
Now hit enter
terminal will ask you password, enter the password and you are into database server
edit my.cnf file:
vi /etc/my.cnf:
make sure that:
bind-address=YOUR-SERVER-IP
and if you have the line:
skip-networking
make sure to comment it:
#skip-networking
don't forget to restart:
/etc/init.d/mysqld restart
For Mac, use the following command:
mysql -u app -h hostaddress -P port -D dbname -p
and then enter the password when prompted.
If you want to not use ssh tunnel, in my.cnf or mysqld.cnf you must change 127.0.0.1 with your local ip address (192.168.1.100) in order to have access over the Lan. example bellow:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Search for bind-address in my.cnf or mysqld.cnf
bind-address = 127.0.0.1
and change 127.0.0.1 to 192.168.1.100 ( local ip address )
bind-address = 192.168.1.100
To apply the change you made, must restart mysql server using next command.
sudo /etc/init.d/mysql restart
Modify user root for lan acces ( run the query's bellow in remote server that you want to have access )
root#192.168.1.100:~$ mysql -u root -p
..
CREATE USER 'root'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
If you want to have access only from specific ip address , change 'root'#'%' to 'root'#'( ip address or hostname)'
CREATE USER 'root'#'192.168.1.100' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'192.168.1.100' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Then you can connect:
nobus#xray:~$ mysql -h 192.168.1.100 -u root -p
tested on ubuntu 18.04 server
Try this command mysql -uuser -hhostname -PPORT -ppassword.
I faced a similar situation and later when mysql port for host was entered with the command, it was solved.
try telnet 3306. If it doesn't open connection, either there is a firewall setting or the server isn't listening (or doesn't work).
run netstat -an on server to see if server is up.
It's possible that you don't allow remote connections.
For more details see:
How Do I Enable Remote Access To MySQL Database Server?
I assume you have MySQL installed on your machine. Execute the command below after filling missing details:
mysql -uUSERNAME -pPASSWORD -hHOSTNAME -P3306
mysql servers are usually configured to listen only to localhost (127.0.0.1), where they are used by web applications.
If that is your case but you have SSH access to your server, you can create an ssh tunnel and connect through that.
On your local machine, create the tunnel.
ssh -L 3307:127.0.0.1:3306 -N $user#$remote_host
(this example uses local port 3307, in case you also have mysql running on your local machine and using the standard port 3306)
Now you should be ale to connect with
mysql -u $user -p -h 127.0.0.1 -P 3307
There is simple command.
mysql -h {hostip} -P {port} -u {username} -p {database}
Example
mysql -h 192.16.16.2 -P 45012 -u rockbook -p rockbookdb
you can use the following code to connect to a remote MY SQL database
mysql -u {database_user} -p{db_password} -h {host_name} -P {port_number}
mysql -u admin -p'your_password' -h your-company.aws.com -P 3306
Must check whether incoming access to port 3306 is block or not by the firewall.
this solution worked for me:
On your remote machine (example: 295.13.12.53) has access to your target remote machine (which runs mysql server)
ssh -f -L 295.13.12.53:3306:10.18.81.36:3306 user#295.13.12.53
Explained:
ssh -f -L your_ssh_mashine_ipaddress:your_ssh_mashine_local_port:target_ipaddress:target_port user#your_ip_address -N
your_ssh_mashine_ipaddress - it is not local ip address, it is ip address
that you ssh to, in this example 295.13.12.53
your_ssh_mashine_local_port -this is custom port not 22, in this example it is 3306.
target_ipaddress - ip of the machine that you trying to dump DB.
target_port - 3306 this is real port for MySQL server.
user#your_ip_address - this is ssh credentials for the ssh mashine that you connect
Once all this done then go back to your machine and do this:
mysqldump -h 295.13.12.53 -P 3306 -u username -p db_name > dumped_db.sql
Will ask for password, put your password and you are connected.
Hope this helps.
Try this, Its working:
mysql -h {hostname} -u{username} -p{password} -N -e "{query to execute}"
This one worked for me in mysql 8, replace hostname with your hostname and port_number with your port_number, you can also change your mysql_user if he is not root
mysql --host=host_name --port=port_number -u root -p
Further Information Here
You should put your password with 'p'
mysql -u root -u 1.1.1.1 -p'MyPass'
I was too getting the same error.
But found it useful by creating new mysql user on remote mysql server ans then connect. Run following command on remote server:
CREATE USER 'openvani'#'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'openvani'#'localhost WITH GRANT
OPTION;
CREATE USER 'openvani'#'%' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'openvani'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Now you can connect with remote mysql with following command.
mysql -u openvani -h 'any ip address'-p
Here is the full post:
http://openvani.com/blog/connect-remotely-mysql-server/
If you are on windows, try Visual Studio Code with MySQL plugins, an easy and integrated way to access MySQL data on a windows machine. And the database tables listed and can execute any custom queries.
If port is default, some version required data base name which you trying to connect.
mysql -u <<your username>> -h <<your host>> <<your db name >> -p
This will prompt password Then type your password. If port is not default 3306
Then:
mysql -u <<your username>> -h <<your host>> -P <<your port>> <<your db name >> -p

Remote Connections Mysql Ubuntu

For some reason, I've been unable to connect remotely to my MySQL server. I've tried everything and I'm still getting errors.
root#server1:/home/administrator# mysql -u monty -p -h www.ganganadores.cl
Enter password:
ERROR 1045 (28000): Access denied for user 'monty'#'server1.ganganadores.cl' (using password: YES)
Now, I've tried running
GRANT ALL ON *.* to monty#localhost IDENTIFIED BY 'XXXXX';
GRANT ALL ON *.* to monty#'%' IDENTIFIED BY 'XXXXXX';`
and still nothing!
What I'm doing wrong?
EDIT: my.cnf has commented out the bind ip .
To expose MySQL to anything other than localhost you will have to have the following line
For mysql version 5.6 and below
uncommented in /etc/mysql/my.cnf and assigned to your computers IP address and not loopback
For mysql version 5.7 and above
uncommented in /etc/mysql/mysql.conf.d/mysqld.cnf and assigned to your computers IP address and not loopback
#Replace xxx with your IP Address
bind-address = xxx.xxx.xxx.xxx
Or add a
bind-address = 0.0.0.0 if you don't want to specify the IP
Then stop and restart MySQL with the new my.cnf entry. Once running go to the terminal and enter the following command.
lsof -i -P | grep :3306
That should come back something like this with your actual IP in the xxx's
mysqld 1046 mysql 10u IPv4 5203 0t0 TCP xxx.xxx.xxx.xxx:3306 (LISTEN)
If the above statement returns correctly you will then be able to accept remote users. However for a remote user to connect with the correct priveleges you need to have that user created in both the localhost and '%' as in.
CREATE USER 'myuser'#'localhost' IDENTIFIED BY 'mypass';
CREATE USER 'myuser'#'%' IDENTIFIED BY 'mypass';
then,
GRANT ALL ON *.* TO 'myuser'#'localhost';
GRANT ALL ON *.* TO 'myuser'#'%';
and finally,
FLUSH PRIVILEGES;
EXIT;
If you don't have the same user created as above, when you logon locally you may inherit base localhost privileges and have access issues. If you want to restrict the access myuser has then you would need to read up on the GRANT statement syntax HERE If you get through all this and still have issues post some additional error output and the my.cnf appropriate lines.
NOTE: If lsof does not return or is not found you can install it HERE based on your Linux distribution. You do not need lsof to make things work, but it is extremely handy when things are not working as expected.
UPDATE: If even after adding/changing the bind-address in my.cnf did not work, then go and change it in the place it was originally declared:
/etc/mysql/mariadb.conf.d/50-server.cnf
Add few points on top of apesa's excellent post:
1) You can use command below to check the ip address mysql server is listening
netstat -nlt | grep 3306
sample result:
tcp 0 0 xxx.xxx.xxx.xxx:3306 0.0.0.0:* LISTEN
2) Use FLUSH PRIVILEGES to force grant tables to be loaded if for some reason the changes not take effective immediately
GRANT ALL ON *.* TO 'user'#'localhost' IDENTIFIED BY 'passwd' WITH GRANT OPTION;
GRANT ALL ON *.* TO 'user'#'%' IDENTIFIED BY 'passwd' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
user == the user u use to connect to mysql ex.root
passwd == the password u use to connect to mysql with
3) If netfilter firewall is enabled (sudo ufw enable) on mysql server machine, do the following to open port 3306 for remote access:
sudo ufw allow 3306
check status using
sudo ufw status
4) Once a remote connection is established, it can be verified in either client or server machine using commands
netstat -an | grep 3306
netstat -an | grep -i established
MySQL only listens to localhost, if we want to enable the remote access to it, then we need to made some changes in my.cnf file:
sudo nano /etc/mysql/my.cnf
We need to comment out the bind-address and skip-external-locking lines:
#bind-address = 127.0.0.1
# skip-external-locking
After making these changes, we need to restart the mysql service:
sudo service mysql restart
You are using ubuntu 12 (quite old one)
First, Open the /etc/mysql/mysql.conf.d/mysqld.cnf file (/etc/mysql/my.cnf in Ubuntu 14.04 and earlier versions
Under the [mysqld] Locate the Line,
bind-address = 127.0.0.1
And change it to,
bind-address = 0.0.0.0
or comment it
Then, Restart the Ubuntu MysQL Server
systemctl restart mysql.service
Now Ubuntu Server will allow remote access to the MySQL Server, But still you need to configure MySQL users to allow access from any host.
User must be 'username'#'%' with all the required grants
To make sure that, MySQL server listens on all interfaces, run the netstat command as follows.
netstat -tulnp | grep mysql
Hope this works !
If testing on Windows, don't forget to open port 3306.
In my case I was using MySql Server version: 8.0.22
I had to add
bind-address = 0.0.0.0
and change this line to be
mysqlx-bind-address = 0.0.0.0
in file at
/etc/mysql/mysql.conf.d
then restart MySQL by running
sudo service mysql restart
I was facing the same problem when I was trying to connect Mysql to a Remote Server. I had found out that I had to change the bind-address to the current private IP address of the DB server.
But when I was trying to add the bind-address =0.0.0.0 line in my.cnf file, it was not understanding the line when I tried to create a DB.
Upon searching, I found out the original place where bind-address was declared.
The actual declaration is in : /etc/mysql/mariadb.conf.d/50-server.cnf
Therefore I changed the bind-address directly there and then all seems working.

Accessing a mysql database from external host/ip? (ie: mysql workbench)

I have a mysql server running on x.x.x.x, and can access it internally no problem (of course). However when attempting to connect externally, ie using mysql workbench, or even from an external server, I get the error message "Host 'bla.bla.bla' is not allowed to connect to this MySQL server".
I have done:
GRANT ALL PRIVILEGES ON *.* TO mysql#x.x.x.x IDENTIFIED BY "somepass";
And I have opened port 3306 in iptables.
Is there another fundamental security issue I am missing?
You need to do
GRANT ALL PRIVILEGES ON *.* TO mysql#'bla.bla.bla' ...
The part after the # is the host from which the connection is coming, so you have allowed only connections coming from localhost. You need to allow access from each remote host necessary (or all hosts - ... mysql#'%' ... - if applicable).
To solve this you needed to perform the following commands:
mysql -u root -p
[enter in your password]
CREATE USER 'mysqluser'#'%' IDENTIFIED BY 'aC0MPL3XPa33W0RD';
GRANT ALL PRIVILEGES ON *.* TO 'mysqluser'#'%' WITH GRANT OPTION;
I had the exactly similar situation.my MYSQL is installed on a centOS.
The path to Nirvana is as below.
bind-address: DID NOT WORK
grant permission: DID NOT WORK
Iptables when turned off: DID work.
SOLUTION:I went fishing into the iptables and made following changes:
Access the iptables using the command : vim /etc/sysconfig/iptables
If you find the below statements COMMENT them out by adding a '#' at the beginning of the line.
-A INPUT -s 123.123.123.123/32 -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A OUTPUT -p tcp -m tcp --dport 3306 -j ACCEPT
Restart the iptables using the command: service iptables restart
Yep, that worked for me. Hope it is useful to someone.
When I Got my server,even I had the same problem accessing the mysql from MySQL client application,
Then I granted the Mysql permission, with following query.
it worked Great
**GRANT ALL PRIVILEGES ON db_base.* TO db_user #'%' IDENTIFIED BY 'db_passwd';**
db_base is the database Name
db_user is database User
db_passwd is the database password
Once you execute this flush it, by the following command
FLUSH PRIVILEGES;
Suppose if you are looking to give privileges to access certain tables in the Database you can use the following command
GRANT ALL PRIVILEGES ON db_base.HELLOWORLD TO db_user #'%' IDENTIFIED BY 'db_passwd';
Where HELLOWORLD is the table Name
Are you connecting as user mysql? You might try running the GRANT query as : GRANT ALL PRIVILEGES ON *.* TO mysql#x.x.x.x IDENTIFIED BY "somepass";
Have you verified that mysql workbench is trying the connect using the appropriate username? Have you flushed the privileges after running the grant command?
I dont know the ins and outs of security behind this bind-addressthing, just learning by installing a debian server on a virtual-box machine.
This guest has a virtual network card set up as a bridge, so the rest of the house can see it. Its IP is 192.168.1.4.
From another computer (192.168.1.3), connection failed with bind-address = 127.0.0.1.
Set up bind-address = 192.168.1.4 works fine.
(its own address, litterally)
It must be the interpretation of 127.0.0.1 inside a virtual config, not sure...
Comment out the line:
bind-address = localhost
#bind-address = localhost < this is what it should look like.
in your MySQL my.conf file. It is normally located in /etc/mysql/my.conf.