Remote MySQL connection

Discussion in 'Site & Server Administration' started by ByteCoder, Aug 14, 2010.

  1. #1
    I'm trying to connect from my site to a remote hosted MySQL server and I get this error :

    Can't connect to MySQL server on 'mysql3341.domain.com' (4)

    I made sure the DB host enables remote connections to it.
    Heres the script I'm using :

    <?php
    
    			//generate user and pass
    			$user = "myuser";
    			$salt = mt_rand(10300678912300008000045670002345, 99999999999999999999999999999999);
    			$password = mt_rand(1000, 999999);
    			
    			$salt = md5($salt);
    			$crypt = md5($password, $salt);
    			$password = $crypt . ':' . $salt;
    			
    			//db info
    			$db_hostname = 'mysql3341.domain.com'; //Server where MySQL is running. 
    			$dbName = 'dbName'; // the name of the db including all the users
    			$db_user = 'dbUsername'; //Username to connect with. 
    			$db_pass = 'dbPassword'; //Password to connect with. 
    			$usersTable = 'jos_users'; //the table where we have the users' info
    
    			//make connection
    			$mysql_con = mysql_connect($db_hostname,$db_user,$db_pass) or die("Unable to connect to the SQL server..." . mysql_error()); 
    			$mysql_db = mysql_select_db($dbName, $mysql_con);
    
    			//write log
    			$sqlq = "INSERT INTO " . $usersTable . " (name, username, email, password, usertype, block, sendemail, gid, registerdate, lastvisitdate, activation, param) values ('". mysql_real_escape_string($user) . "','" . mysql_real_escape_string($user) . "','". mysql_real_escape_string($user) . "','" . mysql_real_escape_string($password)  ."', 'Registered' , '0', '0', '18', '', '', '', '')";
    			mysql_query($sqlq);
    
    
    ?>
    Code (markup):
    It should insert a new user to joomla 1.5 site which is hosted on another host.
     
    ByteCoder, Aug 14, 2010 IP
  2. MirVB

    MirVB Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What port is MySQL running on? Did you make sure that port is not blocked by the Firewall? Did you grant access to your remote IP address?
     
    MirVB, Aug 14, 2010 IP
  3. ByteCoder

    ByteCoder Active Member

    Messages:
    239
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    65
    #3
    Port 3306 and the firewall doesnt block it. Also granted access to the remote IP, yes.
     
    ByteCoder, Aug 14, 2010 IP
  4. MirVB

    MirVB Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try the following:
    mysql -u myuser –h ipaddress –p

    Where myuser is the user, and ipaddress is the remote IP Address.
     
    MirVB, Aug 14, 2010 IP
  5. ByteCoder

    ByteCoder Active Member

    Messages:
    239
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    65
    #5
    Where should I use this?
     
    ByteCoder, Aug 14, 2010 IP
  6. MirVB

    MirVB Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If your local computer runs Windows, you can run this command from the command prompt:
    telnet ipaddress 3306
     
    Last edited: Aug 14, 2010
    MirVB, Aug 14, 2010 IP
  7. hans

    hans Well-Known Member

    Messages:
    2,923
    Likes Received:
    126
    Best Answers:
    1
    Trophy Points:
    173
    #7
    I am no mysql expert - but use my db on multiple servers and had to do something like - as user root in mysql-shell:

    on the mysql db server itself you need to grant something like: ( adapted to your precise needs/names/pwd/IP )

    xxx.yyy.zzz.aaa would be the new IP with access permission

    the WITH GRANT OPTION ; may NOT necessarily be needed - or lesser options sufficient, depending on what a new db-user ( software ) needs to do on that db.

    the key function of above is that as root user you have first to define/authorize a:

    - particular IP
    - precise username
    - dbname that user/IP is allowed to use

    to connect and define the new IP and all access data to be used by that new user.
    after than done comes the other part that you seem to do with the script u show.

    if you do ALL above automatically by a script, keep in mind that if ever at all your script is compromised, then anyone could connect to that db and possible modify/fake data

    if you have only a few new IPs to be added now and then
    I would recommend to do above mysql admin part manually as needed
    instead of authorizing new IP you also could authorize a new hostname ( web site ) to access a db
    if for any reason a web site moves away from you - out of your direct control - that site still would have access unless you remove permissions granted before.
    hence the IP method if preferable if you have your own unique IPs for your servers.
     
    hans, Aug 15, 2010 IP