Laravel项目问题集锦

穆子琪
2023-12-01

1、No supported encrypter found. The cipher and / or key length are invalid.
解决方法:

cd 项目根目录
// 注:新版本生成的key值,外层有[],不要复制错了
php artisan key:generate
// 执行完之后会生成一个key串,将生成的key复制到config/app.php替换82行的APP_KEY键值。
'key' => env('APP_KEY', '8lEB3iDioWwxdTX2i7dp5jvOCAWDyAWV'),

2、用artisan命令创建model时提示[RuntimeException]
Unable to detect application namespace.

php artisan make:model Models\User(指定创建路径)

解决方法:
查看composer.json是否有语法错误。本人在项目中遇到此错误是因为composer.json里有语法错误。

3、laravel5.2 执行php artisan migrate时报如下错误
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for ‘created_at’

This is due to MySQL not accepting zero as a valid defalt date and thus the table creation fails a constraint check on creation.

解决方法:
打开config/database.php 修改

'mysql' => array(
   'strict'    => true  // strict 模型
),

4、PDOException SQLSTATE[HY000] [2002] No such file or directory

在执行php artisan migrate时报上述错误的原因是因为在linux中连接mysql时使用localhost和127.0.0.1连接数据库的机制是不一样的。

当主机填写为localhost时mysql会采用 unix domain socket连接
当主机填写为127.0.0.1时mysql会采用tcp方式连接

第一种解决方法:
查看连接方式及unix socket的连接位置

[root@centos l5beauty]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.30 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like '%sock%';
+-----------------------------------------+-----------------------------+
| Variable_name                           | Value                       |
+-----------------------------------------+-----------------------------+
| performance_schema_max_socket_classes   | 10                          |
| performance_schema_max_socket_instances | 322 
// 这里是socket的路径                        |
| socket                                  | /usr/local/mysql/mysql.sock |
+-----------------------------------------+-----------------------------+
3 rows in set (0.01 sec)

mysql> status
--------------
mysql  Ver 14.14 Distrib 5.6.30, for Linux (i686) using  EditLine wrapper

Connection id:      11
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.6.30 Source distribution
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
// 这里是socket的路径
UNIX socket:        /usr/local/mysql/mysql.sock
Uptime:         --------------

然后修改config\database.php中的mysql连接配置如下

   'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'blog'),
            'username'  => env('DB_USERNAME', 'root'),
            'password'  => env('DB_PASSWORD', '123456'),
            'charset'   => 'utf8',
            // 加入如下一行
            'unix_socket' => '/usr/local/mysql/mysql.sock',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => true,
        ],

第二种解决方法:
使用phpinfo查看php的配置并找到如下配置,也可以使用php -i | grep pdo查看

Directive                       Local Value   Master Value
pdo_mysql.default_socket    /tmp/mysql.sock /tmp/mysql.sock

修改php.ini的配置

//修改成如下值之后重启php-fpm 
pdo_mysql.default_socket=/usr/local/mysql/mysql.sock
 类似资料: