PDO访问被拒绝用户'用户名'@'%'

时间:2016-08-29 07:16:08

标签: php mysql pdo

我需要使用php页面连接到远程mysql服务器;
连接本身作为魅力,但是当我尝试创建数据库的那一刻,因为它还不存在,我得到这个错误:

  

异常'PDOException',消息'SQLSTATE [42000]:语法错误或访问冲突:1044访问被拒绝用户'[myusername]'@'%'到[myurl] / php /中的数据库'[mydbname]'' db_manager.php:76

如您所见,我有权拒绝“%” 现在:什么是“%”?

此外:

主文件

    case 3:// from the main. arrTask3 is initialized By get2Darray. 
                printf("enter the size of the array\n");
                scanf("%d",&arrTask3Size);
                arrTask3Size=getarrsize(arrTask3Size);
                Get2Darr((int**)arrTask3, arrTask3Size, 0, 0);
                num=*((int*)arrTask3);
                start=(int**)arrTask3;
                finish=(int**)(arrTask3)+arrTask3Size*arrTask3Size;

               Task3He(num, arrTask3Size, arrTask3, finish, start, 0, 's');

void Get2Darr(int** arr2D,int size,int row, int col){
    int num;
    if(row<size){
        if(col<size){
            printf("enter the element at cell [%d][%d]\n",row,col);
            scanf("%d",&num);
            *((int*)arr2D+row*size+col)=num;
            Get2Darr(arr2D, size, row, col+1);
        }
        else
            Get2Darr(arr2D, size, row+1, 0);
    }
}

    int Task3He(int num,int N ,int **table,int **Finish,int **Start,int count,char prvstep){
        int tempup,tempdo,templ,tempr;
        if((int)(table-Finish)==0&&num==*((int*)table))
            return count;
        if((int)(table-Finish)==0)
            return 0;
        if((int)table<(int)Start||(int)table>(int)Finish)
            return 0;
        if(*((int*)table)!=num)
            return 0;
        if(prvstep=='u'){
        tempup=Task3He(num,N, (int**)table-N, Finish, Start, count+1,'u');
        if (tempup!=0) return 1;
        templ=Task3He(num,N, (int**)table-1, Finish, Start, count+1,'l');
        if (templ!=0) return 1;
        tempr=Task3He(num,N, (int**)table+1, Finish, Start, count+1,'r');
        if (tempr!=0) return 1;
        }
        if(prvstep=='d'){
            tempdo=Task3He(num,N, (int**)table+N, Finish, Start, count+1,'d');
            if (tempdo!=0) return 1;
            templ=Task3He(num,N, (int**)table-1, Finish, Start, count+1,'l');
            if (templ!=0) return 1;
            tempr=Task3He(num,N, (int**)table+1, Finish, Start, count+1,'r');
            if (tempr!=0) return 1;
        }
        if(prvstep=='l'){
            tempup=Task3He(num,N, (int**)table-N, Finish, Start, count+1,'u');
            if (tempup!=0) return 1;
            tempdo=Task3He(num,N, (int**)table+N, Finish, Start, count+1,'d');
            if (tempdo!=0) return 1;
            templ=Task3He(num,N, (int**)table-1, Finish, Start, count+1,'l');
            if (templ!=0) return 1;
        }
        if(prvstep=='r'){
            tempup=Task3He(num,N, (int**)table-N, Finish, Start, count+1,'u');
            if (tempup!=0) return 1;
            tempdo=Task3He(num,N, (int**)table+N, Finish, Start, count+1,'d');
            if (tempdo!=0) return 1;
            tempr=Task3He(num,N, (int**)table+1, Finish, Start, count+1,'r');
            if (tempr!=0) return 1;
        }
        if(prvstep=='s'){
        tempup=Task3He(num,N, (int**)table-N, Finish, Start, count+1,'u');
        if (tempup!=0) return 1;
        tempdo=Task3He(num,N, (int**)table+N, Finish, Start, count+1,'d');
        if (tempdo!=0) return 1;
        templ=Task3He(num,N, (int**)table-1, Finish, Start, count+1,'l');
        if (templ!=0) return 1;
        tempr=Task3He(num,N, (int**)table+1, Finish, Start, count+1,'r');
        if (tempr!=0) return 1;
        }
        return 0;
        }

$ this-&gt; cm是正确初始化的PDO包装器的实例

PDO包装文件

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-&gt; db是一个正确实例化的,完全连接的PDO对象;
这些是用于连接的行

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;
    }
}

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

2 个答案:

答案 0 :(得分:3)

  

拒绝访问用户&#39; [myusername]&#39; @&#39;%&#39; 到数据库&#39; [mydbname]&#39;

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以获取完整列表。

%是在Account Names and Passwords详细解释的通配符,表示来自任何主机的连接&#34;。

答案 1 :(得分:0)

我在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!

相关问题