在 Debian 系统上,让 Laravel 连接数据库通常包含以下步骤(以 MySQL / MariaDB 为例,也适用于 PostgreSQL 等)。
sudo apt update
sudo apt install mariadb-server -y
启动并设置开机自启:
sudo systemctl enable mariadb
sudo systemctl start mariadb
初始化安全设置(可选):
sudo mysql_secure_installation
sudo mysql -u root -p
在 MySQL 中执行:
CREATE DATABASE laravel_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
sudo apt install php php-cli php-mysql php-mbstring php-xml php-curl php-zip unzip -y
如果是 Nginx:
sudo apt install php-fpm
.env 文件DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=password
php artisan config:clear
php artisan tinker
DB::connection()->getPdo();
没有报错即连接成功。
php artisan migrate
sudo chown -R www-data:www-data storage bootstrap/cache
CREATE USER 'laravel_user'@'%' IDENTIFIED BY 'password';
并修改:
bind-address = 0.0.0.0
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel_db
DB_USERNAME=postgres
DB_PASSWORD=password
安装:
sudo apt install postgresql php-pgsql
如果你告诉我:
我可以给你更精确的配置方案。