douyao1994 2018-07-10 19:51 采纳率: 100%
浏览 73
已采纳

PHP连接MySQL错误

I keep on getting this error : Warning: mysqli_connect(): (HY000/1045): Access denied for user '12345'@'localhost' (using password: YES). I have already tried to create a new user, using a password, using no password, granting privileges to new user. I can login to the mysql database using these credentials. I can run every command with them in MySQL, just not connect to PHP. If it matters, this is my php code

<? php 
$dbServername = "localhost";
$dbUsername = "12345";
$dbPassword = "12345";
$dbName = "loginsystem";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

When showing grants for user: '12345'@'localhost':

mysql> SHOW GRANTS FOR '12345'@'localhost';
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for 12345@localhost                                                                                                                                                                                                                                                                                                                                                              |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `12345`@`localhost` |
| GRANT BACKUP_ADMIN,BINLOG_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SET_USER_ID,SYSTEM_VARIABLES_ADMIN,XA_RECOVER_ADMIN ON *.* TO `12345`@`localhost`                                                                                                  |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

/etc/my.cnf file (INSIDE MYSQL) :

[mysqld_safe]
[mysqld]
secure_file_priv="/Users/Steven/"

/etc/my.cnf file (INSIDE xAMPP):

#password   = your_password
port        = 3306
socket      = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
user = mysql
port=3307
socket      = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
skip-external-locking
key_buffer = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
innodb_force_recovery = 1

Oh I forgot to mention I did flush privileges and restarts of server.

  • 写回答

1条回答 默认 最新

  • douyun4524 2018-07-11 16:57
    关注

    Hello @Steven in my experience xampp gives error on Mac (at least in my opinion) so i used MAMP. You can check it if you want. BUT going to the point of ur error this seems to be a connection issue: try this connection code please:

    $user = '12345'; 
    $password = '12345'; 
    $db = 'loginsystem'; 
    $host = 'localhost'; 
    $port = your_port-might-be-3306; 
    $link = mysqli_init(); 
    $success = mysqli_real_connect( $link, $host, $user, $password, $db, $port ); 
    echo $success;
    

    If the $success variable shows a 1 if the connections is working. If so, then your connection works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?