0

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'

oğuz
  • 145
  • 1
  • 13
  • I have never heard anyone send data directly to a MySQL database. They usually do it through a PHP file on a remote server. – El Tomato Dec 05 '20 at 02:58
  • "that fetches data from the MySQL server" It seems that you are inserting a record to a dabase, not getting data from it. – El Tomato Dec 05 '20 at 03:07
  • @ElTomato I created a Windows application using C# in the same way I used here. This is my first attempt using MySQL and I followed the tutorial. The author managed to do it without any problems and I achieved the same result on my Windows application. I didn't want to use PHP because (first of all, I don't know PHP) the application will show only 1 table from the server. – oğuz Dec 05 '20 at 03:10
  • @ElTomato yes the final application's purpose is fetching from the server, but as I said, I'm trying to test and learn on a project before working on the main server. – oğuz Dec 05 '20 at 03:12
  • I can't help you. I don't know a guy named MySQL() or its instances. – El Tomato Dec 05 '20 at 03:24
  • @ElTomato If writing server code in Swift, you may very well connect with the server like this. But, yes, if writing client code, you generally would not connect with the server directly like that. – Rob Dec 05 '20 at 22:48

1 Answers1

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

enter image description here

Rob
  • 415,655
  • 72
  • 787
  • 1,044