laravel¿ò¼Üdb³¬Ê±ÉèÖÃ
ÔÚʹÓÃlaravel¿ò¼Ü¿ª·¢Ó¦ÓóÌÐòʱ£¬¾³£»áÉæ¼°µ½ÓëÊý¾Ý¿â¾ÙÐн»»¥µÄÇéÐΡ£¿ÉÊÇ£¬ÔÚһЩÌØÊâµÄÇéÐÎÏ£¬ÎÒÃÇ¿ÉÄÜ»áÓöµ½Êý¾Ý¿â³¬Ê±µÄÎÊÌâ¡£Õâʱ£¬¾ÍÐèÒª¶Ôlaravel¿ò¼ÜµÄÊý¾Ý¿âÅþÁ¬¾ÙÐÐһЩÉèÖã¬ÒÔ×èÖ¹·ºÆð³¬Ê±µÄÇéÐΡ£±¾ÎĽ«ÎªÄúÏÈÈÝÔõÑùÔÚlaravel¿ò¼ÜÖÐÉèÖÃÊý¾Ý¿âÅþÁ¬µÄ³¬Ê±Ê±¼ä¡£
Ò»¡¢Laravel¿ò¼ÜÖеÄÊý¾Ý¿âÅþÁ¬
ÔÚLaravel¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýʹÓÃIlluminateDatabaseDatabaseManagerÀàÀ´ÅþÁ¬²î±ðÀàÐ͵ÄÊý¾Ý¿â¡£Õâ¸öÀàÊÇLaravel¿ò¼ÜÖÐÓÃÀ´ÖÎÀíÊý¾Ý¿âÅþÁ¬µÄÖ÷ÒªÀà¡£
ÔÚLaravel¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÒªÁìÀ´»ñÈ¡²î±ðÀàÐ͵ÄÊý¾Ý¿âÅþÁ¬£º
DB::connection(‘connection_name’)£º»ñÈ¡ÌØÃüÃû³ÆµÄÊý¾Ý¿âÅþÁ¬¡£
DB::getPdo()£º»ñÈ¡Ä¿½ñʹÓõÄPDOʵÀý¡£
DB::table(‘table_name’)£º»ñÈ¡Ö¸¶¨Ãü¾Ý±íµÄÅÌÎʽṹÆ÷ʵÀý¡£
ÔÚLaravel¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔÔÚconfig/database.phpÎļþÖнç˵²î±ðµÄÊý¾Ý¿âÅþÁ¬ÉèÖÃÐÅÏ¢¡£ÀýÈ磬ÏÂÃæÊÇÒ»¸öMySQLÊý¾Ý¿âÅþÁ¬ÉèÖõÄʾÀý£º
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ],
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÉèÖÃÐÅÏ¢ÖУ¬ÎÒÃÇ¿ÉÒÔÉèÖòî±ðµÄÊý¾Ý¿âÅþÁ¬²ÎÊý£¬ÀýÈ磺Êý¾Ý¿âЧÀÍÆ÷µÄIPµØµã¡¢¶Ë¿ÚºÅ¡¢Êý¾Ý¿âÃû³Æ¡¢Óû§Ãû¡¢ÃÜÂëµÈµÈ¡£
¶þ¡¢Êý¾Ý¿âÅþÁ¬µÄ³¬Ê±ÎÊÌâ
ÔÚһЩÇéÐÎÏ£¬ÎÒÃÇ¿ÉÄ᷺ܻÆðÊý¾Ý¿âÅþÁ¬³¬Ê±µÄÎÊÌâ¡£ÕâÖÖÇéÐÎͨ³£±¬·¢ÔÚÊý¾Ý¿â³¤Ê±¼äûÓÐÏìÓ¦»òÕßÍøÂçÅþÁ¬·ºÆðÎÊÌâµÄÇéÐÎÏ¡£µ±Êý¾Ý¿âÅþÁ¬³¬Ê±Ê±£¬×ðÁú¿Ê±Ó¦ÓóÌÐò¿ÉÄ᷺ܻÆð¹ýʧ£¬ÉõÖÁÍ߽⡣Òò´Ë£¬ÎÒÃÇÐèÒª¶ÔLaravel¿ò¼ÜÖеÄÊý¾Ý¿âÅþÁ¬¾ÙÐÐһЩÉèÖã¬ÒÔ×èÖ¹·ºÆð³¬Ê±µÄÇéÐΡ£
Èý¡¢Êý¾Ý¿âÅþÁ¬³¬Ê±µÄ½â¾ö¼Æ»®
ÔÚLaravel¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÐÞ¸Äconfig/database.phpÎļþÖеÄÉèÖÃÐÅÏ¢£¬À´ÉèÖÃÊý¾Ý¿âÅþÁ¬µÄ³¬Ê±Ê±¼ä¡£
µ¥¸öÊý¾Ý¿âÅþÁ¬µÄ³¬Ê±ÉèÖÃ
ÎÒÃÇ¿ÉÒÔÔÚµ¥¸öÊý¾Ý¿âÅþÁ¬µÄÉèÖÃÐÅÏ¢ÖУ¬ÉèÖÃÅþÁ¬µÄ³¬Ê±Ê±¼ä¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚMySQLÊý¾Ý¿âÅþÁ¬µÄÉèÖÃÖÐÉèÖÃ’connect_timeout’²ÎÊý£¬À´ÉèÖÃÅþÁ¬µÄ³¬Ê±Ê±¼ä£º
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, 'options' => [ PDO::ATTR_TIMEOUT => 5, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_PERSISTENT => false, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4', time_zone = '+08:00'", PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false, PDO::MYSQL_ATTR_READ_DEFAULT_FILE => '/usr/local/etc/my.cnf', ], ],
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÉèÖÃÐÅÏ¢ÖУ¬ÎÒÃÇÉèÖÃÁËPDO::ATTR_TIMEOUT²ÎÊýµÄֵΪ5Ãë¡£ÕâÒâζ×Å£¬ÈôÊÇÅþÁ¬µÄʱ¼äÁè¼Ý5Ã룬Laravel¿ò¼Ü¾Í»áÅ׳öÒ»¸öÅþÁ¬³¬Ê±µÄ¹ýʧ¡£
ËùÓÐÊý¾Ý¿âÅþÁ¬µÄ³¬Ê±ÉèÖÃ
ÎÒÃÇÒ²¿ÉÒÔÔÚconfig/database.phpÎļþÖУ¬ÉèÖÃËùÓÐÊý¾Ý¿âÅþÁ¬µÄ³¬Ê±Ê±¼ä¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÌí¼ÓÒÔÏ´úÂ룺
'options' => [ PDO::ATTR_TIMEOUT => 5, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_PERSISTENT => false, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4', time_zone = '+08:00'", PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false, PDO::MYSQL_ATTR_READ_DEFAULT_FILE => '/usr/local/etc/my.cnf', ],
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÉèÖÃÐÅÏ¢ÖУ¬ÎÒÃÇͬÑùÉèÖÃÁËPDO::ATTR_TIMEOUT²ÎÊýµÄֵΪ5Ãë¡£ÕâÒâζ×Å£¬ÎÞÂÛÊÇÄĸöÊý¾Ý¿âÅþÁ¬£¬ËüÃǵÄÅþÁ¬³¬Ê±Ê±¼ä¾ùΪ5Ãë¡£ÕâÖÖÉèÖ÷½·¨£¬¿ÉÒÔÀû±ãµØͳһÖÎÀíËùÓеÄÊý¾Ý¿âÅþÁ¬£¬²¢ÇÒÒ×ÓÚά»¤¡£
ËÄ¡¢×ܽá
ÔÚLaravel¿ò¼ÜÖУ¬Êý¾Ý¿âÅþÁ¬µÄ³¬Ê±ÎÊÌâÊÇÒ»¸ö·Ç¾³£¼ûµÄÎÊÌ⡣ΪÁË×èÖ¹ÕâÖÖÎÊÌâµÄ±¬·¢£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃÊý¾Ý¿âÅþÁ¬µÄ³¬Ê±Ê±¼äÀ´½â¾öÕâ¸öÎÊÌâ¡£ËäÈ»£¬ÎÒÃÇ»¹¿ÉÒÔʹÓÃÆäËûµÄÒªÁìÀ´ÓÅ»¯Laravel¿ò¼ÜµÄÊý¾Ý¿âÅþÁ¬£¬ÀýÈçʹÓÃÅþÁ¬³ØµÈÊÖÒÕÊֶΡ£²»¹ÜÊÇÄÄÖÖÒªÁ죬¶¼¿ÉÒÔÈÃÎÒÃǸüºÃµØÖÎÀí¡¢ÓÅ»¯Laravel¿ò¼ÜµÄÊý¾Ý¿âÅþÁ¬£¬´Ó¶øÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄܺÍÎȹÌÐÔ¡£
ÒÔÉϾÍÊÇlaravel¿ò¼Üdb³¬Ê±ÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡