无法通过 Xcode 应用程序连接到 MySQL 服务器,即使我可以通过终端连接它
Can't Connect to MySQL Server through Xcode application even tho I am able to connect it via Terminal
我正在尝试创建一个从 MySQL 服务器获取数据的应用程序。
我从 Homebrew 安装了 MySQL 并使用 PerfectMySql swift 包。
我想先在我的本地主机上测试它,但我无法从我的应用程序连接它。
当我在命令行中 mysql -u root -p
时,它可以正常工作。
我的 MySQL 连接代码:
import PerfectHTTP
import PerfectHTTPServer
import PerfectMySQL
import Foundation
public class DB {
let host = "localhost" // or "127.0.0.1"
let user = "root"
let password = "12345678"
let database = "pets"
func databaseConnect(host: String, user: String, password: String, db: String) -> MySQL {
let mysql = MySQL() // Create an instance of MySQL to work with
let connected = mysql.connect(host: host, user: user, password: password, db: db)
guard connected else {
// verify that we have connected successfully
print(mysql.errorMessage())
return mysql
}
return mysql
}
public func insertGame(title: String, description: String, createdDate: String){
// Connect to our database
var db = databaseConnect(host: host, user: user, password: password, db: database)
defer {
db.close() //This defer block makes sure we terminate the connection once finished, regardless of the result
}
// Create the statement we wish to execute
let statement = MySQLStmt(db)
let insert = "INSERT INTO game(id, title, description, release_date) VALUES (\(statement.insertId()), '\(title)', '\(description)', '\(createdDate)');"
_ = statement.prepare(statement: insert)
// Run the query
let querySuccess = statement.execute()
// Check that our query was successfuly, otherwise return out
guard querySuccess else {
print(db.errorMessage())
return
}
print("Insert successful!");
}
}
当我在 Xcode 中启动我的应用程序时,出现以下错误:
host = "127.0.0.1"
host = "localhost"
我在网络和 Whosebug 上搜索了几个小时,但找不到解决此问题的方法。
这里或其他论坛提出的所有其他问题都是关于通过终端解决连接问题的,这对我来说不是问题。
这里只问了一个相关的问题,但不幸的是没有人回答。 -> 无法连接到 'localhost' 上的 MySQL 服务器
您说您在 macOS 客户端上收到以下错误。
Can't connect to MySQL server on '127.0.0.1' (1)
我知道你现在可能已经明白了,但为了未来的读者,如果你忽略启用此消息(returns 错误代码 2003)可能会在 macOS 目标上产生“传出连接(客户端)”:
我正在尝试创建一个从 MySQL 服务器获取数据的应用程序。
我从 Homebrew 安装了 MySQL 并使用 PerfectMySql swift 包。
我想先在我的本地主机上测试它,但我无法从我的应用程序连接它。
当我在命令行中 mysql -u root -p
时,它可以正常工作。
我的 MySQL 连接代码:
import PerfectHTTP
import PerfectHTTPServer
import PerfectMySQL
import Foundation
public class DB {
let host = "localhost" // or "127.0.0.1"
let user = "root"
let password = "12345678"
let database = "pets"
func databaseConnect(host: String, user: String, password: String, db: String) -> MySQL {
let mysql = MySQL() // Create an instance of MySQL to work with
let connected = mysql.connect(host: host, user: user, password: password, db: db)
guard connected else {
// verify that we have connected successfully
print(mysql.errorMessage())
return mysql
}
return mysql
}
public func insertGame(title: String, description: String, createdDate: String){
// Connect to our database
var db = databaseConnect(host: host, user: user, password: password, db: database)
defer {
db.close() //This defer block makes sure we terminate the connection once finished, regardless of the result
}
// Create the statement we wish to execute
let statement = MySQLStmt(db)
let insert = "INSERT INTO game(id, title, description, release_date) VALUES (\(statement.insertId()), '\(title)', '\(description)', '\(createdDate)');"
_ = statement.prepare(statement: insert)
// Run the query
let querySuccess = statement.execute()
// Check that our query was successfuly, otherwise return out
guard querySuccess else {
print(db.errorMessage())
return
}
print("Insert successful!");
}
}
当我在 Xcode 中启动我的应用程序时,出现以下错误:
host = "127.0.0.1"
host = "localhost"
我在网络和 Whosebug 上搜索了几个小时,但找不到解决此问题的方法。
这里或其他论坛提出的所有其他问题都是关于通过终端解决连接问题的,这对我来说不是问题。
这里只问了一个相关的问题,但不幸的是没有人回答。 -> 无法连接到 'localhost' 上的 MySQL 服务器
您说您在 macOS 客户端上收到以下错误。
Can't connect to MySQL server on '127.0.0.1' (1)
我知道你现在可能已经明白了,但为了未来的读者,如果你忽略启用此消息(returns 错误代码 2003)可能会在 macOS 目标上产生“传出连接(客户端)”: