2015-07-01 15 views
10

Sono nuovo di PHP e CakePHP. Trovare problemi durante il cablaggio del mio database usando CakePhp.SQLSTATE [HY000] [1045] Accesso negato per utente 'nome utente' @ 'localhost' utilizzando CakePhp

Di seguito è riportata la configurazione dell'app. Sono in Bitnami WAMP pila 5.4.40-0 Sto usando CakePHP 3.0.4 per creare un web MVC applicazione

ingresso per origini dati nel mio file app.php

/** 
* Connection information used by the ORM to connect 
* to your application's datastores. 
* Drivers include Mysql Postgres Sqlite Sqlserver 
* See vendor\cakephp\cakephp\src\Database\Driver for complete list 
*/ 
'Datasources' => [ 
    'default' => [ 
     'className' => 'Cake\Database\Connection', 
     'driver' => 'Cake\Database\Driver\Mysql', 
     'persistent' => false, 
     'host' => 'localhost', 
     /** 
     * CakePHP will use the default DB port based on the driver selected 
     * MySQL on MAMP uses port 8889, MAMP users will want to uncomment 
     * the following line and set the port accordingly 
     */ 
     //'port' => 'nonstandard_port_number', 
     'username' => 'test2', 
     'password' => 'computer', 
     'database' => 'jobs', 
     'encoding' => 'utf8', 
     'timezone' => 'UTC', 
     'cacheMetadata' => true, 

     /** 
     * Set identifier quoting to true if you are using reserved words or 
     * special characters in your table or column names. Enabling this 
     * setting will result in queries built using the Query Builder having 
     * identifiers quoted when creating SQL. It should be noted that this 
     * decreases performance because each query needs to be traversed and 
     * manipulated before being executed. 
     */ 
     'quoteIdentifiers' => false, 

     /** 
     * During development, if using MySQL < 5.6, uncommenting the 
     * following line could boost the speed at which schema metadata is 
     * fetched from the database. It can also be set directly with the 
     * mysql configuration directive 'innodb_stats_on_metadata = 0' 
     * which is the recommended value in production environments 
     */ 
     //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'], 
    ], 

Ho già creato una tabella di database chiamata job in base alle convenzioni di CakePhp. L'utente test2 ha i privilegi globali come l'amministratore di root.

Ma quando eseguo il comando bake all. Sto ottenendo il seguente errore

2015-07-01 06:24:56 Error: [PDOException] SQLSTATE[HY000] [1045] Access denied for user 'test2'@'localhost' (using password: YES) 
Stack Trace: 
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Driver\PDODriverTrait.php(48): PDO->__construct('mysql:host=127....', 'test2', 'computer', Array) 
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Driver\Mysql.php(89): Cake\Database\Driver\Mysql->_connect('mysql:host=127....', Array) 
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Schema\BaseSchema.php(46): Cake\Database\Driver\Mysql->connect() 

PROBLEMA RISOLTO (UPDATE)

ho seguito Ankit e le indicazioni di Spencer.

Ho avuto un paio di problemi.

  1. L'host del mio utente non era localhost, era un jolly%. Modificato questo, quindi mysql ha iniziato a rifiutare le connessioni.

  2. Ho disabilitato il mio firewall e ho scoperto che la porta era diversa da 3306. Quindi ho cambiato la voce in app.php. 'Root' & password: Ora la mia domanda è al forno :)

+0

'Accesso negato per l'utente 'test2' @ 'localhost' (usando la password: YES)' che è piuttosto ambigua. _Ha_ l'utente test2 ha accesso? Cioè funziona: 'mysql -utest2 -pomputer jobs'? – AD7six

+0

strano, cambiare '%' in 'localhost' ha funzionato davvero per me ... Potrebbe anche creare un altro utente - eliminare quello con l'host'% ' –

+0

Riavviare MySQL dopo tali modifiche è la ciliegina sulla torta. – dakab

risposta

16

Questo messaggio di errore di solito significa che o la password in uso non corrisponde a quello che pensa MySQL la password dovrebbe essere per l'utente che stai connettendo come, o un utente MySQL corrispondente non esiste (non è stato creato).

In MySQL, un utente è identificato sia da un nome utente (test2) che da un host (localhost).

Il messaggio di errore che stai ottenendo identifica i valori user (test2) e host (localhost) ...

'test2'@'localhost' 

Verificare se esiste l'utente, utilizzando questa query da un client è possibile connettersi da:

SELECT user, host FROM mysql.user 

Siete alla ricerca di una riga che ha test2 per l'utente, e localhost per Host .

user  host  
------- ----------- 
test2  127.0.0.1 
test2  ::1   
test2  localhost 

Se tale riga non esiste, allora l'host può essere impostata sul valore jolly di %, da abbinare qualsiasi altro host che non è una partita.

Se la riga esiste, la password potrebbe non corrispondere. È possibile modificare la password (se si è connessi come utente con privilegi sufficienti, ad esempio root

SET PASSWORD FOR 'test2'@'localhost' = PASSWORD('mysecretcleartextpassword') 

Verificare inoltre che l'utente disponga di privilegi sugli oggetti nel database.

GRANT SELECT ON jobs.* TO 'test2'@'localhost' 
+0

grazie mille Spencer ... anzi l'host del mio utente era un valore jolly, cambiato che ... e anche la porta era diversa dalla 3306..ora la mia cottura del database è riuscita :) – saurav

5

controllare le seguenti cose

  • Assicurarsi di avere MySQL server che esegue
  • Controllare il collegamento con le credenziali di default nome utente cioe '' [ Password vuota]
  • Prova login phpmyadmin con le stesse credenziali
  • Prova a mettere 127.0.0.1 invece localhost o il tuo IP lan farebbe anche.
  • Assicurarsi si esegue MySQL su 3306 e se si è configurato assicurarsi di affermare che mentre si effettua una connessione
+1

Sostituisco "localhost" in "127.0.0.1" e risolvo il problema. –

2

Se si utilizza MAMP, potrebbe essere necessario impostare il socket: unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock

0

Voglio aggiungere alle risposte postate sopra che nessuna delle soluzioni proposte qui ha funzionato per me. My WAMP, sta lavorando sulla porta 3308 ins tead di 3306 che è quello che è installato di default. Ho scoperto che quando si lavora in un ambiente locale, se si utilizza mysqladmin nel computer (per l'ambiente di test) e se si lavora con una porta diversa dalla 3306, è necessario definire la variabile DB_SERVER con il valore localhost: NumberOfThePort, quindi apparirà come segue: define ("DB_SERVER", "localhost: 3308"). È possibile ottenere questo valore facendo clic con il pulsante destro del mouse sull'icona WAMP nella barra delle applicazioni (nella sezione icone nascoste) e selezionare Strumenti. Verrà visualizzata la sezione: "Porta utilizzata da MySQL: NumberOfThePort"

Ciò risolverà la connessione al database.

Questo è stato l'errore che ho ottenuto: errore: SQLSTATE [HY1045] Accesso negato per l'utente 'username' @ 'localhost' on line X.

Spero che questo ti aiuta.

:)

Problemi correlati