3 final class PhabricatorDatabaseSetupCheck
extends PhabricatorSetupCheck
{
5 public function getDefaultGroup() {
6 return self
::GROUP_IMPORTANT
;
9 public function getExecutionOrder() {
10 // This must run after basic PHP checks, but before most other checks.
14 protected function executeChecks() {
15 $host = PhabricatorEnv
::getEnvConfig('mysql.host');
17 if (preg_match('/^([^:]+):(\d+)$/', $host, $matches)) {
21 $this->newIssue('storage.mysql.hostport')
22 ->setName(pht('Deprecated mysql.host Format'))
25 'Move port information from `%s` to `%s` in your config.',
30 'Your `%s` configuration contains a port number, but this usage '.
31 'is deprecated. Instead, put the port number in `%s`.',
34 ->addPhabricatorConfig('mysql.host')
35 ->addPhabricatorConfig('mysql.port')
38 '<tt>phabricator/ $</tt> ./bin/config set mysql.host %s',
42 '<tt>phabricator/ $</tt> ./bin/config set mysql.port %s',
46 $refs = PhabricatorDatabaseRef
::queryAll();
47 $refs = mpull($refs, null, 'getRefKey');
49 // Test if we can connect to each database first. If we can not connect
50 // to a particular database, we only raise a warning: this allows new web
51 // nodes to start during a disaster, when some databases may be correctly
52 // configured but not reachable.
54 $connect_map = array();
55 $any_connection = false;
56 foreach ($refs as $ref_key => $ref) {
57 $conn_raw = $ref->newManagementConnection();
60 queryfx($conn_raw, 'SELECT 1');
61 $database_exception = null;
62 $any_connection = true;
63 } catch (AphrontInvalidCredentialsQueryException
$ex) {
64 $database_exception = $ex;
65 } catch (AphrontConnectionQueryException
$ex) {
66 $database_exception = $ex;
69 if ($database_exception) {
70 $connect_map[$ref_key] = $database_exception;
71 unset($refs[$ref_key]);
76 // This is only a fatal error if we could not connect to anything. If
77 // possible, we still want to start if some database hosts can not be
79 $is_fatal = !$any_connection;
81 foreach ($connect_map as $ref_key => $database_exception) {
82 $issue = PhabricatorSetupIssue
::newDatabaseConnectionIssue(
85 $this->addIssue($issue);
89 foreach ($refs as $ref_key => $ref) {
90 if ($this->executeRefChecks($ref)) {
96 private function executeRefChecks(PhabricatorDatabaseRef
$ref) {
97 $conn_raw = $ref->newManagementConnection();
98 $ref_key = $ref->getRefKey();
100 $engines = queryfx_all($conn_raw, 'SHOW ENGINES');
101 $engines = ipull($engines, 'Support', 'Engine');
103 $innodb = idx($engines, 'InnoDB');
104 if ($innodb != 'YES' && $innodb != 'DEFAULT') {
106 'The "InnoDB" engine is not available in MySQL (on host "%s"). '.
107 'Enable InnoDB in your MySQL configuration.'.
109 '(If you aleady created tables, MySQL incorrectly used some other '.
110 'engine to create them. You need to convert them or drop and '.
111 'reinitialize them.)',
114 $this->newIssue('mysql.innodb')
115 ->setName(pht('MySQL InnoDB Engine Not Available'))
116 ->setMessage($message)
122 $namespace = PhabricatorEnv
::getEnvConfig('storage.default-namespace');
124 $databases = queryfx_all($conn_raw, 'SHOW DATABASES');
125 $databases = ipull($databases, 'Database', 'Database');
127 if (empty($databases[$namespace.'_meta_data'])) {
129 'Run the storage upgrade script to setup databases (host "%s" has '.
130 'not been initialized).',
133 $this->newIssue('storage.upgrade')
134 ->setName(pht('Setup MySQL Schema'))
135 ->setMessage($message)
137 ->addCommand(hsprintf('<tt>phabricator/ $</tt> ./bin/storage upgrade'));
142 $conn_meta = $ref->newApplicationConnection(
143 $namespace.'_meta_data');
145 $applied = queryfx_all($conn_meta, 'SELECT patch FROM patch_status');
146 $applied = ipull($applied, 'patch', 'patch');
148 $all = PhabricatorSQLPatchList
::buildAllPatches();
149 $diff = array_diff_key($all, $applied);
153 'Run the storage upgrade script to upgrade databases (host "%s" is '.
154 'out of date). Missing patches: %s.',
156 implode(', ', array_keys($diff)));
158 $this->newIssue('storage.patch')
159 ->setName(pht('Upgrade MySQL Schema'))
161 ->setMessage($message)
163 hsprintf('<tt>phabricator/ $</tt> ./bin/storage upgrade'));
168 // NOTE: It's possible that replication is broken but we have not been
169 // granted permission to "SHOW SLAVE STATUS" so we can't figure it out.
170 // We allow this kind of configuration and survive these checks, trusting
171 // that operations knows what they're doing. This issue is shown on the
172 // "Database Servers" console.
174 switch ($ref->getReplicaStatus()) {
175 case PhabricatorDatabaseRef
::REPLICATION_MASTER_REPLICA
:
177 'Database host "%s" is configured as a master, but is replicating '.
178 'another host. This is dangerous and can mangle or destroy data. '.
179 'Only replicas should be replicating. Stop replication on the '.
180 'host or reconfigure Phabricator.',
183 $this->newIssue('db.master.replicating')
184 ->setName(pht('Replicating Master'))
186 ->setMessage($message);
189 case PhabricatorDatabaseRef
::REPLICATION_REPLICA_NONE
:
190 case PhabricatorDatabaseRef
::REPLICATION_NOT_REPLICATING
:
191 if (!$ref->getIsMaster()) {
193 'Database replica "%s" is listed as a replica, but is not '.
194 'currently replicating. You are vulnerable to data loss if '.
198 // This isn't a fatal because it can normally only put data at risk,
199 // not actually do anything destructive or unrecoverable.
201 $this->newIssue('db.replica.not-replicating')
202 ->setName(pht('Nonreplicating Replica'))
203 ->setMessage($message);
208 // If we have more than one master, we require that the cluster database
209 // configuration written to each database node is exactly the same as the
210 // one we are running with.
211 $masters = PhabricatorDatabaseRef
::getAllMasterDatabaseRefs();
212 if (count($masters) > 1) {
213 $state_actual = queryfx_one(
215 'SELECT stateValue FROM %T WHERE stateKey = %s',
216 PhabricatorStorageManagementAPI
::TABLE_HOSTSTATE
,
217 'cluster.databases');
219 $state_actual = $state_actual['stateValue'];
222 $state_expect = $ref->getPartitionStateForCommit();
224 if ($state_expect !== $state_actual) {
226 'Database host "%s" has a configured cluster state which disagrees '.
227 'with the state on this host ("%s"). Run `bin/storage partition` '.
228 'to commit local state to the cluster. This host may have started '.
229 'with an out-of-date configuration.',
233 $this->newIssue('db.state.desync')
234 ->setName(pht('Cluster Configuration Out of Sync'))
235 ->setMessage($message)