连接php到mysql社区服务器

xxe27gdn  于 2023-05-16  发布在  Mysql
关注(0)|答案(1)|浏览(96)

我有两个xamppmysql community server安装在我的Ubuntu 22.04。由于xampp/phpmyadminmysql community server都侦听port 3306,因此我更改了/opt/lampp/etc/php.inimysqli.default_port=3307,并在/etc/mysql/conf.d/mysql.cnf中的[mysql]下添加了一行port = 3307。现在我也使用以下命令从我的mysql中提取了主机名

service mysql start
mysql -u root -p
show variables like '%host%';

它说我的主机名是solo-ROG-Strix-G731GV-G731GV,我还在数据库上创建了一个具有所有赠款的用户,如

create database test;
create user 'test'@'solo-ROG-Strix-G731GV-G731GV' identified by '1234';
grant all privileges on test.* to 'test'@'solo-ROG-Strix-G731GV-G731GV';
flush privileges

然后我确保phpmysqladmin没有运行,只运行xampppache使用

cd /opt/lampp/
sudo ./xampp startapache

这里是我的PHP代码连接到MySQL社区服务器

<?php
const MYSQLI_HOSTNAME = "solo-ROG-Strix-G731GV-G731GV";
const MYSQLI_ADMIN_USERNAME = "test";
const MYSQLI_ADMIN_PASSWORD = "1234";
const MYSQLI_DATABASE = "test";
$db_admin = new mysqli(MYSQLI_HOSTNAME, MYSQLI_ADMIN_USERNAME,
    MYSQLI_ADMIN_PASSWORD, MYSQLI_DATABASE
, 3307);

if($db_admin->connect_error){
    echo $db_admin->connect_error;
}

现在显示为connection refused
全误差

Fatal error: Uncaught mysqli_sql_exception: Connection refused in /opt/lampp/htdocs/baharshop/src/lib/db/db_admin.php:2 Stack trace: #0 /opt/lampp/htdocs/baharshop/src/lib/db/db_admin.php(2): mysqli->__construct('solo-ROG-Strix-...', 'test', Object(SensitiveParameterValue), 'test', 3307) #1 /opt/lampp/htdocs/baharshop/config/config_admin.php(17): require_once('/opt/lampp/htdo...') #2 /opt/lampp/htdocs/baharshop/admin/dashboard.php(3): require_once('/opt/lampp/htdo...') #3 {main} thrown in /opt/lampp/htdocs/baharshop/src/lib/db/db_admin.php on line 2`

我是说还需要做什么?
我尝试在php.ini和mysql社区服务器上更改端口

jyztefdp

jyztefdp1#

port = 3307应该在参数文件的[mysqld]选项中,而不是[mysql]。更改端口需要重新启动数据库服务。

相关问题