You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

database-preparation.en-us.md 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. ---
  2. date: "2020-01-16"
  3. title: "Database Preparation"
  4. slug: "database-prep"
  5. weight: 10
  6. toc: true
  7. draft: false
  8. menu:
  9. sidebar:
  10. parent: "installation"
  11. name: "Database preparation"
  12. weight: 20
  13. identifier: "database-prep"
  14. ---
  15. You need a database to use Gitea. Gitea supports PostgreSQL, MySQL, SQLite, and MSSQL. This page will guide into preparing database. Only PostgreSQL and MySQL will be covered here since those database engines are widely-used in production.
  16. Database instance can be on same machine as Gitea (local database setup), or on different machine (remote database).
  17. Note: All steps below requires that the database engine of your choice is installed on your system. For remote database setup, install the server part on database instance and client part on your Gitea server. In addition, make sure you use same engine version for both server and client for some engine features to work. For security reason, protect `root` (MySQL) or `postgres` (PostgreSQL) database superuser with secure password. The steps assumes that you run Linux for both database and Gitea servers.
  18. ## MySQL
  19. 1. On database instance, login to database console as root:
  20. ```
  21. mysql -u root -p
  22. ```
  23. Enter the password as prompted.
  24. 2. Create database user which will be used by Gitea, authenticated by password. This example uses `'gitea'` as password. Please use a secure password for your instance.
  25. For local database:
  26. ```sql
  27. SET old_passwords=0;
  28. CREATE USER 'gitea' IDENTIFIED BY 'gitea';
  29. ```
  30. For remote database:
  31. ```sql
  32. SET old_passwords=0;
  33. CREATE USER 'gitea'@'192.0.2.10' IDENTIFIED BY 'gitea';
  34. ```
  35. where `192.0.2.10` is the IP address of your Gitea instance.
  36. Replace username and password above as appropriate.
  37. 3. Create database with UTF-8 charset and collation. Make sure to use `utf8mb4` charset instead of `utf8` as the former supports all Unicode characters (including emojis) beyond *Basic Multilingual Plane*. Also, collation chosen depending on your expected content. When in doubt, use either `unicode_ci` or `general_ci`.
  38. ```sql
  39. CREATE DATABASE giteadb CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';
  40. ```
  41. Replace database name as appropriate.
  42. 4. Grant all privileges on the database to database user created above.
  43. For local database:
  44. ```sql
  45. GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea';
  46. FLUSH PRIVILEGES;
  47. ```
  48. For remote database:
  49. ```sql
  50. GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea'@'192.0.2.10';
  51. FLUSH PRIVILEGES;
  52. ```
  53. 5. Quit from database console by `exit`.
  54. 6. On your Gitea server, test connection to the database:
  55. ```
  56. mysql -u gitea -h 203.0.113.3 -p giteadb
  57. ```
  58. where `gitea` is database username, `giteadb` is database name, and `203.0.113.3` is IP address of database instance. Omit `-h` option for local database.
  59. You should be connected to the database.
  60. ## PostgreSQL
  61. 1. PostgreSQL uses `md5` challenge-response encryption scheme for password authentication by default. Nowadays this scheme is not considered secure anymore. Use SCRAM-SHA-256 scheme instead by editing the `postgresql.conf` configuration file on the database server to:
  62. ```ini
  63. password_encryption = scram-sha-256
  64. ```
  65. Restart PostgreSQL to apply the setting.
  66. 2. On the database server, login to the database console as superuser:
  67. ```
  68. su -c "psql" - postgres
  69. ```
  70. 3. Create database user (role in PostgreSQL terms) with login privilege and password. Please use a secure, strong password instead of `'gitea'` below:
  71. ```sql
  72. CREATE ROLE gitea WITH LOGIN PASSWORD 'gitea';
  73. ```
  74. Replace username and password as appropriate.
  75. 4. Create database with UTF-8 charset and owned by the database user created earlier. Any `libc` collations can be specified with `LC_COLLATE` and `LC_CTYPE` parameter, depending on expected content:
  76. ```sql
  77. CREATE DATABASE giteadb WITH OWNER gitea TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';
  78. ```
  79. Replace database name as appropriate.
  80. 5. Allow the database user to access the database created above by adding the following authentication rule to `pg_hba.conf`.
  81. For local database:
  82. ```ini
  83. local giteadb gitea scram-sha-256
  84. ```
  85. For remote database:
  86. ```ini
  87. host giteadb gitea 192.0.2.10/32 scram-sha-256
  88. ```
  89. Replace database name, user, and IP address of Gitea instance with your own.
  90. Note: rules on `pg_hba.conf` are evaluated sequentially, that is the first matching rule will be used for authentication. Your PostgreSQL installation may come with generic authentication rules that match all users and databases. You may need to place the rules presented here above such generic rules if it is the case.
  91. Restart PostgreSQL to apply new authentication rules.
  92. 6. On your Gitea server, test connection to the database.
  93. For local database:
  94. ```
  95. psql -U gitea -d giteadb
  96. ```
  97. For remote database:
  98. ```
  99. psql "postgres://gitea@203.0.113.3/giteadb"
  100. ```
  101. where `gitea` is database user, `giteadb` is database name, and `203.0.113.3` is IP address of your database instance.
  102. You should be prompted to enter password for the database user, and connected to the database.
  103. ## Database Connection over TLS
  104. If the communication between Gitea and your database instance is performed through a private network, or if Gitea and the database are running on the same server, this section can be omitted since the security between Gitea and the database instance is not critically exposed. If instead the database instance is on a public network, use TLS to encrypt the connection to the database, as it is possible for third-parties to intercept the traffic data.
  105. ### Prerequisites
  106. - You need two valid TLS certificates, one for the database instance (database server) and one for the Gitea instance (database client). Both certificates must be signed by a trusted CA.
  107. - The database certificate must contain `TLS Web Server Authentication` in the `X509v3 Extended Key Usage` extension attribute, while the client certificate needs `TLS Web Client Authentication` in the corresponding attribute.
  108. - On the database server certificate, one of `Subject Alternative Name` or `Common Name` entries must be the fully-qualified domain name (FQDN) of the database instance (e.g. `db.example.com`). On the database client certificate, one of the entries mentioned above must contain the database username that Gitea will be using to connect.
  109. - You need domain name mappings of both Gitea and database servers to their respective IP addresses. Either set up DNS records for them or add local mappings to `/etc/hosts` (`%WINDIR%\System32\drivers\etc\hosts` in Windows) on each system. This allows the database connections to be performed by domain name instead of IP address. See documentation of your system for details.
  110. ### PostgreSQL
  111. The PostgreSQL driver used by Gitea supports two-way TLS. In two-way TLS, both database client and server authenticate each other by sending their respective certificates to their respective opposite for validation. In other words, the server verifies client certificate, and the client verifies server certificate.
  112. 1. On the server with the database instance, place the following credentials:
  113. - `/path/to/postgresql.crt`: Database instance certificate
  114. - `/path/to/postgresql.key`: Database instance private key
  115. - `/path/to/root.crt`: CA certificate chain to validate client certificates
  116. 2. Add following options to `postgresql.conf`:
  117. ```ini
  118. ssl = on
  119. ssl_ca_file = '/path/to/root.crt'
  120. ssl_cert_file = '/path/to/postgresql.crt'
  121. ssl_key_file = '/path/to/postgresql.key'
  122. ssl_min_protocol_version = 'TLSv1.2'
  123. ```
  124. 3. Adjust credentials ownership and permission, as required by PostgreSQL:
  125. ```
  126. chown postgres:postgres /path/to/root.crt /path/to/postgresql.crt /path/to/postgresql.key
  127. chmod 0600 /path/to/root.crt /path/to/postgresql.crt /path/to/postgresql.key
  128. ```
  129. 4. Edit `pg_hba.conf` rule to only allow Gitea database user to connect over SSL, and to require client certificate verification.
  130. For PostgreSQL 12:
  131. ```ini
  132. hostssl giteadb gitea 192.0.2.10/32 scram-sha-256 clientcert=verify-full
  133. ```
  134. For PostgreSQL 11 and earlier:
  135. ```ini
  136. hostssl giteadb gitea 192.0.2.10/32 scram-sha-256 clientcert=1
  137. ```
  138. Replace database name, user, and IP address of Gitea instance as appropriate.
  139. 5. Restart PostgreSQL to apply configurations above.
  140. 6. On the server running the Gitea instance, place the following credentials under the home directory of the user who runs Gitea (e.g. `git`):
  141. - `~/.postgresql/postgresql.crt`: Database client certificate
  142. - `~/.postgresql/postgresql.key`: Database client private key
  143. - `~/.postgresql/root.crt`: CA certificate chain to validate server certificate
  144. Note: Those file names above are hardcoded in PostgreSQL and it is not possible to change them.
  145. 7. Adjust credentials, ownership and permission as required:
  146. ```
  147. chown git:git ~/.postgresql/postgresql.crt ~/.postgresql/postgresql.key ~/.postgresql/root.crt
  148. chown 0600 ~/.postgresql/postgresql.crt ~/.postgresql/postgresql.key ~/.postgresql/root.crt
  149. ```
  150. 8. Test the connection to the database:
  151. ```
  152. psql "postgres://gitea@example.db/giteadb?sslmode=verify-full"
  153. ```
  154. You should be prompted to enter password for the database user, and then be connected to the database.
  155. ### MySQL
  156. While the MySQL driver used by Gitea also supports two-way TLS, Gitea currently supports only one-way TLS. See issue #10828 for details.
  157. In one-way TLS, the database client verifies the certificate sent from server during the connection handshake, and the server assumes that the connected client is legitimate, since client certificate verification doesn't take place.
  158. 1. On the database instance, place the following credentials:
  159. - `/path/to/mysql.crt`: Database instance certificate
  160. - `/path/to/mysql.key`: Database instance key
  161. - `/path/to/ca.crt`: CA certificate chain. This file isn't used on one-way TLS, but is used to validate client certificates on two-way TLS.
  162. 2. Add following options to `my.cnf`:
  163. ```ini
  164. [mysqld]
  165. ssl-ca = /path/to/ca.crt
  166. ssl-cert = /path/to/mysql.crt
  167. ssl-key = /path/to/mysql.key
  168. tls-version = TLSv1.2,TLSv1.3
  169. ```
  170. 3. Adjust credentials ownership and permission:
  171. ```
  172. chown mysql:mysql /path/to/ca.crt /path/to/mysql.crt /path/to/mysql.key
  173. chmod 0600 /path/to/ca.crt /path/to/mysql.crt /path/to/mysql.key
  174. ```
  175. 4. Restart MySQL to apply the setting.
  176. 5. The database user for Gitea may have been created earlier, but it would authenticate only against the IP addresses of the server running Gitea. To authenticate against its domain name, recreate the user, and this time also set it to require TLS for connecting to the database:
  177. ```sql
  178. DROP USER 'gitea'@'192.0.2.10';
  179. CREATE USER 'gitea'@'example.gitea' IDENTIFIED BY 'gitea' REQUIRE SSL;
  180. GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea'@'example.gitea';
  181. FLUSH PRIVILEGES;
  182. ```
  183. Replace database user name, password, and Gitea instance domain as appropriate.
  184. 6. Make sure that the CA certificate chain required to validate the database server certificate is on the system certificate store of both the database and Gitea servers. Consult your system documentation for instructions on adding a CA certificate to the certificate store.
  185. 7. On the server running Gitea, test connection to the database:
  186. ```
  187. mysql -u gitea -h example.db -p --ssl
  188. ```
  189. You should be connected to the database.