php 用户“username '@”%“的PDO访问被拒绝

bbuxkriu  于 2023-01-08  发布在  PHP
关注(0)|答案(3)|浏览(210)

我需要使用PHP页面连接到远程MySQL服务器;
连接本身就像一个符咒,但是当我试图创建数据库时,因为它还不存在,我得到了这个错误:
异常"PDOException",并显示消息"SQLSTATE [42000]:语法错误或访问冲突:1044拒绝用户'[myusername]'@' %'访问[myurl ]/php/db_manager. php中的数据库'[mydbname]'
如您所见,我对" %"的访问被拒绝。
现在:什么是" %"?
此外:

主文件

private function createDB() {
if($this->cm->update("CREATE DATABASE IF NOT EXISTS " . $this->dbName . " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;", array())) { // Error here
  $this->cm->update("USE " . $this->dbName . ";", array());
  return true;
}
return false;
}

$this-〉cm是正确初始化的PDO Package 器的示例

PDO Package 文件

public function update($query, $values) 
try{
        $sql = $this->db->prepare($query);
        $sql->execute($values);
        return true;
    } catch(PDOException $e) {
        $this->l->error($e); // <- Error here
        return false;
    }
}

$this-〉db是一个正确示例化的、完全连接的PDO对象;
这些是用来连接

$this->db = new PDO($connection, $this->db_user, $this->db_password, array(PDO::ATTR_PERSISTENT => true));
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

我对Mysql服务器有完全访问权限

pieyvz9o

pieyvz9o1#

拒绝用户“[myusername]”@“%”访问数据库“[mydbname]”
MySQL权限是细粒度的:并非所有用户都具有对服务器上所有数据库的完全访问权限。
您需要以管理员和grant the appropriate permissions的身份登录。例如,要授予完全访问权限

GRANT ALL
ON mydbname.*
TO 'myusername'@'%'
WITH GRANT OPTION;
FLUSH PRIVILEGES;

...或者您可以根据自己的喜好进行选择:

GRANT SELECT, ALTER, CREATE, DELETE, DROP, INDEX, INSERT, REFERENCES, TRIGGER, UPDATE
ON mydbname.*
TO 'myusername'@'%';
FLUSH PRIVILEGES;

请查看Privileges Supported by MySQL以获取完整列表。
%是一个通配符,在帐户名和密码中详细解释,表示“从任何主机连接”。

yb3bgrhw

yb3bgrhw2#

我在xampp上也遇到过类似的问题。我把我的密码改为自动生成密码(通过生成密码按钮),然后复制粘贴到配置文件中。工作成功!

代码:

// config information
require_once "config.php";

// make connection string
$connection_string = "mysql:host=" . DB_HOST . ":" . DB_PORT . ";dbname=" . DB_NAME;
$connection = null;

// show connection string
echo $connection_string."</br>";

// try to connect to db or catch exceptions
try{
    $connection = new PDO( $connection_string, DB_USER, DB_PASS );
}catch (PDOException $exc){
    echo '<pre>';
    print_r($exc);
    echo '</pre>';
}

echo '</br>';

// connection status
if ( $connection ) {
    echo "Connected Successfully!";
} else {
    echo "Connection Failed!";
}

结果:

mysql:host=127.0.0.1:3306;dbname=php_pdo_db

Connected Successfully!
xqkwcwgp

xqkwcwgp3#

在PDO查询中,在主机之后添加端口(在我的例子中,将端口更改为8111),它应该可以工作。

    • 代码:**
//write in database
try {
    $dbh = new PDO("mysql:host=$db_host:8111;dbname=$db_name", $db_user, $db_password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->exec($database);
}
catch(PDOException $e) {
    if ($e->getCode() == 2002) {
        header('location: step3.php?_error=Unable to Connect Database, Please make sure Host info is correct and try again !');
        exit;
    }
    elseif ($e->getCode() == 1045) {
        header('location: step3.php?_error=Unable to Connect Database, Please make sure database username and password is correct and try again !');
        exit;
    }
    elseif ($e->getCode() == 1049) {
        header('location: step3.php?_error=Unable to Connect Database, Please make sure database name is correct and try again !');
        exit;
    }
}

相关问题