mysqli connect()使用IP地址时收到错误信息

I pulled this directly off php.net

$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "
";

$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

echo $mysqli->host_info . "
";

my output is like so:

Localhost via UNIX socket

My question is... why am i getting an error message when i try to use the second method.

Failed to connect to MySQL: (1045) Access denied for user '*'@'127.0.0.1' (using password: YES)

Ensure that skip-networking is commented out in your conig file. Also look into your users table and see if "user" is allowed to connect from 127.0.0.1

Go to terminal and type: (login as root user)

mysql -i -u root

Let’s start by making a new user within the MySQL shell (if you do not have (*) user at 127.0.0.1 host):

CREATE USER '*'@'127.0.0.1' IDENTIFIED BY 'password';

Then you've to type the following: (give (*) user permission to access database)

GRANT ALL PRIVILEGES ON * . * TO '*'@'127.0.0.1';

Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges:

FLUSH PRIVILEGES;

Your changes will now be in effect.
How To Grant Different User Permissions Here is a short list of other common possible permissions that users can enjoy.

ALL PRIVILEGES- as we saw previously, this would allow a MySQL user all access to a designated database (or if no database is selected, across the system)

  • CREATE- allows them to create new tables or databases
  • DROP- allows them to them to delete tables or databases
  • DELETE- allows them to delete rows from tables
  • INSERT- allows them to insert rows into tables
  • SELECT- allows them to use the Select command to read through databases
  • UPDATE- allow them to update table rows
  • GRANT OPTION- allows them to grant or remove other users' privileges

    Reference

https://www.digitalocean.com/community/articles/how-to-create-a-new-user-and-grant-permissions-in-mysql