在 Linux 下使用 Laravel 迁移(Migration) 来管理数据库结构,是非常常见的做法。下面从 环境准备 → 创建迁移 → 执行迁移 → 常见问题 一步一步说明。
已安装
Laravel 项目已存在
cd /var/www/your-laravel-project
配置数据库
编辑 .env 文件:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=password
测试连接:
php artisan tinker
>>> DB::connection()->getPdo();
php artisan make:migration create_users_table
生成文件位置:
database/migrations/2024_01_01_000000_create_users_table.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
php artisan migrate
php artisan migrate --force
php artisan migrate:status
php artisan migrate:rollback
php artisan migrate:reset
php artisan migrate:refresh
php artisan migrate:fresh
chown -R www-data:www-data storage bootstrap/cache
chmod -R 775 storage
php artisan config:cache
php artisan route:cache
mysqldump -u root -p laravel > backup.sql
Class not foundcomposer dump-autoload
SQLSTATE[HY000] Access denied.envsystemctl restart mysqlMigration table not foundphp artisan migrate:install
cd /var/www/laravel
cp .env.example .env
php artisan key:generate
php artisan make:migration create_posts_table
# 编辑 migration
php artisan migrate
如果你具体是指:
可以告诉我具体场景,我可以给你更针对性的方案。