Granting remote access to MySQL database - mysql

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.

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.

Connect to remote mysql server from ubuntu fails

I'm using AWS server (ubuntu) for backup my remote mysql db, using mysqldump command.
Since I changed the db password, I cannot connect anymore remotely from the machine using /etc/mysql/my.cnf configuration file.
When I'm using the command
mysql -u root -h 1.1.1.1 -p 123456
It's connects successfully, but when I'm trying to connect by using the mysql configuration file /my.cnf by typing just
mysql
I gets the error message :
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I restarted the db server (I don't need mysql service running on aws because I connected remotely)
I don't want to connect through any socket
my.cnf file content:
[client]
port = 3306
host = 1.1.1.1
user = root
password = 123456
[mysqld]
user = mysql
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
bind-address = 1.1.1.1
I don't know what have changed, except from the password, I didn't change anything neither in the db itself and in the config file, so I have no idea why it stopped working.
Add in the my.cnf the line protocol=tcp
[client]
port = 3306
host = 1.1.1.1
user = root
password = 12345
protocol = tcp
That force MySQL to use TCP and not the socket connection

MySQL local multiple instances not working

So I have MySQL install via homebrew and have edited my.cnf to look like below. I've added mysql and mysql2 directories to /usr/local/var/ which hold the data for the databases and installed a second mysql server configured to use /usr/local/var/mysql2
[mysqld_multi]
mysqld = /usr/local/Cellar/mysql/5.6.24/bin/mysqld_safe
mysqladmin = /usr/local/Cellar/mysql/5.6.24/bin/mysqladmin
log = /var/log/mysqld_multi.log
user = root
password = password
[mysqld1]
socket = /tmp/mysql.sock1
port = 3306
pid-file = /usr/local/var/mysql/mysqld1.pid
datadir = /usr/local/var/mysql/
language = /usr/local/Cellar/mysql/5.6.24/share/mysql/english
user = root
[mysqld2]
socket = /tmp/mysql.sock2
port = 3308
pid-file = /usr/local/var/mysql2/mysqld2.pid
datadir = /usr/local/var/mysql2/
language = /usr/local/Cellar/mysql/5.6.24/share/mysql/english
user = root
The problem is that when I start the servers like so:
mysqld_multi start --user=root --password=password
I see that both of them are instantiated when i run
mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
When I individually start/stop 1/2 both start/stop and in /usr/local/var/mysql the .pid file is generated using my local username not mysqld1.pid and in /usr/local/var/mysql2 there is no .pid file created. I can connect to the database using:
mysql -h 127.0.0.1 -P 3306 -u root -ppassword
However I cannot connect on port 3308
running nmap tells me:
nmap -p 3308 127.0.0.1
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00011s latency).
PORT STATE SERVICE
3308/tcp closed unknown
Not sure what is going on here any help would be greatly appraciated.
My_print_defaults have to be executed as follows to produce any output:
my_print_defaults --defaults-file=/usr/local/Cellar/mysql/5.6.24/my.cnf client mysqld1
--socket=/tmp/mysql.sock1
--port=3306
--pid-file=/usr/local/var/mysql/mysqld1.pid
--datadir=/usr/local/var/mysql/
--language=/usr/local/Cellar/mysql/5.6.24/share/mysql/english
--user=root
my_print_defaults --defaults-file=/usr/local/Cellar/mysql/5.6.24/my.cnf client mysqld2
--socket=/tmp/mysql.sock2
--port=3308
--pid-file=/usr/local/var/mysql2/mysqld2.pid
--datadir=/usr/local/var/mysql2/
--language=/usr/local/Cellar/mysql/5.6.24/share/mysql/english
--user=root

Can not setup external access to MySQL server

I'm trying to allow external access to MySQL server. First I changed my.cnf to this (part of my.cnf):
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[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 = 0.0.0.0
In /etc/hosts.allow I added:
mysqld: all
Also I added user 'root'#'%', but what I have is that:
I'm trying to connect from server to server using external host name:
root#******:~# mysql -h******.com -u root -p
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
But using 'localhost' all is fine:
root#******:~# mysql -hlocalhost -u root -p
MySQL Server version: 5.5.43
Ubuntu 14.10 (GNU/Linux 3.16.0-23-generic x86_64)
You should run the commands after creating user-
GRANT ALL ON dbname.* TO 'root'#'%' IDENTIFIED BY 'root';
FLUSH HOSTS;
FLUSH PRIVILEGES;
if you have already run this code then there could be fire wall problem-

Connecting to mysql doesn't work

I am trying to setup iRedMail but during the installation process I get this error:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
and by default iRedMail tries to connect to MySql with TCP and not with a system socket.
To narrow down the problem, the following works:
mysql -h localhost -P 3306 -u root -p
but this does not
mysql -h 127.0.0.1 -P 3306 -u root -p
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
I am using mysql Ver 14.14 on Ubuntu 14.04
Here are the relevant configuration lines of /etc/mysql/my.cnf:
skip-networking=0
bind-address = 127.0.0.1
Also, mysql> SHOW VARIABLES; shows that
skip_networking | ON
Here are the relevant lines of px aux | grep mysql:
mysql /usr/sbin/mysqld --basedir=/usr --datadir=/mnt/storage/data/mysql/ --plugin- dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
= EDIT =
The main question here is how to turn off skip_networking
Check your Firewall.
Or use Socket connection.
start mysql service then try
sudo service mysqld start
for installation of mysql in ubuntu
sudo apt-get install mysql-server mysql-client
It looks like you are using skip-networking and bind-address at the same time. You should never do this.
skip-networking disables TCP/IP - so just remove it.
bind-address means you are using TCP/IP.