I am new to connecting Swift, Xcode, and MySQL db. I keep getting an error that says:
connect(descriptor:addr:size:): Operation not permitted (errno: 1).
Here is my code:
import Foundation
import MySQLNIO
import NIO
class DatabaseManager {
static let shared = DatabaseManager() // Singleton instance
private let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
private var connection: EventLoopFuture<MySQLConnection>?
private init() {}
deinit {
try? eventLoopGroup.syncShutdownGracefully()
}
func setupConnection() {
do {
// Explicitly create a UnixSocketAddress
let unixSocketAddress = try SocketAddress(unixDomainSocketPath: "/my/address") // here "address" is the address given to me in the terminal when I checked the status of MySQL
connection = MySQLConnection.connect(
to: unixSocketAddress,
username: "root",
database: "dbName",
password: "pwd",
tlsConfiguration: nil,
on: eventLoopGroup.next()
)
connection?.whenComplete { result in
switch result {
case .success:
print("Successfully connected to MySQL!")
case .failure(let error):
print("Failed to connect to MySQL: (error)")
}
}
} catch {
print("Failed to create UnixSocketAddress: (error)")
}
}
func testConnection() {
connection?.whenComplete { result in
switch result {
case .success(let conn):
print("Connection successful!")
conn.close().whenComplete { closeResult in
switch closeResult {
case .success:
print("Connection closed successfully.")
case .failure(let error):
print("Failed to close connection: (error)")
}
}
case .failure(let error):
print("Connection failed: (error)")
}
}
}
}
In Xcode, under Project / “Signing & Capabilities” / “App Sandbox” / “Network”, I have “Outgoing Connections (Client)” checked.
I saw this as a solution in stack overflow for this error, but that did not fix the problem.
New contributor
Brendalis Sanchez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.