Yii2 mysql连接配置调试

1 basic/config/db.php添加

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=172.16.8.12;dbname=student',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',

    // Schema cache options (for production environment)
    //'enableSchemaCache' => true,
    //'schemaCacheDuration' => 60,
    //'schemaCache' => 'cache',
];

2 basic/test/index test controller添加

    $connection = Yii::$app->db;
    $sql = "select * from student limit 1";
    $student = $connection->createCommand($sql)->queryAll();
    var_dump($student);

3 访问测试

http://{url}/test/index
posted @ 2021-06-22 15:40  胡勇健  阅读(389)  评论(0)    收藏  举报