Can't Connect to MySQL Server through Xcode application even tho I am able to connect it via Terminal

折月煮酒 提交于 2021-02-08 09:57:50

问题


I'm trying to create an application that fetches data from the MySQL server.

I installed MySQL from Homebrew and using the PerfectMySql swift package.

I want to test it on my localhost first but I'm unable to connect it from my application.

When I mysql -u root -p in the command line, it works as it should.

My code for MySQL connection:

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!");
    }
    
}

When I start my application in Xcode, it gives me this error:

host = "127.0.0.1"

host = "localhost"

I searched the web and StackOverflow for hours but couldn't find anything to fix this issue.

Every other question asked here or other forums are about fixing connection problems via Terminal, which is not the issue for me here.

Only a related question asked here but it is unfortunately unanswered. -> Can't connect to MySQL server on 'localhost'


回答1:


You said that you were receiving the following error on your macOS client.

Can't connect to MySQL server on '127.0.0.1' (1)

I know you've probably figured it out by now, but for the sake of future readers, this message (which returns an error code of 2003) can result on macOS targets if you neglect to enable “Outgoing Connections (Client)”:



来源:https://stackoverflow.com/questions/65153324/cant-connect-to-mysql-server-through-xcode-application-even-tho-i-am-able-to-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!