我知道有很多类似的问题,但都没有帮助我解决这个问题。
我正试图将我在EC2实例上的laravel项目连接到我的RDS实例。
我可以成功连接到RDS实例,并在使用时创建表。
mysql -h ....rds.amazonaws.com -u cmadmin -p
然而,我目前得到的错误,当我执行 php artisan migrate
:
In Connection.php line 664:
SQLSTATE[HY000] [1045] Access denied for user 'cmadmin'@'ec2_privateIP' (using password: YES) (SQL: select * from information_schema.tables where table_schema = dbname_cm and table_name = migrations)
In Connector.php line 67:
SQLSTATE[HY000] [1045] Access denied for user 'cmadmin'@'ec2_privateIP' (using password: YES)
这里是.env文件的节选。
APP_NAME=Laravel
APP_ENV=local
APP_KEY=generatedkey
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=....rds.amazonaws.com
DB_PORT=3306
DB_DATABASE=dbname_cm
DB_USERNAME=cmadmin
DB_PASSWORD=rfL8zvXnMEzit02
这里是configdatabase.php文件的节选。
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', ''),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_0900_ai_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
'option' => [
PDO::MYSQL_ATTR_SSL_CA => base_path('/var/mysql-cert/dbname_cm-db.pem')
],
],
我甚至在下面添加了以下内容。app/Providers/AppServiceProvider.php
public function boot()
{
Schema::defaultStringLength(191);
}
我已经运行了:php artisan cache:clearphp artisan config:clearphp artisan config:cache。
PHP 7.2.24-0ubuntu0.18.04.4Laravel Framework 5.5.49AWS RDS: MySQL 8.0.17。
mysql> SELECT USER(),CURRENT_USER();
+------------------------+----------------+
| USER() | CURRENT_USER() |
+------------------------+----------------+
| cmadmin@ec2_privateIP | cmadmin@% |
+------------------------+----------------+
mysql> show grants;
| Grants for cmadmin@% |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, PROCESS, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER ON *.* TO `cmadmin`@`%` WITH GRANT OPTION |
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| dbname_cm |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
'driver' => 'mysql',
'host' => env('DB_HOST', ''),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_0900_ai_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
'option' => [
PDO::MYSQL_ATTR_SSL_CA => base_path('/var/mysql-cert/dbname_cm-db.pem')
],
],```
have you already try file this part `'username' => env('DB_USERNAME', ''),` with `'username' => env('DB_USERNAME', 'cmadmin'),`
and of course the DB_PASSWORD part with your predefined password
我在database.php文件中加入以下代码解决了这个问题.
'mysql' = [
'sslmode' => 'require',
'options' => array(
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,
PDO::MYSQL_ATTR_SSL_KEY => '/certs/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/certs/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA => '/certs/ca.pem',
)
],