无法在 Vapor 项目中连接我的 MySQL 数据库

Can`t connect my MySQL database in Vapor project

我是 Vapor 的新手,我想尝试连接我的 MySQL 数据库。我在官方文档中找到了如何执行此操作,但是当我尝试发送查询时它会抛出错误:

No services are available for 'DatabaseConnectionPoolCache'. (Container.swift:112)

我只是从文档中复制粘贴代码,但它不起作用。有人可以帮我找出原因吗?

我在 Mojave 上有 MySQL@5.7。实际上简单的请求效果很好,例如:

router.get("hey") { req in
    return "Stas, hey"
}

configure.swift 中的代码:

import FluentSQLite
import MySQL
import Vapor

/// Called before your application initializes.
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
    /// Register providers first
    try services.register(FluentSQLiteProvider())
    try services.register(MySQLProvider())

    /// Register routes to the router
    let router = EngineRouter.default()
    try routes(router)
    services.register(router, as: Router.self)

    /// Register middleware
    var middlewares = MiddlewareConfig() // Create _empty_ middleware config
    /// middlewares.use(FileMiddleware.self) // Serves files from `Public/` directory
    middlewares.use(ErrorMiddleware.self) // Catches errors and converts to HTTP response
    services.register(middlewares)

    // Configure a SQLite database
    let sqlite = try SQLiteDatabase(storage: .memory)

    /// Register the configured SQLite database to the database config.
    var databases = DatabasesConfig()
    databases.add(database: sqlite, as: .sqlite)
    services.register(databases)

    /// Configure migrations
    var migrations = MigrationConfig()
    migrations.add(model: Todo.self, database: .sqlite)
    services.register(migrations)

    //Configure a MySQL database
    let mysql = try MySQLDatabase(config: MySQLDatabaseConfig(
        hostname: "127.0.0.1",
        port: 3306,
        username: "root",
        password: "7374",
        database: "WORK_TIME"))

    ///Register to the congig
    var mysqlDatabases = DatabasesConfig()
    mysqlDatabases.add(database: mysql, as: .mysql)
    services.register(mysqlDatabases) 
}

我在 main.swift 中的查询:

public struct MySQLVersion: Codable {
    let version: String
}

router.get("sql") { req in
    return req.withPooledConnection(to: .mysql) {conn in
        return conn.raw("SELECT @@version as version")
            .all(decoding: MySQLVersion.self)
        }.map { rows in
            return rows[0].version
    }
}

它应该 return 我 MySQL 的版本,但它抛出了一个奇怪的错误。

你的问题是原始项目模板中有一些遗留的 SQLite 内容。

首先从您的 Package.swift 文件中删除 fluent-sqlite 依赖项,并从任何目标依赖项中删除 FluentSQLite 目标。然后 运行 swift package update(和 vapor xcode 如果你使用 Xcode)在你的终端。

现在您已经从项目中删除了 FluentSQLite 依赖项,您应该能够按照编译器错误来解决您的问题。以下是我找到的:


import FluentSQLite
import MySQL

应该是:

import FluentMySQL

try services.register(FluentSQLiteProvider())
try services.register(MySQLProvider())

应该是

try services.register(FluentMySQLProvider())

删除它,因为您使用的是 MySQL 而不是 SQLite:

// Configure a SQLite database
let sqlite = try SQLiteDatabase(storage: .memory)

/// Register the configured SQLite database to the database config.
var databases = DatabasesConfig()
databases.add(database: sqlite, as: .sqlite)
services.register(databases)

migrations.add(model: Todo.self, database: .sqlite)

应该是

migrations.add(model: Todo.self, database: .mysql)

我认为这涵盖了一切。您的 /sql 路线现在应该可以使用了。

要解决这个问题,你应该从原始项目中删除所有遗留的 SQLite 东西,并且必须在 routes.swift

中编写你的路由