[c5c522c] | 1 | $Id: mysql_drv.txt,v 1.5 2011/06/28 00:13:48 sbajic Exp $ |
---|
| 2 | |
---|
| 3 | COPYRIGHT (C) 2002-2012 DSPAM Project |
---|
| 4 | http://dspam.sourceforge.net |
---|
| 5 | |
---|
| 6 | LICENSE |
---|
| 7 | |
---|
| 8 | This program is free software: you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU Affero General Public License as |
---|
| 10 | published by the Free Software Foundation, either version 3 of the |
---|
| 11 | License, or (at your option) any later version. |
---|
| 12 | |
---|
| 13 | This program is distributed in the hope that it will be useful, |
---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | GNU Affero General Public License for more details. |
---|
| 17 | |
---|
| 18 | You should have received a copy of the GNU Affero General Public License |
---|
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 20 | |
---|
| 21 | ABOUT |
---|
| 22 | |
---|
| 23 | mysql_drv is a MySQL storage driver for DSPAM v3.0 and above. This driver |
---|
| 24 | enables DSPAM to read and write all token, signature, and statistics data |
---|
| 25 | from a MySQL database. The advantages of using a SQL backend are obvious: |
---|
| 26 | |
---|
| 27 | - Centralized data storage |
---|
| 28 | - Structured queries for information |
---|
| 29 | - No need for context locking mechanisms |
---|
| 30 | - Replication and other MySQL features |
---|
| 31 | |
---|
| 32 | DSPAM support MySQL 5.0 and greater. |
---|
| 33 | |
---|
| 34 | 1. CONFIGURING DSPAM |
---|
| 35 | |
---|
| 36 | To configure DSPAM to use mysql_drv, use the following arguments while running |
---|
| 37 | DSPAM's configure: |
---|
| 38 | |
---|
| 39 | --with-storage-driver=mysql_drv |
---|
| 40 | Tells DSPAM to use the mysql_drv driver |
---|
| 41 | |
---|
| 42 | --with-mysql-libraries=/path/to/libs |
---|
| 43 | Tells DSPAM where to find the MySQL client libraries. They are usually |
---|
| 44 | located in /usr/local/mysql/lib |
---|
| 45 | |
---|
| 46 | --with-mysql-includes=/path/to/libs |
---|
| 47 | Tells DSPAM where to find the MySQL headers. They are usually located in |
---|
| 48 | /usr/local/mysql/include |
---|
| 49 | |
---|
| 50 | --enable-virtual-users |
---|
| 51 | Tells DSPAM to create virtual user ids for each dspam user. Use this if your |
---|
| 52 | users don't really exist on the system (e.g. via getpwuid) or if you're doing |
---|
| 53 | something weird like sharing uids. |
---|
| 54 | |
---|
| 55 | After configure has successfully finished, build and install DSPAM using the |
---|
| 56 | instructions from DSPAM's readme. |
---|
| 57 | |
---|
| 58 | 2. CREATING MYSQL OBJECTS |
---|
| 59 | |
---|
| 60 | Before mysql_drv will function, you must run the mysql_objects-4.1.sql file |
---|
| 61 | (located in src/tools.mysql_drv) to create the table objects required by the |
---|
| 62 | driver. See your MySQL documentation for further information. |
---|
| 63 | |
---|
| 64 | This script assumes that you have already created a database for DSPAM objects |
---|
| 65 | and a user with full access to SELECT, INSERT, UPDATE, and DELETE. |
---|
| 66 | |
---|
| 67 | If you plan on enabling virtual users (something you'll need to do if the users |
---|
| 68 | don't actually exist on your system), also run virtual_users.sql. |
---|
| 69 | |
---|
| 70 | You may also wish to add this line to the mysqld portion of /etc/my.cnf: |
---|
| 71 | |
---|
| 72 | set-variable = max_allowed_packet=8192000 |
---|
| 73 | |
---|
| 74 | This will extend the query size from 1MB to 8MB; some larger queries may |
---|
| 75 | cause MySQL to drop connection without this set. |
---|
| 76 | |
---|
| 77 | NOTE: The mysql_objects-space.sql, mysql_objects-speed.sql and purge,sql files |
---|
| 78 | located in src/tools.mysql_drv were originally created for older MySQL versions. |
---|
| 79 | They can be used on recent MySQL versions, but do not use MySQL features for |
---|
| 80 | improved performance available since MySQL 4.1. For new setups, you should |
---|
| 81 | always use the *-4.1.sql files. |
---|
| 82 | |
---|
| 83 | 3. UPDATING DSPAM.CONF |
---|
| 84 | |
---|
| 85 | mysql_drv needs to know how to connect to your MySQL database. You will need to |
---|
| 86 | specify this information in dspam.conf. This file already contains some example |
---|
| 87 | data: |
---|
| 88 | |
---|
| 89 | MySQLServer /var/lib/mysql/mysql.sock |
---|
| 90 | MySQLPort |
---|
| 91 | MySQLUser dspam |
---|
| 92 | MySQLPass changeme |
---|
| 93 | MySQLDb dspam |
---|
| 94 | MySQLCompress true |
---|
| 95 | MySQLReconnect true |
---|
| 96 | |
---|
| 97 | If you are using MySQL >= 5.0.13 and have problems with DSPAM dropping the |
---|
| 98 | connection to your MySQL instance, then set MySQLReconnect to true to allow |
---|
| 99 | the mysql_drv to try to re-establish stale and/or dead connections. |
---|
| 100 | |
---|
| 101 | Or if you'd like to connect via TCP, use the IP address and port of the |
---|
| 102 | MySQL server. DSPAM checks to see if the first character of the HOSTNAME field |
---|
| 103 | is a slash, and if so will treat it like a socket file. |
---|
| 104 | |
---|
| 105 | 4. NIGHTLY PURGE |
---|
| 106 | |
---|
| 107 | If you would like to purge the many stale tokens DSPAM will have lying around |
---|
| 108 | the database, you should run one of the provided purge scripts nightly. |
---|
| 109 | The dspam_clean tool can be configured to perform more granular cleansing, |
---|
| 110 | such as that of uninteresting data. See DSPAM's README for more information |
---|
| 111 | about the dspam_clean tool. |
---|
| 112 | |
---|
| 113 | If you are supporting TOE or TUM-mode users on your system, you will want to |
---|
| 114 | do one of the following: |
---|
| 115 | |
---|
| 116 | 1. Preferences Extensions |
---|
| 117 | |
---|
| 118 | It is recommended you activate preferences extension support. |
---|
| 119 | purge-4.1.sql will skip certain types of purges for users with TOE/TUM |
---|
| 120 | specified in their preferences. |
---|
| 121 | |
---|
| 122 | NOTE: You should add a preference for any global users on your system, so |
---|
| 123 | that their data is purged as if TOE-based (since global user data is |
---|
| 124 | rarely updated). You can do this using: |
---|
| 125 | |
---|
| 126 | dspam_admin add pref [username] trainingMode TOE |
---|
| 127 | |
---|
| 128 | 2. Global TOE Support |
---|
| 129 | |
---|
| 130 | If you are using TOE globally (and not in individual user preferences), |
---|
| 131 | then you should update purge-4.1.sql, and set the default training mode |
---|
| 132 | to TOE: |
---|
| 133 | |
---|
| 134 | SET @TrainingMode = 'TOE'; |
---|
| 135 | |
---|
| 136 | This will prevent the purging of stale tokens, which could cause serious |
---|
| 137 | data loss in TOE databases (because tokens are never touched unless |
---|
| 138 | an error has occured). All other purges should be safe even for TOE-mode |
---|
| 139 | users. |
---|
| 140 | |
---|
| 141 | If you will NOT be supporting TOE users on your system, you may simply run |
---|
| 142 | the purge-4,1.sql script nightly, as-is. |
---|
| 143 | |
---|
| 144 | 5. TUNING |
---|
| 145 | |
---|
| 146 | If you have a busy server, and find a lot of table locks, you may consider |
---|
| 147 | making a few tweaks to the MySQL configuration. Alternatively, you may |
---|
| 148 | consider InnoDB which performs row-level locking, but this is rarely necessary. |
---|
| 149 | |
---|
| 150 | Increase key_buffer_size. If you have the memory, try at least 256M or 512M. |
---|
| 151 | |
---|
| 152 | Increase table_cache. Try a higher value (some systems go as high as 1024). |
---|
| 153 | This is related to the max_connections option and allows many file descriptors |
---|
| 154 | to be shared among threads. |
---|
| 155 | |
---|
| 156 | Increase myisam_sort_buffer_size to a higher value, depending on your memory. |
---|
| 157 | Ideally, a few hundred MB would be great, but at least 64M would be an |
---|
| 158 | improvement. |
---|
| 159 | |
---|
| 160 | 5.1 USING INNODB |
---|
| 161 | |
---|
| 162 | On very large tables using InnoDB instead of MyISAM could speedup DSPAM because |
---|
| 163 | InnoDB uses row-level locking while MyISAM uses table-level locking. The row- |
---|
| 164 | level locking (used in InnoDB) may be faster because additional updates are not |
---|
| 165 | waiting for the entire table (used in MyISAM) to unlock. In most cases using |
---|
| 166 | table-level locking instead of row-level locking is considered to be faster but |
---|
| 167 | since DSPAM can use many concurrent processes (especially if running in daemon |
---|
| 168 | mode) where each of the processes maintains his own connection to MySQL, switching |
---|
| 169 | to row-level locking (as used in InnoDB) could noticeable speedup the processing |
---|
| 170 | throughput of DSPAM. |
---|
| 171 | |
---|
| 172 | To convert all DSPAM tables to use InnoDB engine, execute the following SQL |
---|
| 173 | commands against your DSPAM database: |
---|
| 174 | ALTER TABLE `dspam_signature_data` |
---|
| 175 | ENGINE = InnoDB; |
---|
| 176 | ALTER TABLE `dspam_stats` |
---|
| 177 | ENGINE = InnoDB; |
---|
| 178 | ALTER TABLE `dspam_token_data` |
---|
| 179 | ENGINE = InnoDB; |
---|
| 180 | ALTER TABLE `dspam_virtual_uids` |
---|
| 181 | ENGINE = InnoDB; |
---|
| 182 | |
---|
| 183 | If you are using the preference extension then issue the following SQL command |
---|
| 184 | to convert the preference extension table to InnoDB: |
---|
| 185 | ALTER TABLE `dspam_preferences` |
---|
| 186 | ENGINE = InnoDB; |
---|
| 187 | |
---|
| 188 | When using InnoDB you can add additional constrains to the DSPAM tables so that |
---|
| 189 | automatically when you remove a user in DSPAM all his/her tokens, signatures, |
---|
| 190 | preferences and statistic data get removed as well. |
---|
| 191 | |
---|
| 192 | !! IMPORTANT: Do not add those constrains if you are using DSPAM virtual user |
---|
| 193 | aliases (aka: DSPAM in relay mode) !! |
---|
| 194 | |
---|
| 195 | To add the additional constrains execute the following SQL commands against your |
---|
| 196 | DSPAM database: |
---|
| 197 | SET FOREIGN_KEY_CHECKS=0; |
---|
| 198 | ALTER TABLE `dspam_signature_data` |
---|
| 199 | ADD CONSTRAINT `dspam_signature_data_ibfk_1` |
---|
| 200 | FOREIGN KEY (`uid`) |
---|
| 201 | REFERENCES `dspam_virtual_uids` (`uid`) |
---|
| 202 | ON DELETE CASCADE; |
---|
| 203 | ALTER TABLE `dspam_stats` |
---|
| 204 | ADD CONSTRAINT `dspam_stats_ibfk_1` |
---|
| 205 | FOREIGN KEY (`uid`) |
---|
| 206 | REFERENCES `dspam_virtual_uids` (`uid`) |
---|
| 207 | ON DELETE CASCADE; |
---|
| 208 | ALTER TABLE `dspam_token_data` |
---|
| 209 | ADD CONSTRAINT `dspam_token_data_ibfk_1` |
---|
| 210 | FOREIGN KEY (`uid`) |
---|
| 211 | REFERENCES `dspam_virtual_uids` (`uid`) |
---|
| 212 | ON DELETE CASCADE; |
---|
| 213 | SET FOREIGN_KEY_CHECKS=1; |
---|
| 214 | |
---|
| 215 | If you are using the preference extension then issue the following SQL |
---|
| 216 | commands to add the additional constrain to the preference extension table: |
---|
| 217 | SET FOREIGN_KEY_CHECKS=0; |
---|
| 218 | ALTER TABLE `dspam_preferences` |
---|
| 219 | ADD CONSTRAINT `dspam_preferences_ibfk_1` |
---|
| 220 | FOREIGN KEY (`uid`) |
---|
| 221 | REFERENCES `dspam_virtual_uids` (`uid`) |
---|
| 222 | ON DELETE CASCADE; |
---|
| 223 | SET FOREIGN_KEY_CHECKS=1; |
---|
| 224 | |
---|
| 225 | If you have added those constrains and later decide to switch to DSPAM virtual |
---|
| 226 | user aliases or decide that you don't need/want those constrains then issue the |
---|
| 227 | following SQL commands against your DSPAM database to remove the constrains: |
---|
| 228 | SET FOREIGN_KEY_CHECKS=0; |
---|
| 229 | ALTER TABLE `dspam_signature_data` |
---|
| 230 | DROP FOREIGN KEY `dspam_signature_data_ibfk_1`; |
---|
| 231 | ALTER TABLE `dspam_stats` |
---|
| 232 | DROP FOREIGN KEY `dspam_stats_ibfk_1`; |
---|
| 233 | ALTER TABLE `dspam_token_data` |
---|
| 234 | DROP FOREIGN KEY `dspam_token_data_ibfk_1`; |
---|
| 235 | SET FOREIGN_KEY_CHECKS=1; |
---|
| 236 | |
---|
| 237 | If you have enabled the preference extension in DSPAM then do not forget to |
---|
| 238 | remove the constrain from the dspam_preferences table as well: |
---|
| 239 | SET FOREIGN_KEY_CHECKS=0; |
---|
| 240 | ALTER TABLE `dspam_preferences` |
---|
| 241 | DROP FOREIGN KEY `dspam_preferences_ibfk_1`; |
---|
| 242 | SET FOREIGN_KEY_CHECKS=1; |
---|
| 243 | |
---|
| 244 | 6. REPAIRING |
---|
| 245 | |
---|
| 246 | If your database gets corrupt, you'll need to repair it. This could take a |
---|
| 247 | long time, and so it may make sense to keep a hot backup somewhere. You can |
---|
| 248 | run a command like this to repair the database: |
---|
| 249 | |
---|
| 250 | mysqlcheck --all-databases --fast --auto-repair |
---|
| 251 | |
---|
| 252 | ERRORS |
---|
| 253 | |
---|
| 254 | Any SQL errors will be reported to LOGDIR/sql.errors as well as the standard |
---|
| 255 | syslog facilities (although the query will be truncated). |
---|
| 256 | |
---|
| 257 | QUESTIONS |
---|
| 258 | |
---|
| 259 | Please contact the dspam-dev mailing list with any questions or constructive |
---|
| 260 | feedback. |
---|
| 261 | |
---|
| 262 | Initial storage driver written by Jonathan A. Zdziarski <jonathan@nuclearelephant.com> |
---|
| 263 | and later enhanced by Stevan Bajic <stevan@bajic.ch> for DSPAM 3.9.0. |
---|