oğuz
oğuz

Reputation: 180

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

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.

enter image description here

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"

enter image description here

host = "localhost"

enter image description here

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'

Upvotes: 0

Views: 404

Answers (1)

Rob
Rob

Reputation: 438467

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)”:

enter image description here

Upvotes: 1

Related Questions