Welcome Guest, Not a member yet? Register   Sign In
I always get mysqli_sql_exception #2002
#1

Hi, I am new to Ci. I have completed a few tutorials and everything works OK until I get to connecting to MySQL. I am using MAMP pro, a Macbook pro and Mac OS. 

mysqli_sql_exception #2002

SYSTEMPATH/Database/MySQLi/Connection.php at line 225

---------------

This is what I have done so far to debug:

I have found a way to get the page to load, run PHP and display data from the database -> I turned on 'Allow network Access to MySQL' in MAMP and ran the index.php file in the public folder. The error went away and I see the page loading -> so the code is not incorrect. Interestingly, when I 'Allow network Access to MySQL' and try to view the project in my browser, I get the MySQL connection error.

Every time, I try to 'php spark serve' and run the application on localhost:8080 -> I get the error mysqli_sql_exception #2002 where Ci can't connect to MySQL.

--------------

I have seen a fgew other posts on Stack Exchange and also on the Ci form about this. Does anyone know how to fix it?

I am assuming, I should always use 'php spark serve' and not open the file in a path within my htdocs?

Attached Files Thumbnail(s)
       
Reply
#2

"php spark serve" uses a different php.ini than your MAMP, so it may not have mysqli or other settings enabled. "php spark serve" are the fast way of getting things up and running, but you can use either of them.

If you choose to go the MAMP path, you need to create a virtual host and point it into your public folder. You should never do:
http://localhost/codeigniter4/public to run your app.

"No such file or directory" means your MySQL server don't have a correct socket path.
Reply
#3

OK, this issue is solved. If you are using MAMP pro on a mac, you need to set up a new host as the public folder in your Ci project (screenshot attached)
Reply
#4

For anyone else who may be stuck with this issue:

I have a similar setup: macOS, MAMP (non-Pro) running MySQL, individual Terminal tab/session running `php spark serve`. After hours of searching and debugging, I finally stumbled upon a StackOverflow question that solved this for me by changing "localhost" in "/app/Config/Database.php" to "127.0.0.1". Apparently, you have to define a socket if you want to use "localhost" and using "127.0.0.1" instead rides on the TCP/IP connection.

Here's the SO link:
https://stackoverflow.com/questions/4188...-directory
Reply




Theme © iAndrew 2016 - Forum software by © MyBB