admin.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 compatibile changes
  33. introduced. For example, the addition of a new index. It is
  34. desirable, but not mandatory to 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. <!-- @todo: document lease file upgrades once they are implemented in kea-admin -->
  134. </section>
  135. <section>
  136. <title>MySQL</title>
  137. <para>
  138. The MySQL database must be properly set up if you want Kea to
  139. store information in MySQL. This section can be safely ignored
  140. if you chose to store the data in other backends.
  141. </para>
  142. <section id="mysql-database-create">
  143. <title>First Time Creation of Kea Database</title>
  144. <para>
  145. If you are setting the MySQL database for the first time,
  146. you need to create the database area within MySQL and set up
  147. the MySQL user ID under which Kea will access the database.
  148. This needs to be done manually: <command>kea-admin</command>
  149. is not able to do this for you.
  150. </para>
  151. <para>
  152. To create the database:
  153. <orderedlist>
  154. <listitem>
  155. <para>
  156. Log into MySQL as "root":
  157. <screen>
  158. $ <userinput>mysql -u root -p</userinput>
  159. Enter password:
  160. mysql>
  161. </screen>
  162. </para>
  163. </listitem>
  164. <listitem>
  165. <para>
  166. Create the MySQL database:
  167. <screen>
  168. mysql> <userinput>CREATE DATABASE <replaceable>database-name</replaceable>;</userinput>
  169. </screen>
  170. (<replaceable>database-name</replaceable> is the name
  171. you have chosen for the database.)
  172. </para>
  173. </listitem>
  174. <listitem>
  175. <para>
  176. Create the user under which Kea will access the database
  177. (and give it a password), then grant it access to the
  178. database tables:
  179. <screen>
  180. mysql> <userinput>CREATE USER '<replaceable>user-name</replaceable>'@'localhost' IDENTIFIED BY '<replaceable>password</replaceable>';</userinput>
  181. mysql> <userinput>GRANT ALL ON <replaceable>database-name</replaceable>.* TO '<replaceable>user-name</replaceable>'@'localhost';</userinput>
  182. </screen>
  183. (<replaceable>user-name</replaceable> and
  184. <replaceable>password</replaceable> are the user ID
  185. and password you are using to allow Keas access to the
  186. MySQL instance. All apostrophes in the command lines
  187. above are required.)
  188. </para>
  189. </listitem>
  190. <listitem>
  191. <para>
  192. At this point, you may elect to create the database
  193. tables. (Alternatively, you can exit MySQL and create
  194. the tables using the <command>kea-admin</command> tool,
  195. as explained below.) To do this:
  196. <screen>
  197. mysql> <userinput>CONNECT <replaceable>database-name</replaceable>;</userinput>
  198. mysql> <userinput>SOURCE <replaceable>path-to-kea</replaceable>/share/kea/scripts/mysql/dhcpdb_create.mysql</userinput>
  199. </screen>
  200. (<replaceable>path-to-kea</replaceable> is the
  201. location where you installed Kea.)
  202. </para>
  203. </listitem>
  204. <listitem>
  205. <para>
  206. Exit MySQL:
  207. <screen>
  208. mysql> <userinput>quit</userinput>
  209. Bye
  210. $
  211. </screen>
  212. </para>
  213. </listitem>
  214. </orderedlist>
  215. </para>
  216. <para>
  217. If you elected not to create the tables in step 4, you can do
  218. so now by running the <command>kea-admin</command> tool:
  219. <screen>
  220. $ <userinput>kea-admin lease-init mysql -u <replaceable>database-user</replaceable> -p <replaceable>database-password</replaceable> -n <replaceable>database-name</replaceable></userinput>
  221. </screen>
  222. (Do not do this if you did create the tables in step 4.)
  223. <command>kea-admin</command> implements rudimentary checks:
  224. it will refuse to initialize a database that contains any
  225. existing tables. If you want to start from scratch, you
  226. must remove all data manually. (This process is a manual
  227. operation on purpose to avoid possibly irretrievable mistakes
  228. by <command>kea-admin</command>.)
  229. </para>
  230. </section>
  231. <section id="mysql-upgrade">
  232. <title>Upgrading a MySQL Database from an Earlier Version of Kea</title>
  233. <para>
  234. Sometimes a new Kea version may use newer database schema, so
  235. there will be a need to upgrade the existing database. This can
  236. be done using the <command>kea-admin lease-upgrade</command>
  237. command.
  238. </para>
  239. <para>
  240. To check the current version of the database, use the following command:
  241. <screen>
  242. $ <userinput>kea-admin lease-version mysql -u <replaceable>database-user</replaceable> -p <replaceable>database-password</replaceable> -n <replaceable>database-name</replaceable></userinput>
  243. </screen>
  244. (See <xref linkend="kea-database-version"/> for a discussion
  245. about versioning.) If the version does not match the minimum
  246. required for the new version of Kea (as described in the
  247. release notes), the database needs to be upgraded.
  248. </para>
  249. <para>
  250. Before upgrading, please make sure that the database is
  251. backed up. The upgrade process does not discard any data but,
  252. depending on the nature of the changes, it may be impossible
  253. to subsequently downgrade to an earlier version. To perform
  254. an upgrade, issue the following command:
  255. <screen>
  256. $ <userinput>kea-admin lease-upgrade mysql -u <replaceable>database-user</replaceable> -p <replaceable>database-password</replaceable> -n <replaceable>database-name</replaceable></userinput>
  257. </screen>
  258. </para>
  259. </section>
  260. </section> <!-- end of MySQL sections -->
  261. <section>
  262. <title>PostgreSQL</title>
  263. <para>
  264. A PostgreSQL database must be set up if you want Kea to store
  265. lease and other information in PostgreSQL. This step can be
  266. safely ignored if you are using other database backends.
  267. </para>
  268. <section id="pgsql-database-create">
  269. <title>Manually Create the PostgreSQL Database and the Kea User</title>
  270. <para>
  271. The first task is to create both the lease database and the
  272. user under which the servers will access it. A number of steps
  273. are required:
  274. <orderedlist>
  275. <listitem>
  276. <para>
  277. Log into PostgreSQL as "root":
  278. <screen>
  279. $ <userinput>sudo -u postgres psql postgres</userinput>
  280. Enter password:
  281. postgres=#
  282. </screen>
  283. </para>
  284. </listitem>
  285. <listitem>
  286. <para>
  287. Create the database:
  288. <screen>
  289. postgres=#<userinput> CREATE DATABASE <replaceable>database-name</replaceable>;</userinput>
  290. CREATE DATABASE
  291. postgres=#
  292. </screen>
  293. (<replaceable>database-name</replaceable> is the name
  294. you have chosen for the database.)
  295. </para>
  296. </listitem>
  297. <listitem>
  298. <para>
  299. Create the user under which Kea will access the database
  300. (and give it a password), then grant it access to the
  301. database:
  302. <screen>
  303. postgres=#<userinput> CREATE USER <replaceable>user-name</replaceable> WITH PASSWORD '<replaceable>password</replaceable>';</userinput>
  304. CREATE ROLE
  305. postgres=#
  306. postgres=#<userinput> GRANT ALL PRIVILEGES ON DATABASE <replaceable>database-name</replaceable> TO <replaceable>user-name</replaceable>;</userinput>
  307. GRANT
  308. postgres=#
  309. </screen>
  310. </para>
  311. </listitem>
  312. <listitem>
  313. <para>
  314. Exit PostgreSQL:
  315. <screen>
  316. postgres=# <userinput>\q</userinput>
  317. Bye
  318. $
  319. </screen>
  320. </para>
  321. </listitem>
  322. <listitem>
  323. <para>
  324. At this point you are ready to create the database tables.
  325. This can be done using the <command>kea-admin</command> tool
  326. as explained in the next section (recommended), or manually.
  327. To create the tables manually enter the following command.
  328. Note that PostgreSQL will prompt you to enter the new user's
  329. password you specified in Step 3. When the command completes
  330. you will be returned to the shell prompt. You should see output
  331. similar to following:
  332. <screen>
  333. $ <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>
  334. Password for user <replaceable>user-name</replaceable>:
  335. CREATE TABLE
  336. CREATE INDEX
  337. CREATE INDEX
  338. CREATE TABLE
  339. CREATE INDEX
  340. CREATE TABLE
  341. START TRANSACTION
  342. INSERT 0 1
  343. INSERT 0 1
  344. INSERT 0 1
  345. COMMIT
  346. CREATE TABLE
  347. START TRANSACTION
  348. INSERT 0 1
  349. COMMIT
  350. $
  351. </screen>
  352. (<replaceable>path-to-kea</replaceable> is the location
  353. where you installed Kea.)
  354. </para>
  355. <para>
  356. If instead you encounter an error like:
  357. <screen>
  358. psql: FATAL: no pg_hba.conf entry for host "[local]", user "<replaceable>user-name</replaceable>", database "<replaceable>database-name</replaceable>", SSL off
  359. </screen>
  360. ... you will need to alter the PostgreSQL configuration.
  361. Kea uses password authentication when connecting to
  362. the database and must have the appropriate entries
  363. added to PostgreSQL's pg_hba.conf file. This file is
  364. normally located in the primary data directory for your
  365. PostgreSQL server. The precise path may vary but the
  366. default location for PostgreSQL 9.3 on Centos 6.5 is:
  367. <filename>/var/lib/pgsql/9.3/data/pg_hba.conf</filename>.
  368. </para>
  369. <para>
  370. Assuming Kea is running on the same host as PostgreSQL,
  371. adding lines similar to following should be sufficient to
  372. provide password-authenticated access to Kea's database:
  373. <screen>
  374. local <replaceable>database-name</replaceable> <replaceable>user-name</replaceable> password
  375. host <replaceable>database-name</replaceable> <replaceable>user-name</replaceable> 127.0.0.1/32 password
  376. host <replaceable>database-name</replaceable> <replaceable>user-name</replaceable> ::1/128 password
  377. </screen>
  378. </para>
  379. <para>
  380. These edits are primarily intended as a starting point
  381. not a definitive reference on PostgreSQL administration or
  382. database security. Please consult your PostgreSQL user
  383. manual before making these changes as they may expose
  384. other databases that you run. It may be necessary to
  385. restart PostgreSQL in order for these changes to take effect.
  386. </para>
  387. </listitem>
  388. </orderedlist>
  389. </para>
  390. </section>
  391. <section>
  392. <title>Initialize the PostgreSQL Database Using kea-admin</title>
  393. <para>
  394. If you elected not to create the tables manually, you can do
  395. so now by running the <command>kea-admin</command> tool:
  396. <screen>
  397. $ <userinput>kea-admin lease-init pgsql -u <replaceable>database-user</replaceable> -p <replaceable>database-password</replaceable> -n <replaceable>database-name</replaceable></userinput>
  398. </screen>
  399. Do not do this if you already created the tables in manually.
  400. <command>kea-admin</command> implements rudimentary checks:
  401. it will refuse to initialize a database that contains any
  402. existing tables. If you want to start from scratch, you
  403. must remove all data manually. (This process is a manual
  404. operation on purpose to avoid possibly irretrievable mistakes
  405. by <command>kea-admin</command>.)
  406. </para>
  407. </section>
  408. <section id="pgsql-upgrade">
  409. <title>Upgrading a PostgreSQL Database from an Earlier Version of Kea</title>
  410. <para>
  411. Currently, PostgreSQL only supports Kea schema version 1.0 so no upgrades
  412. are available. As upgrades become available, <command>kea-admin</command>
  413. will support them.
  414. </para>
  415. </section>
  416. </section> <!-- end of PostgreSQL sections -->
  417. <section>
  418. <title>Limitations related to the use of the SQL databases</title>
  419. <para>
  420. The lease expiration time is stored in the SQL database for each lease
  421. as a timestamp value. Kea developers observed that MySQL database doesn't
  422. accept timestamps beyond 2147483647 seconds (maximum signed 32-bit number)
  423. from the beginning of the epoch. At the same time, some versions of PostgreSQL
  424. do accept greater values but the value is altered when it is read back.
  425. For this reason the lease database backends put the restriction for the
  426. maximum timestamp to be stored in the database, which is equal to the
  427. maximum signed 32-bit number. This effectively means that the current
  428. Kea version can't store the leases which expiration time is later than
  429. 2147483647 seconds since the beginning of the epoch (around year 2038).
  430. </para>
  431. </section>
  432. </section> <!-- End of Database sections -->
  433. </chapter>