How can I grant MySQL permissions from inside PHP? - php

what is the syntax to execute this statement in php page-
grant file on *.* to kentest#localhost identified by 'kentest1';

Check your [MySQL?] database vendors documentation - because that's a DBMS statement, not PHP. I'd start here: http://php.net/manual/en/function.mysql-connect.php
EDIT: To clarify, assuming you get your connection working, it would be as simple as wrapping your query in a call to mysql_query(). Example:
mysql_connect(...);
mysql_select_db(...);
mysql_query("grant file on *.* to kentest#localhost identified by 'kentest1';");

Check out the GRANT command! http://dev.mysql.com/doc/refman/5.1/en/grant.html
Check out the CREATE USER command! http://dev.mysql.com/doc/refman/5.1/en/account-management-sql.html
Ultimately check out the all section about account management in MySQL: http://dev.mysql.com/doc/refman/5.1/en/account-management-sql.html

firstly,you check the user of mysql_connect,is a root? this user must have the right to grant.

Here's some basic code that will work for many MySQL queries, including GRANT so long as the user is permissioned to do so:
// Credential variables, separated so we can reuse them later
$host = "localhost";
$user = "user";
$pass = "123456notsecure";
$db = "database_to_use";
// Set up the query we're going to run
$query_to_run = "QUERY TO RUN";
// Make the MySQL connection
$mysql_connection = mysql_connect($host, $user, $pass);
// Select the database to use
mysql_select_db($db) or die(mysql_error());
// Run the query
$result_of_query = mysql_query($query_to_run) or die('Running the query failed: ' . mysql_error());
// Close the MySQL connection
mysql_close($mysql_connection);

Related

Why can't access to especific DB using PDO, but can using mysqli without DB name? (usign devserver/phpmyadmin)?

I install easyphp/devserver (v17.0) in a windows 10 x64 machine.
Working with tables and testing to store remote data with simple php files (very new with DataBases).
I'm trying to setup an access to my DB for my project (preferably using PDO).
Setup all database through mysql commands:
CREATE database arduDB;
CREATE USER 'Atmega'#'localhost';
GRANT USAGE on *.* to 'Atmega'#'localhost';
GRANT ALL PRIVILEGES ON arduDB.* TO 'Atmega'#'localhost';
CREATE USER 'Atmega'#'%';
GRANT USAGE on *.* to 'Atmega'#'%';
GRANT ALL PRIVILEGES ON arduDB.* TO 'Atmega'#'%';
FLUSH PRIVILEGES;
All fine up here.
But can't access to my DB (arduDB) using PDO, but can using MYSQLI without using DB name 'arduDB'.
I deleted '' (Any) users from phpmyadmin, but still can't access using PDO.
Searched and reading all day about this issue, but can't find a reason why happen this.
As well I create another user with a password, but can't access using PDO to DB neither.
It seems that phpmyadmin can't relate the permissions of the DBs,
edited
This 'add.php', without using 'arduDB' name give access through MYSQLI:
<?php
$servername = "localhost";
$username = "Atmega";
// Create connection
$conn = new mysqli($servername, $username);
// Check connection
if ($conn->connect_error) {
die("Failed: " . $conn->connect_error);
}
echo "Connected!!!";
?>
shows "Connected" in web browser. But same error if I try to use arduDB argument.
But when I use PDO, indicating arduDB database, I can't access to my DB.
<?php
$servername = "localhost";
$username = "Atmega";
try {
$conn = new PDO("mysql:host=$servername;dbname=arduDB", $username);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected!!!";
}
catch(PDOException $e)
{
echo "Failed: " . $e->getMessage();
}
?>
SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'ardudb'
or
SQLSTATE[HY000] [1044] Access denied for user 'Atmega'#'localhost' to database 'ardudb'
Please, what can I need to configure or use in my code to grant access to a specific DB using PDO in my project?
Finlay I found the response.
Like André Verwijs says in his post, phpmyadmins use "unix_socket" to store user passwords in 'mysql' db. This is the motive that even if create or change password for any user, in 'User accounts' section of phpmyadmin always appears "NO" in PASSWORD COLUMN.
To solve this André Verwijs suggests do this in mysql console:
use mysql;
SELECT user, plugin FROM user;
UPDATE user SET plugin="mysql_native_password";
update user set authentication_string=password('USE-HERE-PASSWORD'), plugin='mysql_native_password' where user='Atmega';
FLUSH PRIVILEGES;
With this phpmyadmin will use plugin "mysql_native_password" for passwords in mysql DB. And now can access to my DB whit correct user and password (or without password).

Does a PHP file that attempts to connect to a database have to exist on the same server as the DB?

For example, when trying to connect to server xy.xy.xyz.xyz like below, does the script have to exist on that server to work?
<?php
$servername = "xy.xy.xyz.xyz";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
No it doesn't. The DB server has to be configured to allow remote connections though.
You can check this for further details for MySQL.
No, what's most likely happening is that your MySQL database isn't allowing users to sign in if they are trying to access it from an outside IP address.
Run the following command through MySQL
SELECT *
FROM information_schema.user_privileges
WHERE GRANTEE LIKE '%username%';
Change the username value to whatever the actual user name value is. If you're seeing a bunch of rows where the GRANTEE column is 'username'#'localhost' and nothing that looks like 'username'#'%', then your user is being limited to only localhost (same machine) access.
If you want to grant your user non-local access, you can use the following:
GRANT ALL PRIVILEGES
ON mydatabasename.*
TO 'username'#'%'
IDENTIFIED BY 'mypassword'
In the above, % means my user can connect from anywhere. This example grants all administrator rights to your user, but you can change that if you wish.

PHP code doesn't create the database

In my web application, it has a self installing functionality so, after it is copied to the server, you just need to run the install.php file which has the following PHP code.
define("DB_SERVER","localhost");
define("DB_USER","db_user");
define("DB_PASS","pass");
//create mysql connection
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS);
if(mysqli_connect_errno()){
die("Connection Error");
}else{
echo "MySQL Connection Successful!";
}
//create database if not exists
$sql = "CREATE DATABASE equiz";
$mysqlQuery = mysqli_query($connection, $sql);
if($mysqlQuery){
echo "Database Created Successfully";
}else{
die("Database Not Created!");
}
In the localhost, this works perfectly fine. But, when this is moved to the server, it gives me this error
Access denied for user 'db_user'#'localhost' to database 'equiz'
I had created the user db_user in advance along with another database and I'm sure he was granted with all the permissions. But I'm not sure if those permissions were for that particular database only.
Anyway any clue for this error ? I know little bit PHP but very new to real world web servers.
The key here is:
Access denied for user 'db_user'#'localhost' to database 'equiz'
If it's your localhost, then probably you gotten phpMyAdmin installed. You need to grant access rights to your db_user. How to do this, there's explicit manual:
http://docs.phpmyadmin.net/en/latest/privileges.html
You also can do it manually, if you don't have phpMyAdmin installed, reference the official MySQL manual, or tutorials
You need to set privilege for that user:
GRANT INSERT, SELECT, DELETE, UPDATE ON database.* TO 'db_user'#'localhost' IDENTIFIED BY 'password';
Then try running your code.
If the above does not work out try creating the user will all rights:
CREATE USER 'user_name'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'user_name'#'localhost';
FLUSH PRIVILEGES;
To create using PHP:
//Creation of user "user_name"
$mysqli->("CREATE USER 'user_name'#'%' IDENTIFIED BY 'pass_word';");
//Creation of database "new_db"
$mysqli->("CREATE DATABASE `new_db`;");
//Adding all privileges on our newly created database
$mysqli->("GRANT ALL PRIVILEGES on `new_db`.* TO 'user_name'#'%';");
Update:
Connect to database
// localhost <==> user_name <==> password <==> database
$mysqli = new mysqli("localhost", "user_id", "passwlrd", "db_name");
/* check connection */
if ($mysqli->connect_errno)
{
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}

Accessing my other database from my website 1 to my website 2

I have website 1 currently uploaded in the web and i have also develop a website 2 running on the localhost for now.. I want to access or get some value from the website 1 database to my website 2..is this possible using php query or javascripting? if not, what approach i need to take? thanks for the help
Yes you can, You have to just pass the parameters of the server details like this example.
<?php
//Connect To Database
$hostname='ukld.db.5510597.hostedresource.com';
$username='myusername';
$password='mypassword';
$dbname='testdb';
//your rest of code
?>
To allow connections from an external IP-address, you will need to do the following as well:
Grant access to a new database
If you want to add a new database called foo for user bar and remote IP 202.54.10.20 then you >need to type the following commands at mysql> prompt:
mysql> CREATE DATABASE foo;
mysql> GRANT ALL ON foo.* TO bar#'202.54.10.20' IDENTIFIED BY 'PASSWORD';
More information
Yes, It's simple.
<?php
$hostname = "remote_host_name";
$database = "remote_database_name";
$username = "database_username";
$password = "database_password";
$con = mysql_connect($hostname, $username, $password);
mysql_select_db($database, $con);
?>
Use this $con as the second parameter while running query.
e.g. mysql_query($query, $con);
Make sure that you have granted the access of server 1 in server 2 mysql database.

MySQL table doesn't exist when it does exist

I have this query:
mysql_select_db('scanner');
$query = "SELECT * FROM scanner.proxy ORDER BY RAND() LIMIT 20";
$result = mysql_query($query) or die(mysql_error());
it tells me:'scanner.proxy ' doesnt exist.. even though I do have it the table with the database. It is weird, cause my other queries work..but this one doesnt.
UPADTE:
Event when I put this:
$user='root';
$user='root'
$password='<removed_password>';
$dbname = 'scanner';
$host = 'localhost';
$link = mysql_connect($host, $user, $password) or die (mysql_error());
mysql_select_db($dbname, $link) or die (mysql_error());
it gives me this...
Unknown database 'scanner'
But I can see the scanner database in the phpmyadmin
Even when I type in phpmyadmin the sql statement
SHOW TABLES FROM 'scanner'
it says it cant find scanner
We solved this problem which occurred when connecting to multiple remote MySQL databases within the same script by adding a re-connect method before every mysql_query statement. The re-connect method invoked both mysql_connect and mysql_select_db functions. We had previously tried setting the new link parameter to true in the connect statement(s) but We still got database errors and all kinds of funny behavior.
make sure the user you use to connect has enough rights to query that table. Perhaps that table was created by another user.
EDIT: Perhaps you need to grant access to scanner from a remote location. Running this sholud help on that.
GRANT ALL ON scanner.* TO your_user#'IP_ofServer_where_PHP_runs' IDENTIFIED BY 'PASSWORD';
You have set the $dbname = 'aws_backlinks', but you are trying to connect to a different database within your query. Change your connection string to include this db instead or just connect to the server and set your database at the time of the query.
$db_host = 'localhost:Port';
Specify port for localhost.
I was facing the same problem and specifying the port solved the problem.
You're missing the argument that specifies the connection (result of mysql_connect()) in your mysql_select_db() call:
$db_host = 'localhost';
$db_username = 'root';
$db_password = 'whatever';
$db_name = 'scanner';
$link = mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_name, $link);

Categories