在CentOS上设置Laravel数据库连接,你需要遵循以下步骤:
composer create-project --prefer-dist laravel/laravel your_project_name
将your_project_name
替换为你的项目名称。
sudo yum install mysql-server
对于PostgreSQL,请运行:
sudo yum install postgresql-server
sudo systemctl start mysqld
sudo systemctl enable mysqld
对于PostgreSQL,请运行:
sudo systemctl start postgresql
sudo systemctl enable postgresql
mysql -u root -p
输入密码后,创建数据库和用户:
CREATE DATABASE your_database_name;
CREATE USER 'your_user_name'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_user_name'@'localhost';
FLUSH PRIVILEGES;
EXIT;
对于PostgreSQL,请运行:
sudo -u postgres psql
创建数据库和用户:
CREATE DATABASE your_database_name;
CREATE USER your_user_name WITH ENCRYPTED PASSWORD 'your_password';
ALTER ROLE your_user_name SET client_encoding TO 'utf8';
ALTER ROLE your_user_name SET default_transaction_isolation TO 'read committed';
ALTER ROLE your_user_name SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_user_name;
\q
.env
文件,找到以下行:DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_user_name
DB_PASSWORD=your_password
根据你在第4步中创建的数据库和用户信息,修改这些值。例如,如果你使用的是MySQL数据库,那么配置应该如下所示:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_user_name
DB_PASSWORD=your_password
php artisan migrate
如果一切正常,你应该会看到迁移成功的消息。这意味着Laravel已成功连接到CentOS上的数据库。
现在你已经成功设置了Laravel数据库连接。你可以开始开发你的应用程序了。