Browse code

- add group functionality to read content from specified sections of the mysql config to the db_mysql module, db_url examples: * mysql://user:pass@[group]host:port/db or * mysql://user:pass@[group]/db or * mysql://[group]/db - existing functionality is not changed, this is backward compatible - check usernames for NULL before comparing in DB API, otherwise it would segfault when only a group name is specified - patch from Andreas Granig, agranig at sipwise dot com, closes #1815631

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@5413 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt authored on 05/01/2009 12:27:49
Showing 3 changed files
... ...
@@ -34,12 +34,16 @@ Daniel-Constantin Mierla
34 34
 
35 35
         1.4. Exported Functions
36 36
         1.5. Installation
37
+        1.6. Reading configuration from my.cnf
37 38
 
38 39
    List of Examples
39 40
 
40 41
    1.1. Set ping_interval parameter
41 42
    1.2. Set timeout_interval parameter
42 43
    1.3. Set auto_reconnect parameter
44
+   1.4. Set a my.cnf group in db_url parameter
45
+   1.5. Adding a kamailio group to my.cnf
46
+   1.6. Using [client] and specific group
43 47
 
44 48
 Chapter 1. Admin Guide
45 49
 
... ...
@@ -121,3 +125,50 @@ modparam("auto_reconnect", "auto_reconnect", 0)
121 125
      * - from command line use: 'make all
122 126
        include_modules="db_mysql"; make install
123 127
        include_modules="db_mysql"'.
128
+
129
+1.6. Reading configuration from my.cnf
130
+
131
+   In order to take into account specific mysql client options, a
132
+   my.cnf config group can be passed using the db_url module
133
+   parameter. This is done by setting [group] in front of or
134
+   instead of the host part. The following examples are valid
135
+   db_url definitions, which include a my.cnf group:
136
+     * mysql://user:pass@[group]host:port/db
137
+     * mysql://user:pass@[group]:port/db
138
+     * mysql://user:pass@[group]/db
139
+     * mysql://[group]/db
140
+
141
+   Example 1.4. Set a my.cnf group in db_url parameter
142
+...
143
+modparam("usrloc", "db_url", "mysql://[kamailio]/kamailio)
144
+...
145
+
146
+   Example 1.5. Adding a kamailio group to my.cnf
147
+...
148
+[kamailio]
149
+socket = /path/to/mysql.sock
150
+user = kamailiouser
151
+password = kamailiopass
152
+default-character-set = utf8
153
+...
154
+
155
+   In addition to the given group, also the [client] section is
156
+   read, in the order given in my.cnf. So if you for example
157
+   specify a socket in both your specific group and the client
158
+   group, then the value is taken from the last one.
159
+
160
+   Example 1.6. Using [client] and specific group
161
+...
162
+[client]
163
+socket = /var/run/mysql/mysqld.sock
164
+
165
+[kamailio]
166
+socket = /path/to/mysqld.sock
167
+user = kamailiouser
168
+password = kamailiopass
169
+default-character-set = utf8
170
+...
171
+
172
+   In the example given above, the socket /path/to/mysqld.sock is
173
+   used by Kamailio because both [kamailio] and [client] define
174
+   this option, and the latter overwrites the first.
... ...
@@ -140,5 +140,59 @@ modparam("auto_reconnect", "auto_reconnect", 0)
140 140
 			</listitem>
141 141
 		</itemizedlist>
142 142
 	</section>
143
+	<section>
144
+		<title>Reading configuration from my.cnf</title>
145
+		<para>
146
+		In order to take into account specific mysql client options, a my.cnf config group can be passed using the <emphasis>db_url</emphasis> module parameter. This is done by setting <emphasis>[group]</emphasis> in front of or instead of the host part. The following examples are valid <emphasis>db_url</emphasis> definitions, which include a my.cnf group:
147
+		</para>
148
+
149
+		<itemizedlist>
150
+			<listitem>mysql://user:pass@[group]host:port/db</listitem>
151
+			<listitem>mysql://user:pass@[group]:port/db</listitem>
152
+			<listitem>mysql://user:pass@[group]/db</listitem>
153
+			<listitem>mysql://[group]/db</listitem>
154
+		</itemizedlist>
155
+		<example>
156
+		<title>Set a my.cnf group in <varname>db_url</varname> parameter</title>
157
+		<programlisting format="linespecific">
158
+...
159
+modparam("usrloc", "db_url", "mysql://[kamailio]/kamailio)
160
+...
161
+</programlisting>
162
+		</example>
163
+		<example>
164
+		<title>Adding a kamailio group to my.cnf</title>
165
+		<programlisting format="linespecific">
166
+...
167
+[kamailio]
168
+socket = /path/to/mysql.sock
169
+user = kamailiouser
170
+password = kamailiopass
171
+default-character-set = utf8
172
+...
173
+</programlisting>
174
+		</example>
175
+		<para>
176
+		In addition to the given group, also the <emphasis>[client]</emphasis> section is read, in the order given in my.cnf. So if you for example specify a <emphasis>socket</emphasis> in both your specific group and the client group, then the value is taken from the last one.
177
+		</para>
178
+		<example>
179
+		<title>Using [client] and specific group</title>
180
+		<programlisting format="linespecific">
181
+...
182
+[client]
183
+socket = /var/run/mysql/mysqld.sock
184
+
185
+[kamailio]
186
+socket = /path/to/mysqld.sock
187
+user = kamailiouser
188
+password = kamailiopass
189
+default-character-set = utf8
190
+...
191
+</programlisting>
192
+		</example>
193
+		<para>
194
+		In the example given above, the socket <emphasis>/path/to/mysqld.sock</emphasis> is used by &kamailio; because both <emphasis>[kamailio]</emphasis> and <emphasis>[client]</emphasis> define this option, and the latter overwrites the first.
195
+		</para>
196
+	</section>
143 197
 </chapter>
144 198
 
... ...
@@ -43,6 +43,7 @@
43 43
 struct my_con* db_mysql_new_connection(const struct db_id* id)
44 44
 {
45 45
 	struct my_con* ptr;
46
+	char *host, *grp;
46 47
 
47 48
 	if (!id) {
48 49
 		LM_ERR("invalid parameter value\n");
... ...
@@ -66,11 +67,30 @@ struct my_con* db_mysql_new_connection(const struct db_id* id)
66 67
 
67 68
 	mysql_init(ptr->con);
68 69
 
70
+	if (id->host[0] == '[' && (host = strchr(id->host, ']')) != NULL) {
71
+		grp = id->host + 1;
72
+		*host = '\0';
73
+		if (host != id->host + strlen(id->host)-1) {
74
+			host += 1; // host found after closing bracket
75
+		}
76
+		else {
77
+			// let mysql read host info from my.cnf
78
+			// (defaults to "localhost")
79
+			host = NULL;
80
+		}
81
+		// read [client] and [<grp>] sections in the order
82
+		// given in my.cnf
83
+		mysql_options(ptr->con, MYSQL_READ_DEFAULT_GROUP, grp);
84
+	}
85
+	else {
86
+		host = id->host;
87
+	}
88
+
69 89
 	if (id->port) {
70
-		LM_DBG("opening connection: mysql://xxxx:xxxx@%s:%d/%s\n", ZSW(id->host),
90
+		LM_DBG("opening connection: mysql://xxxx:xxxx@%s:%d/%s\n", ZSW(host),
71 91
 			id->port, ZSW(id->database));
72 92
 	} else {
73
-		LM_DBG("opening connection: mysql://xxxx:xxxx@%s/%s\n", ZSW(id->host),
93
+		LM_DBG("opening connection: mysql://xxxx:xxxx@%s/%s\n", ZSW(host),
74 94
 			ZSW(id->database));
75 95
 	}
76 96
 
... ...
@@ -80,10 +100,10 @@ struct my_con* db_mysql_new_connection(const struct db_id* id)
80 100
 	mysql_options(ptr->con, MYSQL_OPT_WRITE_TIMEOUT, (const char *)&db_mysql_timeout_interval);
81 101
 
82 102
 #if (MYSQL_VERSION_ID >= 40100)
83
-	if (!mysql_real_connect(ptr->con, id->host, id->username, id->password,
103
+	if (!mysql_real_connect(ptr->con, host, id->username, id->password,
84 104
 				id->database, id->port, 0, CLIENT_MULTI_STATEMENTS)) {
85 105
 #else
86
-	if (!mysql_real_connect(ptr->con, id->host, id->username, id->password,
106
+	if (!mysql_real_connect(ptr->con, host, id->username, id->password,
87 107
 				id->database, id->port, 0, 0)) {
88 108
 #endif
89 109
 		LM_ERR("driver error: %s\n", mysql_error(ptr->con));