简体   繁体   中英

Creat remote connection to mysql with mysqli_connect()

I have a new server with MySQL installed, 1 user (an other than root user), I setup a database with tables. I want to be able to connect from my other server with my PHP code to this new server's database. So for that, I make a

mysqli_connect($servername, $username, $password, $dbname, $db_port);

but there is this error:

mysqli_connect(): (HY000/2002)

I can affirme that this is not a PHP code error, but certainly a SQL one. I don't know if that come from remote possibility connexion with my new server, or a problem of user generation. Just to begin, we do agree that in PHP $servername is egal to my new server's IP and $db_port is egal to 3306, like return by SHOW GLOBAL VARIABLES LIKE 'PORT'; in mysql?

And thank you very much for your time, I ask my question here, because I did a lot of research on the subject and nothing helped me. Can someone enlighten me?

For people who had the same problem, I finally solved it:
I create a db
CREATE MYSQL db_name; after I create a user
CREATE USER 'user_name'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; % mean from everywhere.
After that I used
GRANT your_privileges ON db_name.* TO 'user_name'@'%';
FLUSH PRIVILEGES;

After that, I just had to open my firewall provider to the MYSQL port (3306), and I opened it to every incoming ip.
And that was good !

Thank you for your help:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM