Cannot connect to database

Discussion in 'MySQL' started by Lotos1, Nov 28, 2009.

  1. #1
    I am getting this Connect Error: 2002
    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
    I'm trying to make a connection to the DB using this: $db = new mysqli('localhost', 'username', 'password', 'books');
    I can log into my DB using the MySQL command line with the root password but for some reason cannot access the database from PHP.
    Even when I try to access it with the root like this: $db = new mysqli('localhost', 'root', 'rootpassword', 'books'); it still gives the same error. The database 'books' does exist as I can display it from the command line client.
    Do I have to change something in the php.ini file or some other configuration file because I left the default settings? I'm trying to run this on a Windows Vista machine using IIS 7. Thank you in advance.
     
    Lotos1, Nov 28, 2009 IP
  2. sahabcse

    sahabcse Well-Known Member

    Messages:
    272
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Is your Host has permission to access the DB
     
    sahabcse, Nov 28, 2009 IP
  3. Lotos1

    Lotos1 Peon

    Messages:
    454
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm doing this on my local Windows Vista machine. I am not using a Web host. I installed PHP and MySQL on my own machine and I got back root password. Do I need to configure something so that PHP can communicate with MySQL properly like assign some special permissions?
     
    Lotos1, Nov 28, 2009 IP
  4. sahabcse

    sahabcse Well-Known Member

    Messages:
    272
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    140
    #4
    you need to set the grand permission of the user to acces the particular databases.
     
    sahabcse, Nov 28, 2009 IP
  5. Lotos1

    Lotos1 Peon

    Messages:
    454
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    But I'm trying to access the database as a root user. Doesn't the root user have access to all databases? When I log in as a root user using the MySQL command line client, I can open all the tables from the database. What permissions are you talking about? When I try to run the php page script, it waits for like 15 seconds and then either shows that error or gives me no output when it should. It seems to be timing out or cannot connect using mysqli to the MySQL database as a root user.
     
    Last edited: Nov 28, 2009
    Lotos1, Nov 28, 2009 IP
  6. sahabcse

    sahabcse Well-Known Member

    Messages:
    272
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    140
    #6
    check the mysql configuration, is there any connection time out setup is made and check your php script also
     
    sahabcse, Nov 28, 2009 IP
  7. Lotos1

    Lotos1 Peon

    Messages:
    454
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I checked my.ini file and there is nothing set for anything related to timeout. I also checked php.ini and I saw this max_execution_time =300, max_input_time = 60 and mysql.connect_timeout = 60. Are these values normal and what can be the problem? I don't understand why I can log into MySQL using the command line client and cannot connect to it using PHP and the same username and password, ie. root and rootpassword.

    I also checked and MySQL server and port 3306 that I use for MySQL server are in the allowed list for my firewall so the firewall should not be a problem.
     
    Last edited: Nov 28, 2009
    Lotos1, Nov 28, 2009 IP
  8. bhagwant.banger

    bhagwant.banger Active Member

    Messages:
    99
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    60
    #8
    Try connecting with mysql_connect first

    if you are able to connect with it

    then post here again
     
    bhagwant.banger, Nov 28, 2009 IP
  9. Lotos1

    Lotos1 Peon

    Messages:
    454
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I tried that too. Calling mysql_connect as a procedure and not class. It still does not work. I'm baffled. I followed all of the instructions when installing PHP on Windows and it does not seem to work with MySQL server for some reason. MySQL works on its own through the command line client. Now, I'm not sure if something is wrong with the settings of PHP, or MySQL, or IIS? PHP files do run on IIS without any problems.
     
    Lotos1, Nov 28, 2009 IP
  10. Lotos1

    Lotos1 Peon

    Messages:
    454
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Just wanted to post an update. I tried using mysql_connect('localhost','root','rootpassword');
    @ mysql_select_db('books') or die("Unable to select database"); instead of $db = new mysqli('localhost', 'root', 'rootpassword', 'books');
    and still nothing was fixed. Same thing happens again.
     
    Last edited: Nov 29, 2009
    Lotos1, Nov 29, 2009 IP
  11. iama_gamer

    iama_gamer Active Member

    Messages:
    404
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #11
    Please check the permissions on /var/lib/mysql . In linux the required permission is 755 so give the corresponding permissions
     
    iama_gamer, Dec 3, 2009 IP