我遇到了Laravel Mysql连接错误。

3

当我想要运行 php artisan migrate 时,出现了这个错误。错误消息如下:

   Illuminate\Database\QueryException

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id bigint unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null, email_verified_at timestamp null, password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

at C:\composer\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671 667▕ // If an exception occurs when attempting to run a query, we'll format the error 668▕ // message to include the bindings with SQL, which will make this exception a 669▕ // lot more helpful to the developer instead of just the database's errors. 670▕ catch (Exception $e) { ➜ 671▕ throw new QueryException( 672▕ $query, $this->prepareBindings($bindings), $e 673▕ ); 674▕ } 675▕

1
C:\composer\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464 PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists")

2
C:\composer\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464 PDOStatement::execute()

数据库设置如下。 Database.php 文件中的设置如下:

 'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'laravel'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
.env 文件中的设置如下。
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:sBKduFaKhOJdg7/A1U4IzAUnj3yLLcjngjmMvEoWl94=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

注意:我在wampserver中使用mysql。


1
欢迎来到SO.. 1050表'users'已经存在,你能看到它所说的错误是users已经存在,所以不能再创建了,或者你可以尝试php artisan migrate:fresh - Kamlesh Paul
我已经尝试过了,没有任何改变。仍然会出现错误。 - Mert Çevik
勇敢地删除用户表。 - Kamlesh Paul
php artisan db:wipe(它将从数据库中删除所有表),然后运行php artisan migrate。 - crescast
4个回答

2

删除已迁移的文件,然后执行迁移命令。


2

以下是我解决相同问题的步骤:

  1. 在控制台中输入 => php artisan tinker

  2. 然后再次在控制台中输入 => Schema::drop('users')

  3. 最后 => php artisan migrate


当它按照您的要求执行时,会出现以下错误。SQLSTATE[42000]: 语法错误或访问冲突:1071 指定的键太长;最大键长度为1000字节(SQL:alter table users add unique users_email_unique(email)) - Mert Çevik
请更新您的/app/Providers/AppServiceProvider.php文件,添加以下内容:use Illuminate\Support\Facades\Schema;/**
  • 引导任何应用程序服务。
  • @return void */ public function boot() { Schema::defaultStringLength(191); }
- Pruthviraj Chauhan

0

或许下面的命令可以帮助你!

php artisan migrate:fresh

迁移命令及其用途

  migrate:fresh        Drop all tables and re-run all migrations
  migrate:install      Create the migration repository
  migrate:refresh      Reset and re-run all migrations
  migrate:reset        Rollback all database migrations
  migrate:rollback     Rollback the last database migration
  migrate:status       Show the status of each migratioin

你能否扩展你的回答,包括这个命令的作用是什么? - Yay295

0
只需在Windows中转到php路径,您将在此处找到它
Windows中的php路径 C:\ tools \ php82 \ php.ini
然后取消注释您想要的php扩展 对我来说,我取消了pdo_mysql和sqlite的注释,运行良好

1
你的回答可以通过提供更多支持性的信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接