admin.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
  4. <!ENTITY mdash "&#x2014;" >
  5. ]>
  6. <chapter id="admin">
  7. <title>Kea Database Administration</title>
  8. <section id="kea-database-version">
  9. <title>Databases and Database Version Numbers</title>
  10. <para>
  11. Kea stores leases in one of several supported databases.
  12. As future versions of Kea are released, the structure of those
  13. databases will change. For example, Kea currently only stores
  14. lease information: in the future, additional data - such as host
  15. reservation details - will also be stored.
  16. </para>
  17. <para>
  18. A given version of Kea expects a particular structure in
  19. the database. It ensures this by checking the version of the
  20. database it is using. Separate version numbers are maintained for
  21. backend databases, independent of the version of Kea itself. It
  22. is possible that the backend database version will stay the same
  23. through several Kea revisions. Likewise, it is possible that the
  24. version of backend database may go up several revisions during a
  25. Kea upgrade. Versions for each database are independent, so an
  26. increment in the MySQL database version does not imply an increment
  27. in that of PostgreSQL.
  28. </para>
  29. <para>
  30. Backend versions are specified in
  31. a <replaceable>major.minor</replaceable> format. The minor
  32. number is increased when there are backward compatible changes
  33. introduced. For example, the addition of a new index. It is
  34. desirable, but not mandatory to apply such a change; you
  35. can run on older database version if you want to. (Although, in
  36. the example given, running without the new index may be at the
  37. expense of a performance penalty.) On the other hand, the major
  38. number is increased when an incompatible change is introduced,
  39. for example an extra column is added to a table. If you try to
  40. run Kea software on a database that is too old (as signified by
  41. mismatched backend major version number), Kea will refuse to run:
  42. administrative action will be required to upgrade the database.
  43. </para>
  44. </section>
  45. <section id="kea-admin">
  46. <title>The kea-admin Tool</title>
  47. <para>
  48. To manage the databases, Kea provides the
  49. <command>kea-admin</command> tool. It is able to initialize
  50. a new database, check its version number, perform a
  51. database upgrade, and dump lease data to a text file.
  52. </para>
  53. <para>
  54. <command>kea-admin</command> takes two mandatory
  55. parameters: <command>command</command> and
  56. <command>backend</command>. Additional, non-mandatory options
  57. may be specified. Currently supported commands are:
  58. <itemizedlist>
  59. <listitem>
  60. <simpara>
  61. <command>lease-init</command> &mdash;
  62. Initializes a new lease database. Useful during first
  63. Kea installation. The database is initialized to the
  64. latest version supported by the version of the software.
  65. </simpara>
  66. </listitem>
  67. <listitem>
  68. <simpara>
  69. <command>lease-version</command> &mdash;
  70. Reports the lease database version number. This is
  71. not necessarily equal to the Kea version number as
  72. each backend has its own versioning scheme.
  73. </simpara>
  74. </listitem>
  75. <listitem>
  76. <simpara>
  77. <command>lease-upgrade</command> &mdash;
  78. Conducts a lease database upgrade. This is useful when
  79. upgrading Kea.
  80. </simpara>
  81. </listitem>
  82. <listitem>
  83. <simpara>
  84. <command>lease-dump</command> &mdash;
  85. Dumps the contents of the lease database (for MySQL or PostgreSQL
  86. backends) to CSV text file. The first line of the file contains
  87. the column names. This is meant to be used as a diagnostic
  88. tool that provides a portable, human-readable form of lease data.
  89. </simpara>
  90. </listitem>
  91. </itemizedlist>
  92. <command>backend</command> specifies the backend type. Currently
  93. supported types are:
  94. <itemizedlist>
  95. <listitem>
  96. <simpara>
  97. <command>memfile</command> &mdash; Lease information is
  98. stored on disk in a text file.
  99. </simpara>
  100. </listitem>
  101. <listitem>
  102. <simpara>
  103. <command>mysql</command> &mdash;
  104. Lease information is stored in a MySQL relational
  105. database.
  106. </simpara>
  107. </listitem>
  108. <listitem>
  109. <simpara>
  110. <command>pgsql</command> &mdash;
  111. Lease information is stored in a PostgreSQL relational
  112. database.
  113. </simpara>
  114. </listitem>
  115. </itemizedlist>
  116. Additional parameters may be needed, depending on your setup
  117. and specific operation: username, password and database name or
  118. the directory where specific files are located. See appropriate
  119. manual page for details (<command>man 8 kea-admin</command>).
  120. </para>
  121. </section>
  122. <section>
  123. <title>Supported Databases</title>
  124. <section>
  125. <title>memfile</title>
  126. <para>
  127. There are no special initialization steps necessary
  128. for the memfile backend. During the first run, both
  129. <command>kea-dhcp4</command> and <command>kea-dhcp6</command>
  130. will create an empty lease file if one is not
  131. present. Necessary disk write permission is required.
  132. </para>
  133. <section id="memfile-upgrade">
  134. <title>Upgrading Memfile Lease Files from an Earlier Version of Kea</title>
  135. <para>
  136. There are no special steps required to upgrade memfile lease files
  137. from an earlier version of Kea to a new version of Kea.
  138. During startup the servers will check the schema version of the lease
  139. files against their own. If there is a mismatch, the servers will
  140. automatically launch the LFC process to convert the files to the
  141. server's schema version. While this mechanism is primarily meant to
  142. ease the process of upgrading to newer versions of Kea, it can also
  143. be used for downgrading should the need arise. When upgrading, any
  144. values not present in the original lease files will be assigned
  145. appropriate default values. When downgrading, any data present in
  146. the files but not in the server's schema will be dropped.
  147. If you wish to convert the files manually, prior to starting the
  148. servers you may do so by running the LFC process yourself.
  149. See <xref linkend="kea-lfc"/> for more information.
  150. </para>
  151. </section>
  152. <!-- @todo: document lease file upgrades once they are implemented in kea-admin -->
  153. </section>
  154. <section>
  155. <title>MySQL</title>
  156. <para>
  157. The MySQL database must be properly set up if you want Kea to
  158. store information in MySQL. This section can be safely ignored
  159. if you chose to store the data in other backends.
  160. </para>
  161. <section id="mysql-database-create">
  162. <title>First Time Creation of Kea Database</title>
  163. <para>
  164. If you are setting the MySQL database for the first time,
  165. you need to create the database area within MySQL and set up
  166. the MySQL user ID under which Kea will access the database.
  167. This needs to be done manually: <command>kea-admin</command>
  168. is not able to do this for you.
  169. </para>
  170. <para>
  171. To create the database:
  172. <orderedlist>
  173. <listitem>
  174. <para>
  175. Log into MySQL as "root":
  176. <screen>
  177. $ <userinput>mysql -u root -p</userinput>
  178. Enter password:
  179. mysql>
  180. </screen>
  181. </para>
  182. </listitem>
  183. <listitem>
  184. <para>
  185. Create the MySQL database:
  186. <screen>
  187. mysql> <userinput>CREATE DATABASE <replaceable>database-name</replaceable>;</userinput>
  188. </screen>
  189. (<replaceable>database-name</replaceable> is the name
  190. you have chosen for the database.)
  191. </para>
  192. </listitem>
  193. <listitem>
  194. <para>
  195. Create the user under which Kea will access the database
  196. (and give it a password), then grant it access to the
  197. database tables:
  198. <screen>
  199. mysql> <userinput>CREATE USER '<replaceable>user-name</replaceable>'@'localhost' IDENTIFIED BY '<replaceable>password</replaceable>';</userinput>
  200. mysql> <userinput>GRANT ALL ON <replaceable>database-name</replaceable>.* TO '<replaceable>user-name</replaceable>'@'localhost';</userinput>
  201. </screen>
  202. (<replaceable>user-name</replaceable> and
  203. <replaceable>password</replaceable> are the user ID
  204. and password you are using to allow Keas access to the
  205. MySQL instance. All apostrophes in the command lines
  206. above are required.)
  207. </para>
  208. </listitem>
  209. <listitem>
  210. <para>
  211. At this point, you may elect to create the database
  212. tables. (Alternatively, you can exit MySQL and create
  213. the tables using the <command>kea-admin</command> tool,
  214. as explained below.) To do this:
  215. <screen>
  216. mysql> <userinput>CONNECT <replaceable>database-name</replaceable>;</userinput>
  217. mysql> <userinput>SOURCE <replaceable>path-to-kea</replaceable>/share/kea/scripts/mysql/dhcpdb_create.mysql</userinput>
  218. </screen>
  219. (<replaceable>path-to-kea</replaceable> is the
  220. location where you installed Kea.)
  221. </para>
  222. </listitem>
  223. <listitem>
  224. <para>
  225. Exit MySQL:
  226. <screen>
  227. mysql> <userinput>quit</userinput>
  228. Bye
  229. $
  230. </screen>
  231. </para>
  232. </listitem>
  233. </orderedlist>
  234. </para>
  235. <para>
  236. If you elected not to create the tables in step 4, you can do
  237. so now by running the <command>kea-admin</command> tool:
  238. <screen>
  239. $ <userinput>kea-admin lease-init mysql -u <replaceable>database-user</replaceable> -p <replaceable>database-password</replaceable> -n <replaceable>database-name</replaceable></userinput>
  240. </screen>
  241. (Do not do this if you did create the tables in step 4.)
  242. <command>kea-admin</command> implements rudimentary checks:
  243. it will refuse to initialize a database that contains any
  244. existing tables. If you want to start from scratch, you
  245. must remove all data manually. (This process is a manual
  246. operation on purpose to avoid possibly irretrievable mistakes
  247. by <command>kea-admin</command>.)
  248. </para>
  249. </section>
  250. <section id="mysql-upgrade">
  251. <title>Upgrading a MySQL Database from an Earlier Version of Kea</title>
  252. <para>
  253. Sometimes a new Kea version may use newer database schema, so
  254. there will be a need to upgrade the existing database. This can
  255. be done using the <command>kea-admin lease-upgrade</command>
  256. command.
  257. </para>
  258. <para>
  259. To check the current version of the database, use the following command:
  260. <screen>
  261. $ <userinput>kea-admin lease-version mysql -u <replaceable>database-user</replaceable> -p <replaceable>database-password</replaceable> -n <replaceable>database-name</replaceable></userinput>
  262. </screen>
  263. (See <xref linkend="kea-database-version"/> for a discussion
  264. about versioning.) If the version does not match the minimum
  265. required for the new version of Kea (as described in the
  266. release notes), the database needs to be upgraded.
  267. </para>
  268. <para>
  269. Before upgrading, please make sure that the database is
  270. backed up. The upgrade process does not discard any data but,
  271. depending on the nature of the changes, it may be impossible
  272. to subsequently downgrade to an earlier version. To perform
  273. an upgrade, issue the following command:
  274. <screen>
  275. $ <userinput>kea-admin lease-upgrade mysql -u <replaceable>database-user</replaceable> -p <replaceable>database-password</replaceable> -n <replaceable>database-name</replaceable></userinput>
  276. </screen>
  277. </para>
  278. </section>
  279. </section> <!-- end of MySQL sections -->
  280. <section>
  281. <title>PostgreSQL</title>
  282. <para>
  283. A PostgreSQL database must be set up if you want Kea to store
  284. lease and other information in PostgreSQL. This step can be
  285. safely ignored if you are using other database backends.
  286. </para>
  287. <section id="pgsql-database-create">
  288. <title>Manually Create the PostgreSQL Database and the Kea User</title>
  289. <para>
  290. The first task is to create both the lease database and the
  291. user under which the servers will access it. A number of steps
  292. are required:
  293. <orderedlist>
  294. <listitem>
  295. <para>
  296. Log into PostgreSQL as "root":
  297. <screen>
  298. $ <userinput>sudo -u postgres psql postgres</userinput>
  299. Enter password:
  300. postgres=#
  301. </screen>
  302. </para>
  303. </listitem>
  304. <listitem>
  305. <para>
  306. Create the database:
  307. <screen>
  308. postgres=#<userinput> CREATE DATABASE <replaceable>database-name</replaceable>;</userinput>
  309. CREATE DATABASE
  310. postgres=#
  311. </screen>
  312. (<replaceable>database-name</replaceable> is the name
  313. you have chosen for the database.)
  314. </para>
  315. </listitem>
  316. <listitem>
  317. <para>
  318. Create the user under which Kea will access the database
  319. (and give it a password), then grant it access to the
  320. database:
  321. <screen>
  322. postgres=#<userinput> CREATE USER <replaceable>user-name</replaceable> WITH PASSWORD '<replaceable>password</replaceable>';</userinput>
  323. CREATE ROLE
  324. postgres=#
  325. postgres=#<userinput> GRANT ALL PRIVILEGES ON DATABASE <replaceable>database-name</replaceable> TO <replaceable>user-name</replaceable>;</userinput>
  326. GRANT
  327. postgres=#
  328. </screen>
  329. </para>
  330. </listitem>
  331. <listitem>
  332. <para>
  333. Exit PostgreSQL:
  334. <screen>
  335. postgres=# <userinput>\q</userinput>
  336. Bye
  337. $
  338. </screen>
  339. </para>
  340. </listitem>
  341. <listitem>
  342. <para>
  343. At this point you are ready to create the database tables.
  344. This can be done using the <command>kea-admin</command> tool
  345. as explained in the next section (recommended), or manually.
  346. To create the tables manually enter the following command.
  347. Note that PostgreSQL will prompt you to enter the new user's
  348. password you specified in Step 3. When the command completes
  349. you will be returned to the shell prompt. You should see output
  350. similar to following:
  351. <screen>
  352. $ <userinput>psql -d <replaceable>database-name</replaceable> -U <replaceable>user-name</replaceable> -f <replaceable>path-to-kea</replaceable>/share/kea/scripts/pgsql/dhcpdb_create.pgsql</userinput>
  353. Password for user <replaceable>user-name</replaceable>:
  354. CREATE TABLE
  355. CREATE INDEX
  356. CREATE INDEX
  357. CREATE TABLE
  358. CREATE INDEX
  359. CREATE TABLE
  360. START TRANSACTION
  361. INSERT 0 1
  362. INSERT 0 1
  363. INSERT 0 1
  364. COMMIT
  365. CREATE TABLE
  366. START TRANSACTION
  367. INSERT 0 1
  368. COMMIT
  369. $
  370. </screen>
  371. (<replaceable>path-to-kea</replaceable> is the location
  372. where you installed Kea.)
  373. </para>
  374. <para>
  375. If instead you encounter an error like:
  376. <screen>
  377. psql: FATAL: no pg_hba.conf entry for host "[local]", user "<replaceable>user-name</replaceable>", database "<replaceable>database-name</replaceable>", SSL off
  378. </screen>
  379. ... you will need to alter the PostgreSQL configuration.
  380. Kea uses password authentication when connecting to
  381. the database and must have the appropriate entries
  382. added to PostgreSQL's pg_hba.conf file. This file is
  383. normally located in the primary data directory for your
  384. PostgreSQL server. The precise path may vary but the
  385. default location for PostgreSQL 9.3 on Centos 6.5 is:
  386. <filename>/var/lib/pgsql/9.3/data/pg_hba.conf</filename>.
  387. </para>
  388. <para>
  389. Assuming Kea is running on the same host as PostgreSQL,
  390. adding lines similar to following should be sufficient to
  391. provide password-authenticated access to Kea's database:
  392. <screen>
  393. local <replaceable>database-name</replaceable> <replaceable>user-name</replaceable> password
  394. host <replaceable>database-name</replaceable> <replaceable>user-name</replaceable> 127.0.0.1/32 password
  395. host <replaceable>database-name</replaceable> <replaceable>user-name</replaceable> ::1/128 password
  396. </screen>
  397. </para>
  398. <para>
  399. These edits are primarily intended as a starting point
  400. not a definitive reference on PostgreSQL administration or
  401. database security. Please consult your PostgreSQL user
  402. manual before making these changes as they may expose
  403. other databases that you run. It may be necessary to
  404. restart PostgreSQL in order for these changes to take effect.
  405. </para>
  406. </listitem>
  407. </orderedlist>
  408. </para>
  409. </section>
  410. <section>
  411. <title>Initialize the PostgreSQL Database Using kea-admin</title>
  412. <para>
  413. If you elected not to create the tables manually, you can do
  414. so now by running the <command>kea-admin</command> tool:
  415. <screen>
  416. $ <userinput>kea-admin lease-init pgsql -u <replaceable>database-user</replaceable> -p <replaceable>database-password</replaceable> -n <replaceable>database-name</replaceable></userinput>
  417. </screen>
  418. Do not do this if you already created the tables in manually.
  419. <command>kea-admin</command> implements rudimentary checks:
  420. it will refuse to initialize a database that contains any
  421. existing tables. If you want to start from scratch, you
  422. must remove all data manually. (This process is a manual
  423. operation on purpose to avoid possibly irretrievable mistakes
  424. by <command>kea-admin</command>.)
  425. </para>
  426. </section>
  427. <section id="pgsql-upgrade">
  428. <title>Upgrading a PostgreSQL Database from an Earlier Version of Kea</title>
  429. <para>
  430. Currently, PostgreSQL only supports Kea schema version 1.0 so no upgrades
  431. are available. As upgrades become available, <command>kea-admin</command>
  432. will support them.
  433. </para>
  434. </section>
  435. </section> <!-- end of PostgreSQL sections -->
  436. <section>
  437. <title>Limitations related to the use of the SQL databases</title>
  438. <para>
  439. The lease expiration time is stored in the SQL database for each lease
  440. as a timestamp value. Kea developers observed that MySQL database doesn't
  441. accept timestamps beyond 2147483647 seconds (maximum signed 32-bit number)
  442. from the beginning of the epoch. At the same time, some versions of PostgreSQL
  443. do accept greater values but the value is altered when it is read back.
  444. For this reason the lease database backends put the restriction for the
  445. maximum timestamp to be stored in the database, which is equal to the
  446. maximum signed 32-bit number. This effectively means that the current
  447. Kea version can't store the leases which expiration time is later than
  448. 2147483647 seconds since the beginning of the epoch (around year 2038).
  449. </para>
  450. </section>
  451. </section> <!-- End of Database sections -->
  452. </chapter>