在 Ubuntu 上配置 ThinkPHP 数据库,你需要遵循以下步骤:
对于 MySQL,使用以下命令安装:
sudo apt-get update
sudo apt-get install mysql-server
对于 PostgreSQL,使用以下命令安装:
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
对于 MySQL,使用以下命令安装 PHP MySQL 扩展:
sudo apt-get install php-mysql
对于 PostgreSQL,使用以下命令安装 PHP PostgreSQL 扩展:
sudo apt-get install php-pgsql
对于 Apache,使用以下命令重启:
sudo systemctl restart apache2
对于 Nginx,使用以下命令重启:
sudo systemctl restart nginx
登录到 MySQL 或 PostgreSQL 控制台:
# 对于 MySQL
sudo mysql
# 对于 PostgreSQL
sudo -u postgres psql
在 MySQL 中创建数据库和用户:
CREATE DATABASE your_database_name;
CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
在 PostgreSQL 中创建数据库和用户:
CREATE DATABASE your_database_name;
CREATE USER your_user WITH ENCRYPTED PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_user;
EXIT;
打开你的 ThinkPHP 项目中的 .env
文件(如果没有这个文件,请创建一个),并添加以下内容(根据你的数据库类型和凭据进行修改):
对于 MySQL:
DB_TYPE=mysql
DB_HOST=127.0.0.1
DB_NAME=your_database_name
DB_USER=your_user
DB_PWD=your_password
DB_PORT=3306
DB_PREFIX=
对于 PostgreSQL:
DB_TYPE=pgsql
DB_HOST=127.0.0.1
DB_NAME=your_database_name
DB_USER=your_user
DB_PWD=your_password
DB_PORT=5432
DB_PREFIX=
index.php
或 application.php
)中,确保你加载了 .env
文件中的配置。现在,你的 ThinkPHP 项目应该已经成功配置了数据库。你可以开始开发和运行你的应用程序了。