mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
813 lines
59 KiB
HTML
813 lines
59 KiB
HTML
<!DOCTYPE html>
|
||
<html class="writer-html5" lang="en">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>Database Configuration — CodeIgniter 4.6.0 documentation</title>
|
||
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
|
||
<link rel="stylesheet" type="text/css" href="../_static/css/citheme.css" />
|
||
<link rel="stylesheet" type="text/css" href="../_static/css/citheme_dark.css" />
|
||
|
||
|
||
<link rel="shortcut icon" href="../_static/favicon.ico"/>
|
||
<!--[if lt IE 9]>
|
||
<script src="../_static/js/html5shiv.min.js"></script>
|
||
<![endif]-->
|
||
|
||
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/_sphinx_javascript_frameworks_compat.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/sphinx_highlight.js"></script>
|
||
<script src="../_static/js/citheme.js"></script>
|
||
<script src="../_static/js/carbon.js"></script>
|
||
<script src="../_static/js/theme.js"></script>
|
||
<link rel="index" title="Index" href="../genindex.html" />
|
||
<link rel="search" title="Search" href="../search.html" />
|
||
<link rel="next" title="Connecting to your Database" href="connecting.html" />
|
||
<link rel="prev" title="Quick Start: Usage Examples" href="examples.html" />
|
||
</head>
|
||
|
||
<body class="wy-body-for-nav">
|
||
<div class="wy-grid-for-nav">
|
||
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
||
<div class="wy-side-scroll">
|
||
<div class="wy-side-nav-search" >
|
||
|
||
|
||
|
||
<a href="../index.html">
|
||
|
||
<img src="../_static/ci-logo-text.svg" class="logo" alt="Logo"/>
|
||
</a>
|
||
<div role="search">
|
||
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
|
||
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../license.html">License Agreement</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/deployment.html">Deployment</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../changelogs/index.html">Change Logs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static Pages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News Section</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create News Items</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/factories.html">Factories</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working with HTTP Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/goals.html">Design and Architectural Goals</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/configuration.html">Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helper Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Global Functions and Constants</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/logging.html">Logging Information</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Web Page Caching</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/ajax.html">AJAX Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/modules.html">Code Modules</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/routing.html">URI Routing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/controllers.html">Controllers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/filters.html">Controller Filters</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/auto_routing_improved.html">Auto Routing (Improved)</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/message.html">HTTP Messages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/request.html">Request Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/incomingrequest.html">IncomingRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/content_negotiation.html">Content Negotiation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/methodspoofing.html">HTTP Method Spoofing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/restful.html">RESTful Resource Handling</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/views.html">Views</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_renderer.html">View Renderer</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_layouts.html">View Layouts</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_cells.html">View Cells</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_parser.html">View Parser</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_decorators.html">View Decorators</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/table.html">HTML Table Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/response.html">HTTP Responses</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/api_responses.html">API Response Trait</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/csp.html">Content Security Policy</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/localization.html">Localization</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/alternative_php.html">Alternate PHP Syntax for View Files</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul class="current">
|
||
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Working with Databases</a><ul class="current">
|
||
<li class="toctree-l2"><a class="reference internal" href="examples.html">Quick Start: Usage Examples</a></li>
|
||
<li class="toctree-l2 current"><a class="current reference internal" href="#">Database Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="connecting.html">Connecting to a Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="queries.html">Running Queries</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="results.html">Generating Query Results</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="helpers.html">Query Helper Methods</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="query_builder.html">Query Builder Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="transactions.html">Transactions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="metadata.html">Getting Metadata</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="call_function.html">Custom Function Calls</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="events.html">Database Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="utilities.html">Database Utilities</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/model.html">Using CodeIgniter's Model</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/entities.html">Using Entity Classes</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/forge.html">Database Forge</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/migration.html">Database Migrations</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/seeds.html">Database Seeding</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/db_commands.html">Database Commands</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">Caching Driver</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/cookies.html">Cookies</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/cors.html">Cross-Origin Resource Sharing (CORS)</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/curlrequest.html">CURLRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Service</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/files.html">Working with Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/file_collections.html">File Collections</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/honeypot.html">Honeypot Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/images.html">Image Manipulation Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/publisher.html">Publisher</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/throttler.html">Throttler</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/time.html">Times and Dates</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/uploaded_files.html">Working with Uploaded Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">Working with URIs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">User Agent Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/validation.html">Validation</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/overview.html">Getting Started</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/database.html">Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/fabricator.html">Generating Data</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/controllers.html">Controller Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/feature.html">HTTP Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/response.html">Testing Responses</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/cli.html">Testing CLI Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/mocking.html">Mocking</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/benchmark.html">Benchmarking</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_overview.html">CLI Overview</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_controllers.html">Running Controllers via CLI</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/spark_commands.html">Spark Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Creating Spark Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_generators.html">CLI Generators</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_library.html">CLI Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_request.html">CLIRequest Class</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/composer_packages.html">Creating Composer Packages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../libraries/official_packages.html">Official Packages</a></li>
|
||
</ul>
|
||
|
||
</div>
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
|
||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
||
<a href="../index.html">CodeIgniter</a>
|
||
</nav>
|
||
|
||
<div class="wy-nav-content">
|
||
<div class="rst-content">
|
||
<a class="btn btn-neutral float-right" href="https://github.com/codeigniter4/CodeIgniter4/edit/develop/user_guide_src/source/database/configuration.rst">Edit this page</a>
|
||
|
||
<div role="navigation" aria-label="Page navigation">
|
||
<ul class="wy-breadcrumbs">
|
||
<li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
|
||
<li class="breadcrumb-item"><a href="index.html">Working with Databases</a></li>
|
||
<li class="breadcrumb-item active">Database Configuration</li>
|
||
<li class="wy-breadcrumbs-aside">
|
||
</li>
|
||
</ul>
|
||
<hr/>
|
||
</div>
|
||
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
||
<div itemprop="articleBody">
|
||
|
||
<section id="database-configuration">
|
||
<h1>Database Configuration<a class="headerlink" href="#database-configuration" title="Permalink to this heading"></a></h1>
|
||
<nav class="contents local" id="contents">
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="#config-file" id="id1">Config File</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#setting-default-database" id="id2">Setting Default Database</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#dsn" id="id3">DSN</a></p></li>
|
||
<li><p><a class="reference internal" href="#failovers" id="id4">Failovers</a></p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p><a class="reference internal" href="#setting-multiple-databases" id="id5">Setting Multiple Databases</a></p></li>
|
||
<li><p><a class="reference internal" href="#changing-databases-automatically" id="id6">Changing Databases Automatically</a></p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p><a class="reference internal" href="#configuring-with-env-file" id="id7">Configuring with .env File</a></p></li>
|
||
<li><p><a class="reference internal" href="#description-of-values" id="id8">Description of Values</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#mysqli" id="id9">MySQLi</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#hostname" id="id10">hostname</a></p></li>
|
||
<li><p><a class="reference internal" href="#encrypt" id="id11">encrypt</a></p></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</nav>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>See <a class="reference internal" href="../intro/requirements.html#requirements-supported-databases"><span class="std std-ref">Supported Databases</span></a> for currently supported database drivers.</p>
|
||
</div>
|
||
<section id="config-file">
|
||
<h2><a class="toc-backref" href="#id1" role="doc-backlink">Config File</a><a class="headerlink" href="#config-file" title="Permalink to this heading"></a></h2>
|
||
<p>CodeIgniter has a config file that lets you store your database
|
||
connection values (username, password, database name, etc.). The config
|
||
file is located at <strong>app/Config/Database.php</strong>. You can also set
|
||
database connection values in the <strong>.env</strong> file. See below for more details.</p>
|
||
<section id="setting-default-database">
|
||
<h3><a class="toc-backref" href="#id2" role="doc-backlink">Setting Default Database</a><a class="headerlink" href="#setting-default-database" title="Permalink to this heading"></a></h3>
|
||
<p>The config settings are stored in a class property that is an array with this
|
||
prototype:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Database\Config</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Database</span> <span class="k">extends</span> <span class="nx">Config</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
|
||
<span class="k">public</span> <span class="k">array</span> <span class="nv">$default</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'DSN'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'hostname'</span> <span class="o">=></span> <span class="s1">'localhost'</span><span class="p">,</span>
|
||
<span class="s1">'username'</span> <span class="o">=></span> <span class="s1">'root'</span><span class="p">,</span>
|
||
<span class="s1">'password'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'database'</span> <span class="o">=></span> <span class="s1">'database_name'</span><span class="p">,</span>
|
||
<span class="s1">'DBDriver'</span> <span class="o">=></span> <span class="s1">'MySQLi'</span><span class="p">,</span>
|
||
<span class="s1">'DBPrefix'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'pConnect'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="s1">'DBDebug'</span> <span class="o">=></span> <span class="k">true</span><span class="p">,</span>
|
||
<span class="s1">'charset'</span> <span class="o">=></span> <span class="s1">'utf8mb4'</span><span class="p">,</span>
|
||
<span class="s1">'DBCollat'</span> <span class="o">=></span> <span class="s1">'utf8mb4_general_ci'</span><span class="p">,</span>
|
||
<span class="s1">'swapPre'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'encrypt'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="s1">'compress'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="s1">'strictOn'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="s1">'failover'</span> <span class="o">=></span> <span class="p">[],</span>
|
||
<span class="s1">'port'</span> <span class="o">=></span> <span class="mi">3306</span><span class="p">,</span>
|
||
<span class="p">];</span>
|
||
|
||
<span class="c1">// ...</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The name of the class property is the connection name, and can be used
|
||
while connecting to specify a group name.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>The default location of the SQLite3 database is the <strong>writable</strong> folder.
|
||
If you want to change the location, you must set the full path to the new folder (e.g., ‘database’ => WRITEPATH . ‘db/database_name.db’).</p>
|
||
</div>
|
||
<section id="dsn">
|
||
<h4><a class="toc-backref" href="#id3" role="doc-backlink">DSN</a><a class="headerlink" href="#dsn" title="Permalink to this heading"></a></h4>
|
||
<p>Some database drivers (such as Postgre, OCI8) requires a full DSN (Data Source Name) string to connect.
|
||
But if you do not specify a DSN string for a driver that requires it, CodeIgniter
|
||
will try to build it with the rest of the provided settings.</p>
|
||
<p>If you specify a DSN, you should use the <code class="docutils literal notranslate"><span class="pre">'DSN'</span></code> configuration setting, as if
|
||
you’re using the driver’s underlying native PHP extension, like this:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span> <span class="c1">// OCI8</span>
|
||
<span class="k">public</span> <span class="k">array</span> <span class="nv">$default</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'DSN'</span> <span class="o">=></span> <span class="s1">'//localhost/XE'</span><span class="p">,</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
<section id="dsn-in-universal-manner">
|
||
<h5>DSN in Universal Manner<a class="headerlink" href="#dsn-in-universal-manner" title="Permalink to this heading"></a></h5>
|
||
<p>You can also set a DSN in universal manner (URL like). In that case DSNs must have this prototype:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span> <span class="k">public</span> <span class="k">array</span> <span class="nv">$default</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'DSN'</span> <span class="o">=></span> <span class="s1">'DBDriver://username:password@hostname:port/database'</span><span class="p">,</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>To override default config values when connecting with a universal version of the DSN string,
|
||
add the config variables as a query string:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span> <span class="c1">// MySQLi</span>
|
||
<span class="k">public</span> <span class="k">array</span> <span class="nv">$default</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'DSN'</span> <span class="o">=></span> <span class="s1">'MySQLi://username:password@hostname:3306/database?charset=utf8mb4&DBCollat=utf8mb4_general_ci'</span><span class="p">,</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span> <span class="c1">// Postgre</span>
|
||
<span class="k">public</span> <span class="k">array</span> <span class="nv">$default</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'DSN'</span> <span class="o">=></span> <span class="s1">'Postgre://username:password@hostname:5432/database?charset=utf8&connect_timeout=5&sslmode=require'</span><span class="p">,</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>If you provide a DSN string and it is missing some valid settings (e.g., the
|
||
database character set), which are present in the rest of the configuration
|
||
fields, CodeIgniter will append them.</p>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="failovers">
|
||
<h4><a class="toc-backref" href="#id4" role="doc-backlink">Failovers</a><a class="headerlink" href="#failovers" title="Permalink to this heading"></a></h4>
|
||
<p>You can also specify failovers for the situation when the main connection cannot connect for some reason.
|
||
These failovers can be specified by setting the failover for a connection like this:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Database\Config</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Database</span> <span class="k">extends</span> <span class="nx">Config</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
|
||
<span class="k">public</span> <span class="k">array</span> <span class="nv">$default</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="s1">'failover'</span> <span class="o">=></span> <span class="p">[</span>
|
||
<span class="p">[</span>
|
||
<span class="s1">'hostname'</span> <span class="o">=></span> <span class="s1">'localhost1'</span><span class="p">,</span>
|
||
<span class="s1">'username'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'password'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'database'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'DBDriver'</span> <span class="o">=></span> <span class="s1">'MySQLi'</span><span class="p">,</span>
|
||
<span class="s1">'DBPrefix'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'pConnect'</span> <span class="o">=></span> <span class="k">true</span><span class="p">,</span>
|
||
<span class="s1">'DBDebug'</span> <span class="o">=></span> <span class="k">true</span><span class="p">,</span>
|
||
<span class="s1">'charset'</span> <span class="o">=></span> <span class="s1">'utf8mb4'</span><span class="p">,</span>
|
||
<span class="s1">'DBCollat'</span> <span class="o">=></span> <span class="s1">'utf8mb4_general_ci'</span><span class="p">,</span>
|
||
<span class="s1">'swapPre'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'encrypt'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="s1">'compress'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="s1">'strictOn'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="p">],</span>
|
||
<span class="p">[</span>
|
||
<span class="s1">'hostname'</span> <span class="o">=></span> <span class="s1">'localhost2'</span><span class="p">,</span>
|
||
<span class="s1">'username'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'password'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'database'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'DBDriver'</span> <span class="o">=></span> <span class="s1">'MySQLi'</span><span class="p">,</span>
|
||
<span class="s1">'DBPrefix'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'pConnect'</span> <span class="o">=></span> <span class="k">true</span><span class="p">,</span>
|
||
<span class="s1">'DBDebug'</span> <span class="o">=></span> <span class="k">true</span><span class="p">,</span>
|
||
<span class="s1">'charset'</span> <span class="o">=></span> <span class="s1">'utf8mb4'</span><span class="p">,</span>
|
||
<span class="s1">'DBCollat'</span> <span class="o">=></span> <span class="s1">'utf8mb4_general_ci'</span><span class="p">,</span>
|
||
<span class="s1">'swapPre'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'encrypt'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="s1">'compress'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="s1">'strictOn'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="p">],</span>
|
||
<span class="p">],</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="p">];</span>
|
||
|
||
<span class="c1">// ...</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You can specify as many failovers as you like.</p>
|
||
</section>
|
||
</section>
|
||
<section id="setting-multiple-databases">
|
||
<h3><a class="toc-backref" href="#id5" role="doc-backlink">Setting Multiple Databases</a><a class="headerlink" href="#setting-multiple-databases" title="Permalink to this heading"></a></h3>
|
||
<p>You may optionally store multiple sets of connection
|
||
values. If, for example, you run multiple environments (development,
|
||
production, test, etc.) under a single installation, you can set up a
|
||
connection group for each, then switch between groups as needed. For
|
||
example, to set up a “test” environment you would do this:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Database\Config</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Database</span> <span class="k">extends</span> <span class="nx">Config</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
|
||
<span class="k">public</span> <span class="k">array</span> <span class="nv">$test</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'DSN'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'hostname'</span> <span class="o">=></span> <span class="s1">'localhost'</span><span class="p">,</span>
|
||
<span class="s1">'username'</span> <span class="o">=></span> <span class="s1">'root'</span><span class="p">,</span>
|
||
<span class="s1">'password'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'database'</span> <span class="o">=></span> <span class="s1">'database_name'</span><span class="p">,</span>
|
||
<span class="s1">'DBDriver'</span> <span class="o">=></span> <span class="s1">'MySQLi'</span><span class="p">,</span>
|
||
<span class="s1">'DBPrefix'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'pConnect'</span> <span class="o">=></span> <span class="k">true</span><span class="p">,</span>
|
||
<span class="s1">'DBDebug'</span> <span class="o">=></span> <span class="k">true</span><span class="p">,</span>
|
||
<span class="s1">'charset'</span> <span class="o">=></span> <span class="s1">'utf8mb4'</span><span class="p">,</span>
|
||
<span class="s1">'DBCollat'</span> <span class="o">=></span> <span class="s1">'utf8mb4_general_ci'</span><span class="p">,</span>
|
||
<span class="s1">'swapPre'</span> <span class="o">=></span> <span class="s1">''</span><span class="p">,</span>
|
||
<span class="s1">'compress'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="s1">'encrypt'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="s1">'strictOn'</span> <span class="o">=></span> <span class="k">false</span><span class="p">,</span>
|
||
<span class="s1">'failover'</span> <span class="o">=></span> <span class="p">[],</span>
|
||
<span class="p">];</span>
|
||
|
||
<span class="c1">// ...</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Then, to globally tell the system to use that group you would set this
|
||
variable located in the config file:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Database\Config</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Database</span> <span class="k">extends</span> <span class="nx">Config</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
|
||
<span class="k">public</span> <span class="nx">string</span> <span class="nv">$defaultGroup</span> <span class="o">=</span> <span class="s1">'test'</span><span class="p">;</span>
|
||
|
||
<span class="c1">// ...</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>The name <code class="docutils literal notranslate"><span class="pre">test</span></code> is arbitrary. It can be anything you want. By
|
||
default we’ve used the word <code class="docutils literal notranslate"><span class="pre">default</span></code> for the primary connection,
|
||
but it too can be renamed to something more relevant to your project.</p>
|
||
</div>
|
||
</section>
|
||
<section id="changing-databases-automatically">
|
||
<h3><a class="toc-backref" href="#id6" role="doc-backlink">Changing Databases Automatically</a><a class="headerlink" href="#changing-databases-automatically" title="Permalink to this heading"></a></h3>
|
||
<p>You could modify the config file to detect the environment and automatically
|
||
update the <code class="docutils literal notranslate"><span class="pre">defaultGroup</span></code> value to the correct one by adding the required logic
|
||
within the class’ constructor:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Database\Config</span><span class="p">;</span>
|
||
|
||
<span class="sd">/**</span>
|
||
<span class="sd"> * Database Configuration</span>
|
||
<span class="sd"> */</span>
|
||
<span class="k">class</span> <span class="nc">Database</span> <span class="k">extends</span> <span class="nx">Config</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="k">public</span> <span class="nv">$development</span> <span class="o">=</span> <span class="p">[</span><span class="cm">/* ... */</span><span class="p">];</span>
|
||
<span class="k">public</span> <span class="nv">$test</span> <span class="o">=</span> <span class="p">[</span><span class="cm">/* ... */</span><span class="p">];</span>
|
||
<span class="k">public</span> <span class="nv">$production</span> <span class="o">=</span> <span class="p">[</span><span class="cm">/* ... */</span><span class="p">];</span>
|
||
|
||
<span class="k">public</span> <span class="k">function</span> <span class="fm">__construct</span><span class="p">()</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
|
||
<span class="nv">$this</span><span class="o">-></span><span class="na">defaultGroup</span> <span class="o">=</span> <span class="nx">ENVIRONMENT</span><span class="p">;</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="configuring-with-env-file">
|
||
<span id="database-config-with-env-file"></span><h2><a class="toc-backref" href="#id7" role="doc-backlink">Configuring with .env File</a><a class="headerlink" href="#configuring-with-env-file" title="Permalink to this heading"></a></h2>
|
||
<p>You can also save your configuration values within a <strong>.env</strong> file with the current server’s
|
||
database settings. You only need to enter the values that change from what is in the
|
||
default group’s configuration settings. The values should follow this format, where
|
||
<code class="docutils literal notranslate"><span class="pre">default</span></code> is the group name:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">database</span><span class="o">.</span><span class="k">default</span><span class="o">.</span><span class="nx">username</span> <span class="o">=</span> <span class="s1">'root'</span><span class="p">;</span>
|
||
<span class="nx">database</span><span class="o">.</span><span class="k">default</span><span class="o">.</span><span class="nx">password</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span>
|
||
<span class="nx">database</span><span class="o">.</span><span class="k">default</span><span class="o">.</span><span class="nx">database</span> <span class="o">=</span> <span class="s1">'ci4'</span><span class="p">;</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>But you cannot add a new property by setting environment variables, nor change a
|
||
scalar value to an array. See <a class="reference internal" href="../general/configuration.html#env-var-replacements-for-data"><span class="std std-ref">Environment Variables as Replacements for Data</span></a> for details.</p>
|
||
<p>So if you want to use SSL with MySQL, you need a hack. For example, set the array
|
||
values as a JSON string in your <strong>.env</strong> file:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">database</span><span class="o">.</span><span class="k">default</span><span class="o">.</span><span class="nx">encrypt</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"ssl_verify"</span><span class="o">:</span><span class="k">true</span><span class="p">,</span><span class="s2">"ssl_ca"</span><span class="o">:</span><span class="s2">"/var/www/html/BaltimoreCyberTrustRoot.crt.pem"</span><span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>and decode it in the constructor in the Config class:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Database\Config</span><span class="p">;</span>
|
||
|
||
<span class="sd">/**</span>
|
||
<span class="sd"> * Database Configuration</span>
|
||
<span class="sd"> */</span>
|
||
<span class="k">class</span> <span class="nc">Database</span> <span class="k">extends</span> <span class="nx">Config</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
|
||
<span class="k">public</span> <span class="k">function</span> <span class="fm">__construct</span><span class="p">()</span>
|
||
<span class="p">{</span>
|
||
<span class="c1">// ...</span>
|
||
|
||
<span class="nv">$array</span> <span class="o">=</span> <span class="nb">json_decode</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">default</span><span class="p">[</span><span class="s1">'encrypt'</span><span class="p">],</span> <span class="k">true</span><span class="p">);</span>
|
||
<span class="k">if</span> <span class="p">(</span><span class="nb">is_array</span><span class="p">(</span><span class="nv">$array</span><span class="p">))</span> <span class="p">{</span>
|
||
<span class="nv">$this</span><span class="o">-></span><span class="na">default</span><span class="p">[</span><span class="s1">'encrypt'</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$array</span><span class="p">;</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="description-of-values">
|
||
<span id="database-config-explanation-of-values"></span><h2><a class="toc-backref" href="#id8" role="doc-backlink">Description of Values</a><a class="headerlink" href="#description-of-values" title="Permalink to this heading"></a></h2>
|
||
<table class="docutils align-default">
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>Config Name</p></th>
|
||
<th class="head"><p>Description</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p><strong>DSN</strong></p></td>
|
||
<td><p>The DSN connect string (an all-in-one configuration sequence).</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>hostname</strong></p></td>
|
||
<td><p>The hostname of your database server. Often this is ‘localhost’.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>username</strong></p></td>
|
||
<td><p>The username used to connect to the database. (<code class="docutils literal notranslate"><span class="pre">SQLite3</span></code> does not use this.)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>password</strong></p></td>
|
||
<td><p>The password used to connect to the database. (<code class="docutils literal notranslate"><span class="pre">SQLite3</span></code> does not use this.)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>database</strong></p></td>
|
||
<td><p>The name of the database you want to connect to.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>CodeIgniter doesn’t support dots (<code class="docutils literal notranslate"><span class="pre">.</span></code>) in the table and column names.
|
||
Since v4.5.0, database names with dots are supported.</p>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>DBDriver</strong></p></td>
|
||
<td><p>The database driver name. The case must match the driver name.
|
||
You can set a fully qualified classname to use your custom driver.
|
||
Supported drivers: <code class="docutils literal notranslate"><span class="pre">MySQLi</span></code>, <code class="docutils literal notranslate"><span class="pre">Postgre</span></code>, <code class="docutils literal notranslate"><span class="pre">SQLite3</span></code>, <code class="docutils literal notranslate"><span class="pre">SQLSRV</span></code>, and <code class="docutils literal notranslate"><span class="pre">OCI8</span></code>.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>DBPrefix</strong></p></td>
|
||
<td><p>An optional table prefix which will be added to the table name when running
|
||
<a class="reference internal" href="query_builder.html"><span class="doc">Query Builder</span></a> queries. This permits multiple CodeIgniter
|
||
installations to share one database.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>pConnect</strong></p></td>
|
||
<td><p>true/false (boolean) - Whether to use a persistent connection.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>DBDebug</strong></p></td>
|
||
<td><p>true/false (boolean) - Whether to throw exceptions when database errors occur.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>charset</strong></p></td>
|
||
<td><p>The character set used in communicating with the database.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>DBCollat</strong></p></td>
|
||
<td><p>(<code class="docutils literal notranslate"><span class="pre">MySQLi</span></code> only) The character collation used in communicating with the database.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>swapPre</strong></p></td>
|
||
<td><p>A default table prefix that should be swapped with <code class="docutils literal notranslate"><span class="pre">DBPrefix</span></code>. This is useful for distributed
|
||
applications where you might run manually written queries, and need the prefix to still be
|
||
customizable by the end user.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>schema</strong></p></td>
|
||
<td><p>(<code class="docutils literal notranslate"><span class="pre">Postgre</span></code> and <code class="docutils literal notranslate"><span class="pre">SQLSRV</span></code> only) The database schema, default value varies by driver.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>encrypt</strong></p></td>
|
||
<td><p>(<code class="docutils literal notranslate"><span class="pre">MySQLi</span></code> and <code class="docutils literal notranslate"><span class="pre">SQLSRV</span></code> only) Whether to use an encrypted connection.
|
||
See <a class="reference internal" href="#mysqli-encrypt"><span class="std std-ref">MySQLi encrypt</span></a> for <code class="docutils literal notranslate"><span class="pre">MySQLi</span></code> settings.
|
||
<code class="docutils literal notranslate"><span class="pre">SQLSRV</span></code> driver accepts true/false.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>compress</strong></p></td>
|
||
<td><p>(<code class="docutils literal notranslate"><span class="pre">MySQLi</span></code> only) Whether to use client compression.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>strictOn</strong></p></td>
|
||
<td><p>(<code class="docutils literal notranslate"><span class="pre">MySQLi</span></code> only) true/false (boolean) - Whether to force “Strict Mode” connections, good for ensuring
|
||
strict SQL while developing an application.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>port</strong></p></td>
|
||
<td><p>The database port number - Empty string <code class="docutils literal notranslate"><span class="pre">''</span></code> for default port (or dynamic port with <code class="docutils literal notranslate"><span class="pre">SQLSRV</span></code>).</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>foreignKeys</strong></p></td>
|
||
<td><p>(<code class="docutils literal notranslate"><span class="pre">SQLite3</span></code> only) true/false (boolean) - Whether to enable Foreign Key constraint.</p>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">Important</p>
|
||
<p>SQLite3 Foreign Key constraint is disabled by default.
|
||
See <a class="reference external" href="https://www.sqlite.org/pragma.html#pragma_foreign_keys">SQLite documentation</a>.
|
||
To enforce Foreign Key constraint, set this config item to true.</p>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>busyTimeout</strong></p></td>
|
||
<td><p>(<code class="docutils literal notranslate"><span class="pre">SQLite3</span></code> only) milliseconds (int) - Sleeps for a specified amount of time when a table is locked.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>synchronous</strong></p></td>
|
||
<td><p>(<code class="docutils literal notranslate"><span class="pre">SQLite3</span></code> only) flag (int) - How strict SQLite will be at flushing to disk during transactions.
|
||
Use <cite>null</cite> to stay with the default setting. This can be used since v4.6.0.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>numberNative</strong></p></td>
|
||
<td><p>(<code class="docutils literal notranslate"><span class="pre">MySQLi</span></code> only) true/false (boolean) - Whether to enable MYSQLI_OPT_INT_AND_FLOAT_NATIVE.</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><strong>foundRows</strong></p></td>
|
||
<td><p>(<code class="docutils literal notranslate"><span class="pre">MySQLi</span></code> only) true/false (boolean) - Whether to enable MYSQLI_CLIENT_FOUND_ROWS.</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><strong>dateFormat</strong></p></td>
|
||
<td><p>The default date/time formats as PHP’s <a class="reference external" href="https://www.php.net/manual/en/datetime.format.php">DateTime format</a>.
|
||
* <code class="docutils literal notranslate"><span class="pre">date</span></code> - date format
|
||
* <code class="docutils literal notranslate"><span class="pre">datetime</span></code> - date and time format
|
||
* <code class="docutils literal notranslate"><span class="pre">datetime-ms</span></code> - date and time with millisecond format
|
||
* <code class="docutils literal notranslate"><span class="pre">datetime-us</span></code> - date and time with microsecond format
|
||
* <code class="docutils literal notranslate"><span class="pre">time</span></code> - time format
|
||
This can be used since v4.5.0, and you can get the value, e.g., <code class="docutils literal notranslate"><span class="pre">$db->dateFormat['datetime']</span></code>.
|
||
Currently, the database drivers do not use these values directly,
|
||
but <a class="reference internal" href="../models/model.html#model-saving-dates"><span class="std std-ref">Model</span></a> uses them.</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Depending on what database driver you are using (<code class="docutils literal notranslate"><span class="pre">MySQLi</span></code>, <code class="docutils literal notranslate"><span class="pre">Postgre</span></code>,
|
||
etc.) not all values will be needed. For example, when using <code class="docutils literal notranslate"><span class="pre">SQLite3</span></code> you
|
||
will not need to supply a username or password, and the database name
|
||
will be the path to your database file.</p>
|
||
</div>
|
||
<section id="mysqli">
|
||
<h3><a class="toc-backref" href="#id9" role="doc-backlink">MySQLi</a><a class="headerlink" href="#mysqli" title="Permalink to this heading"></a></h3>
|
||
<section id="hostname">
|
||
<h4><a class="toc-backref" href="#id10" role="doc-backlink">hostname</a><a class="headerlink" href="#hostname" title="Permalink to this heading"></a></h4>
|
||
<section id="configuring-a-socket-connection">
|
||
<h5>Configuring a Socket Connection<a class="headerlink" href="#configuring-a-socket-connection" title="Permalink to this heading"></a></h5>
|
||
<p>To connect to a MySQL server over a filesystem socket, the path to the socket should be specified in
|
||
the <code class="docutils literal notranslate"><span class="pre">'hostname'</span></code> setting. CodeIgniter’s MySQLi driver will notice this and configure the
|
||
connection properly.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span> <span class="c1">// MySQLi over a socket</span>
|
||
<span class="k">public</span> <span class="k">array</span> <span class="nv">$default</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="s1">'hostname'</span> <span class="o">=></span> <span class="s1">'/cloudsql/toolbox-tests:europe-north1:toolbox-db'</span><span class="p">,</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="s1">'DBDriver'</span> <span class="o">=></span> <span class="s1">'MySQLi'</span><span class="p">,</span>
|
||
<span class="c1">// ...</span>
|
||
<span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="encrypt">
|
||
<span id="mysqli-encrypt"></span><h4><a class="toc-backref" href="#id11" role="doc-backlink">encrypt</a><a class="headerlink" href="#encrypt" title="Permalink to this heading"></a></h4>
|
||
<p>MySQLi driver accepts an array with the following options:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">ssl_key</span></code> - Path to the private key file</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">ssl_cert</span></code> - Path to the public key certificate file</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">ssl_ca</span></code> - Path to the certificate authority file</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">ssl_capath</span></code> - Path to a directory containing trusted CA certificates in PEM format</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">ssl_cipher</span></code> - List of <em>allowed</em> ciphers to be used for the encryption, separated by colons (<code class="docutils literal notranslate"><span class="pre">:</span></code>)</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">ssl_verify</span></code> - true/false (boolean) - Whether to verify the server certificate or not</p></li>
|
||
</ul>
|
||
</section>
|
||
</section>
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
|
||
<a href="examples.html" class="btn btn-neutral float-left" title="Quick Start: Usage Examples" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
|
||
<a href="connecting.html" class="btn btn-neutral float-right" title="Connecting to your Database" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
|
||
</div>
|
||
|
||
<hr/>
|
||
|
||
<div role="contentinfo">
|
||
<p>© Copyright 2019-2025 CodeIgniter Foundation.
|
||
<span class="lastupdated">Last updated on Feb 07, 2025.
|
||
</span></p>
|
||
</div>
|
||
|
||
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
|
||
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
|
||
provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
||
|
||
|
||
</footer>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
<script>
|
||
jQuery(function () {
|
||
SphinxRtdTheme.Navigation.enable(false);
|
||
});
|
||
</script>
|
||
|
||
</body>
|
||
</html> |