stuckinmobile
Posts: 7
Joined: Sun Jan 06, 2013 10:22 pm

Accessing mysql database

Wed Jan 22, 2014 6:33 am

I'm sure my problem is permissions related, yet I don't know what my next step should be.

After accessing mysql via: mysql -u root -p I was able to create a database.

When I try and access it via a php script, mysqli_connect_error() tells me: Access denied for user 'pi'@'localhost' (using password: YES)No database selected

In my php script there is: $con=mysqli_connect("localhost","pi","*******","db_name");

mysql_select_db("db_name");

I've double checked, all that content is correct.

While logged in to the database as root (via mysql -u root -p) I am able to access the database, create a table, insert content into it and read it back via "SELECT * FROM created_table".

FWIW, when I access mysql with a simple "mysql" at the command line, I do get the "mysql>" prompt. But SHOW DATABASES; does not list the "db_name" I created with root and it does not allow me to create a database.

Am I wrong to be able to expect to access a database created while root from a php script on a web page? How should I be approaching this??

Thanks from a Noobie.

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Accessing mysql database

Wed Jan 22, 2014 6:57 am

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
rpdom
Posts: 17172
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Accessing mysql database

Wed Jan 22, 2014 6:57 am

You need to add permissions for the user "pi" to access the database "db_name" at "localhost". It depends on what permissions you want the "pi" user to have.

You then add these permissions to the main mysql database as root, using the "GRANT" command.

The least secure, lazy, option, is to do something like this

Code: Select all

pi@raspberrypi:~$ sudo mysql
(blah)
mysql> use mysql
(blah)
Database changed
mysql> GRANT ALL ON db_name.* TO `pi`@`localhost`;
OK
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.03 sec)

mysql> quit
Please check out the syntax of the GRANT command before following this advice. I haven't checked it, just typed out as I remember it!

stuckinmobile
Posts: 7
Joined: Sun Jan 06, 2013 10:22 pm

Re: Accessing mysql database

Wed Jan 22, 2014 2:32 pm

Thank you for the responses, will be trying them out tonight. (Day job is calling!)

Return to “Troubleshooting”