]> git.pld-linux.org Git - packages/mysql.git/blob - mysql.init
- initialize database for mysql 4.x not 3.x (until now this script after mysql init...
[packages/mysql.git] / mysql.init
1 #!/bin/sh
2 #
3 # mysql         A very fast and reliable SQL database engine
4 #
5 # chkconfig:    2345 84 25
6 #
7 # description:  A very fast and reliable SQL database engine.
8 #
9 # pidfile:      /var/lib/mysql/mysqld.pid
10
11 # Source function library
12 . /etc/rc.d/init.d/functions
13
14 # Get network config
15 . /etc/sysconfig/network
16
17 # Check that networking is up
18 if is_yes "${NETWORKING}"; then
19         if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
20                 msg_network_down MySQL
21                 exit 1
22         fi
23 else
24         exit 0
25 fi
26
27 MYSQLD_ERRLOG=/var/log/mysql/err
28 MYSQLD_CONFIG=/etc/mysqld.conf 
29
30 # Get service config
31 if [ -f /etc/sysconfig/mysql ]; then
32         . /etc/sysconfig/mysql
33 fi
34
35 # Daemon doesn't die if config doesn't exist
36 if [ ! -f $MYSQLD_CONFIG ]; then 
37         nls "%s config %s does not exist." MySQL "$MYSQLD_CONFIG" >&2
38         exit 6
39 fi
40
41 eval `awk '
42 /^[ \t]*\[.*\][ \t]*$/ {
43         match($0,/\[.*\]/)
44         section=substr($0,RSTART+1,RSTART+RLENGTH-3)
45 }
46 section=="mysqld" && $2~"=" {
47         if ($1=="datadir") {
48                 printf("MYSQL_DATA_DIR=%s;", $3)
49         } else if ($1=="user") {
50                 printf("MYSQL_USER=%s;", $3)
51         } else if ($1=="pid-file") {
52                 printf("MYSQL_PIDFILE=%s;", $3)
53         }
54 }
55 END {
56         print "export MYSQL_DATA_DIR MYSQL_USER MYSQL_PIDFILE"
57 }
58 ' /etc/mysqld.conf`
59
60 if [ -z $MYSQL_DATA_DIR ]; then 
61         echo "$(nls 'MySQL datadir %s not configured properly' "$MYSQL_DATA_DIR")"'!' >&2
62         nls "Edit %s and configure it." "$MYSQLD_CONFIG" >&2
63         exit 6
64 fi
65
66 if [ -z $MYSQL_USER ]; then 
67         echo "$(nls 'MySQL user not configured properly')"'!' >&2
68         nls "Edit %s and configure it." "$MYSQLD_CONFIG" >&2
69         exit 6
70 fi
71
72 RETVAL=0
73 case "$1" in
74   start)
75         if [ ! -d "$MYSQL_DATA_DIR/mysql" ]; then
76                 nls "MySQL database not initialized. Try \"%s init\" before start." "$0" >&2
77                 exit 6
78         fi
79
80         # Check if the service is already running?              
81         if [ ! -f /var/lock/subsys/mysql ]; then
82                 msg_starting MySQL
83                 busy
84
85                 if [ ! -f $MYSQLD_ERRLOG ]; then 
86                         touch $MYSQLD_ERRLOG
87                 fi
88                 chown mysql.mysql $MYSQLD_ERRLOG
89                 chmod 640 $MYSQLD_ERRLOG
90
91                 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
92                 TMPDIR=/tmp nice -n ${SERVICE_RUN_NICE_LEVEL:-$DEFAULT_SERVICE_RUN_NICE_LEVEL} /usr/sbin/mysqld --defaults-file=$MYSQLD_CONFIG >>$MYSQLD_ERRLOG 2>&1 &
93                 sleep 1
94                 if ps -C mysqld >/dev/null 2>&1; then
95                         ok
96                 else
97                         RETVAL=1
98                         fail
99                 fi
100                 [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mysql
101         else    
102                 msg_already_running MySQL
103         fi
104         ;;
105   stop)
106         if [ -f /var/lock/subsys/mysql ]; then
107                 msg_stopping MySQL
108                 killproc mysqld
109                 sleep 1
110                 rm -f /var/lock/subsys/mysql $MYSQLD_PIDFILE >/dev/null 2>&1
111         else
112                 msg_not_running MySQL
113         fi
114         ;;
115   status)
116         status mysqld
117         exit $?
118         ;;
119   restart|force-reload)
120         $0 stop
121         $0 start
122         exit $?
123         ;;
124   init)
125         # Check if not exist init database
126         if [ -d "$MYSQL_DATA_DIR/mysql" ]; then
127                 nls "Seems that database is initialized now. Remove by hand %s" "$MYSQL_DATA_DIR/mysql"
128                 nls "before initializing database."
129                 exit 1;
130         fi
131
132         show "Creating privilege mysql tables"
133         busy
134         TMP=/tmp TMPDIR=/tmp 
135         
136         # Install this in the user table, too
137         hostname="`hostname --fqdn | tr -d '[:space:]'`"
138                 
139         # Check if hostname is valid
140         if [ -z "$hostname" ]; then
141                 deltext
142                 fail
143                 nls "Sorry, the host name is not configured."
144                 nls "Please configure the 'hostname' command to return a hostname."
145                 exit 1
146         elif ! hostname -i >/dev/null 2>&1; then 
147                 deltext
148                 fail
149                 nls "Sorry, the host '%s' could not be looked up." "$hostname"
150                 nls "Please configure the 'hostname' command to return a correct hostname."
151                 exit 1
152         fi
153
154         # Initialize variables
155         c_d="" i_d=""
156         c_h="" i_h=""
157         c_u="" i_u=""
158         c_f="" i_f=""
159         c_t="" c_c=""
160
161         # Check for old tables
162         if test ! -f $mdata/db.frm
163         then
164           # mysqld --bootstrap wants one command/line
165           c_d="$c_d CREATE TABLE db ("
166           c_d="$c_d   Host char(60) DEFAULT '' NOT NULL,"
167           c_d="$c_d   Db char(64) DEFAULT '' NOT NULL,"
168           c_d="$c_d   User char(16) DEFAULT '' NOT NULL,"
169           c_d="$c_d   Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
170           c_d="$c_d   Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
171           c_d="$c_d   Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
172           c_d="$c_d   Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
173           c_d="$c_d   Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
174           c_d="$c_d   Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
175           c_d="$c_d   Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
176           c_d="$c_d   References_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
177           c_d="$c_d   Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
178           c_d="$c_d   Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
179           c_d="$c_d   Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
180           c_d="$c_d   Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
181           c_d="$c_d PRIMARY KEY Host (Host,Db,User),"
182           c_d="$c_d KEY User (User)"
183           c_d="$c_d )"
184           c_d="$c_d comment='Database privileges';"
185
186           i_d="INSERT INTO db VALUES ('%','test','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y','Y','Y');
187           INSERT INTO db VALUES ('%','test\_%','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y','Y','Y');"
188             
189         fi
190
191         if test ! -f $mdata/host.frm
192         then
193           c_h="$c_h CREATE TABLE host ("
194           c_h="$c_h  Host char(60) DEFAULT '' NOT NULL,"
195           c_h="$c_h  Db char(64) DEFAULT '' NOT NULL,"
196           c_h="$c_h  Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
197           c_h="$c_h  Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
198           c_h="$c_h  Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
199           c_h="$c_h  Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
200           c_h="$c_h  Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
201           c_h="$c_h  Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
202           c_h="$c_h  Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
203           c_h="$c_h  References_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
204           c_h="$c_h  Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
205           c_h="$c_h  Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
206           c_h="$c_h  Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
207           c_h="$c_h  Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
208           c_h="$c_h  PRIMARY KEY Host (Host,Db)"
209           c_h="$c_h )"
210           c_h="$c_h comment='Host privileges;  Merged with database privileges';"
211         fi
212
213         if test ! -f $mdata/user.frm
214         then
215           c_u="$c_u CREATE TABLE user ("
216           c_u="$c_u   Host char(60) DEFAULT '' NOT NULL,"
217           c_u="$c_u   User char(16) DEFAULT '' NOT NULL,"
218           c_u="$c_u   Password char(16) DEFAULT '' NOT NULL,"
219           c_u="$c_u   Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
220           c_u="$c_u   Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
221           c_u="$c_u   Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
222           c_u="$c_u   Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
223           c_u="$c_u   Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
224           c_u="$c_u   Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
225           c_u="$c_u   Reload_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
226           c_u="$c_u   Shutdown_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
227           c_u="$c_u   Process_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
228           c_u="$c_u   File_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
229           c_u="$c_u   Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
230           c_u="$c_u   References_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
231           c_u="$c_u   Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
232           c_u="$c_u   Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
233           c_u="$c_u   Show_db_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
234           c_u="$c_u   Super_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
235           c_u="$c_u   Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
236           c_u="$c_u   Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
237           c_u="$c_u   Execute_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
238           c_u="$c_u   Repl_slave_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
239           c_u="$c_u   Repl_client_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
240           c_u="$c_u   ssl_type enum('','ANY','X509', 'SPECIFIED') DEFAULT '' NOT NULL,"
241           c_u="$c_u   ssl_cipher BLOB NOT NULL,"
242           c_u="$c_u   x509_issuer BLOB NOT NULL,"
243           c_u="$c_u   x509_subject BLOB NOT NULL,"
244           c_u="$c_u   max_questions int(11) unsigned DEFAULT 0  NOT NULL,"
245           c_u="$c_u   max_updates int(11) unsigned DEFAULT 0  NOT NULL,"
246           c_u="$c_u   max_connections int(11) unsigned DEFAULT 0  NOT NULL,"
247           c_u="$c_u   PRIMARY KEY Host (Host,User)"
248           c_u="$c_u )"
249           c_u="$c_u comment='Users and global privileges';"
250
251           i_u="INSERT INTO user VALUES ('localhost','mysql','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
252           INSERT INTO user VALUES ('$hostname','mysql','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
253
254           REPLACE INTO user VALUES ('localhost','mysql','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
255           REPLACE INTO user VALUES ('$hostname','mysql','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
256
257           INSERT INTO user VALUES ('localhost','mysql_logrotate','','N','N','N','N','N','N','Y','N','N','N','N','N','N','N','N','N','N','N','N','N','N','','','','',0,0,0);"
258         fi
259
260         if test ! -f $mdata/func.frm
261         then
262           c_f="$c_f CREATE TABLE func ("
263           c_f="$c_f   name char(64) DEFAULT '' NOT NULL,"
264           c_f="$c_f   ret tinyint(1) DEFAULT '0' NOT NULL,"
265           c_f="$c_f   dl char(128) DEFAULT '' NOT NULL,"
266           c_f="$c_f   type enum ('function','aggregate') NOT NULL,"
267           c_f="$c_f   PRIMARY KEY (name)"
268           c_f="$c_f )"
269           c_f="$c_f   comment='User defined functions';"
270         fi
271
272         if test ! -f $mdata/tables_priv.frm
273         then
274           c_t="$c_t CREATE TABLE tables_priv ("
275           c_t="$c_t   Host char(60) DEFAULT '' NOT NULL,"
276           c_t="$c_t   Db char(64) DEFAULT '' NOT NULL,"
277           c_t="$c_t   User char(16) DEFAULT '' NOT NULL,"
278           c_t="$c_t   Table_name char(60) DEFAULT '' NOT NULL,"
279           c_t="$c_t   Grantor char(77) DEFAULT '' NOT NULL,"
280           c_t="$c_t   Timestamp timestamp(14),"
281           c_t="$c_t   Table_priv set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter') DEFAULT '' NOT NULL,"
282           c_t="$c_t   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
283           c_t="$c_t   PRIMARY KEY (Host,Db,User,Table_name),"
284           c_t="$c_t   KEY Grantor (Grantor)"
285           c_t="$c_t )"
286           c_t="$c_t   comment='Table privileges';"
287         fi
288
289         if test ! -f $mdata/columns_priv.frm
290         then
291           c_c="$c_c CREATE TABLE columns_priv ("
292           c_c="$c_c   Host char(60) DEFAULT '' NOT NULL,"
293           c_c="$c_c   Db char(60) DEFAULT '' NOT NULL,"
294           c_c="$c_c   User char(16) DEFAULT '' NOT NULL,"
295           c_c="$c_c   Table_name char(60) DEFAULT '' NOT NULL,"
296           c_c="$c_c   Column_name char(60) DEFAULT '' NOT NULL,"
297           c_c="$c_c   Timestamp timestamp(14),"
298           c_c="$c_c   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
299           c_c="$c_c   PRIMARY KEY (Host,Db,User,Table_name,Column_name)"
300           c_c="$c_c )"
301           c_c="$c_c   comment='Column privileges';"
302         fi
303
304         if /usr/sbin/mysqld --bootstrap --skip-grant-tables \
305             --datadir=$MYSQL_DATA_DIR --user=$MYSQL_USER << END_OF_DATA
306 CREATE DATABASE mysql;
307 use mysql;
308 $c_d
309 $i_d
310
311 $c_h
312 $i_h
313
314 $c_u
315 $i_u
316
317 $c_f
318 $i_f
319
320 $c_t
321 $c_c
322 END_OF_DATA
323         then 
324             ok
325             cat << END_OF_MSG
326
327 PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL USERS!
328 This is done (after starting database) with:
329
330 /usr/bin/mysqladmin -u mysql password 'password'
331 /usr/bin/mysqladmin -h $hostname -u mysql password 'password'
332 /usr/bin/mysqladmin -u mysql_logrotate password 'password'
333
334 NOTE: mysql_logrotate password should be placed to /etc/mysqld.conf in
335 mysqladmin section. See the manual for more instructions.
336
337 END_OF_MSG
338       else  
339             fail
340             cat << END_OF_MSG
341 Installation of grant tables FAILED!
342
343 Examine the logs in $MYSQL_DATA_DIR for more information.  You can
344 also try to start the mysqld demon with:
345
346 /usr/sbin/mysqld --skip-grant &
347
348 You can use the command line tool /usr/bin/mysql to connect to the mysql
349 database and look at the grant tables:
350
351 shell> /usr/bin/mysql -u mysql mysql
352 mysql> show tables
353
354 Try 'mysqld --help' if you have problems with paths. Setting on
355 logging in /etc/mysqld.conf gives you a log in /var/log/mysql/log that
356 may be helpful. The latest information about MySQL is available on the
357 web at http://www.mysql.com/.
358
359 Please check PLD ftp site for newer versions of this package.
360
361 Please consult the MySQL manual section: 'Problems running
362 mysql_install_db', and the manual section that describes problems on
363 your OS.  Another information source is the MySQL email archive.
364 Please check all of the above before mailing us!  And if you do mail
365 us, you MUST use the /usr/bin/mysqlbug script!  
366
367 END_OF_MSG
368
369         exit 1 
370         fi
371         ;;
372   *)
373         msg_usage "$0 {start|stop|init|restart|force-reload|status}"
374         exit 3
375 esac
376
377 exit $RETVAL
This page took 0.075903 seconds and 4 git commands to generate.