3 # mysql A very fast and reliable SQL database engine
5 # chkconfig: 2345 90 90
7 # description: A very fast and reliable SQL database engine.
9 # pidfile: /var/state/mysql/mysqld.pid
11 # Source function library
12 . /etc/rc.d/init.d/functions
15 . /etc/sysconfig/network
17 MYSQL_DATA_DIR=/var/state/mysql
21 if [ -f /etc/sysconfig/mysql ] ; then
22 . /etc/sysconfig/mysql
23 if [ -z "${MYSQL_DATA_DIR}" ]; then
24 MYSQL_DATA_DIR=/var/state/mysql
28 # Check that networking is up.
29 if [ "${NETWORKING}" = "no" ]; then
30 echo "WARNING: Networking is down. Mysql service can't be runed."
36 # Check if the service is already running?
37 if [ ! -d "$MYSQL_DATA_DIR/mysql" ]; then
38 echo "Mysq database not initialized. Try \"$0 init\" before start."
41 if [ ! -f /var/lock/subsys/mysql ]; then
44 /usr/sbin/mysqld -u $MYSQL_USER --datadir=$MYSQL_DATA_DIR >>/var/log/mysql.log 2>&1 &
47 if ps -C mysqld >/dev/null 2>&1; then
52 touch /var/lock/subsys/mysql
54 echo "Mysql already is running"
58 show Stopping mysql service
61 rm -f /var/state/mysql/mysqld.pid
62 rm -f /var/lock/subsys/mysql
72 # Check if not exist init database
73 if [ ! -d "$MYSQL_DATA_DIR/mysql" ]; then
74 show Creating privilege mysql tables
77 hostname=`hostname` # Install this too in the user table
79 # Check if hostname is valid
80 resolved=`/usr/bin/resolveip $hostname 2>&1`
82 resolved=`/usr/bin/resolveip localhost 2>&1`
86 echo "Sorry, the host '$hostname' could not be looked up."
87 echo "Please configure the 'hostname' command to return a correct hostname."
92 # Create database directories mysql & test
93 if test ! -d $MYSQL_DATA_DIR/mysql; then
94 install -d -m700 -o $MYSQL_USER $MYSQL_DATA_DIR/mysql;
97 #mysqld --bootstrap need whole (and only one) sql command in one line
99 create_db="$create_db CREATE TABLE db ( "
100 create_db="$create_db Host char(60) DEFAULT '' NOT NULL, "
101 create_db="$create_db Db char(32) DEFAULT '' NOT NULL,"
102 create_db="$create_db User char(16) DEFAULT '' NOT NULL, "
103 create_db="$create_db Select_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
104 create_db="$create_db Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
105 create_db="$create_db Update_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
106 create_db="$create_db Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
107 create_db="$create_db Create_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
108 create_db="$create_db Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
109 create_db="$create_db Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
110 create_db="$create_db References_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
111 create_db="$create_db Index_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
112 create_db="$create_db Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
113 create_db="$create_db PRIMARY KEY Host (Host,Db,User), "
114 create_db="$create_db KEY User (User)"
115 create_db="$create_db );"
118 create_host="$create_host CREATE TABLE host ( "
119 create_host="$create_host Host char(60) DEFAULT '' NOT NULL, "
120 create_host="$create_host Db char(32) DEFAULT '' NOT NULL, "
121 create_host="$create_host Select_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
122 create_host="$create_host Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
123 create_host="$create_host Update_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
124 create_host="$create_host Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
125 create_host="$create_host Create_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
126 create_host="$create_host Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
127 create_host="$create_host Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
128 create_host="$create_host References_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
129 create_host="$create_host Index_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
130 create_host="$create_host Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
131 create_host="$create_host PRIMARY KEY Host (Host,Db)"
132 create_host="$create_host );"
135 create_user="$create_user CREATE TABLE user ("
136 create_user="$create_user Host char(60) DEFAULT '' NOT NULL, "
137 create_user="$create_user User char(16) DEFAULT '' NOT NULL, "
138 create_user="$create_user Password char(16) DEFAULT '' NOT NULL, "
139 create_user="$create_user Select_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
140 create_user="$create_user Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
141 create_user="$create_user Update_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
142 create_user="$create_user Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
143 create_user="$create_user Create_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
144 create_user="$create_user Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
145 create_user="$create_user Reload_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
146 create_user="$create_user Shutdown_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
147 create_user="$create_user Process_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
148 create_user="$create_user File_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
149 create_user="$create_user Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
150 create_user="$create_user References_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
151 create_user="$create_user Index_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
152 create_user="$create_user Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
153 create_user="$create_user PRIMARY KEY Host (Host,User)"
154 create_user="$create_user );"
157 create_func="$create_func CREATE TABLE func ( "
158 create_func="$create_func name char(64) DEFAULT '' NOT NULL, "
159 create_func="$create_func ret tinyint(1) DEFAULT '0' NOT NULL,"
160 create_func="$create_func dl char(128) DEFAULT '' NOT NULL, "
161 create_func="$create_func type enum ('function','aggregate') NOT NULL, "
162 create_func="$create_func PRIMARY KEY (name)"
163 create_func="$create_func ); "
166 create_tblpriv="$create_tblpriv CREATE TABLE tables_priv ( "
167 create_tblpriv="$create_tblpriv Host char(60) DEFAULT '' NOT NULL, "
168 create_tblpriv="$create_tblpriv Db char(60) DEFAULT '' NOT NULL, "
169 create_tblpriv="$create_tblpriv User char(16) DEFAULT '' NOT NULL,"
170 create_tblpriv="$create_tblpriv Table_name char(60) DEFAULT '' NOT NULL, "
171 create_tblpriv="$create_tblpriv Grantor char(77) DEFAULT '' NOT NULL, "
172 create_tblpriv="$create_tblpriv Timestamp timestamp(14), "
173 create_tblpriv="$create_tblpriv Table_priv set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter') DEFAULT '' NOT NULL, "
174 create_tblpriv="$create_tblpriv Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
175 create_tblpriv="$create_tblpriv PRIMARY KEY (Host,Db,User,Table_name), "
176 create_tblpriv="$create_tblpriv KEY Grantor (Grantor)"
177 create_tblpriv="$create_tblpriv );"
180 create_colpriv="$create_colpriv CREATE TABLE columns_priv ( "
181 create_colpriv="$create_colpriv Host char(60) DEFAULT '' NOT NULL, "
182 create_colpriv="$create_colpriv Db char(60) DEFAULT '' NOT NULL, "
183 create_colpriv="$create_colpriv User char(16) DEFAULT '' NOT NULL, "
184 create_colpriv="$create_colpriv Table_name char(60) DEFAULT '' NOT NULL, "
185 create_colpriv="$create_colpriv Column_name char(60) DEFAULT '' NOT NULL, "
186 create_colpriv="$create_colpriv Timestamp timestamp(14), "
187 create_colpriv="$create_colpriv Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL, "
188 create_colpriv="$create_colpriv PRIMARY KEY (Host,Db,User,Table_name,Column_name)"
189 create_colpriv="$create_colpriv );"
191 if /usr/sbin/mysqld --bootstrap --skip-grant-tables \
192 --basedir=/usr --datadir=$MYSQL_DATA_DIR --user=$MYSQL_USER << END_OF_DATA
203 INSERT INTO db VALUES ('%','test','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y');
204 INSERT INTO db VALUES ('%','test\_%','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y');
205 INSERT INTO user VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
206 INSERT INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
207 REPLACE INTO user VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
208 REPLACE INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
209 INSERT INTO user VALUES ('localhost','','','N','N','N','N','N','N','N','N','N','N','N','N','N','N');
210 INSERT INTO user VALUES ('$hostname','','','N','N','N','N','N','N','N','N','N','N','N','N','N','N');
216 echo " PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !"
217 echo " This is done with:"
218 echo " /usr/bin/mysqladmin -u root password 'new-password'"
219 echo " See the manual for more instructions."
224 echo " Installation of grant tables failed!"
226 echo " Examine the logs in $MYSQL_DATA_DIR for more information."
227 echo " You can also try to start the mysqld demon with:"
228 echo " /usr/sbin/mysqld --skip-grant &"
229 echo " You can use the command line tool"
230 echo " /usr/bin/mysql to connect to the mysql"
231 echo " database and look at the grant tables:"
233 echo "shell> /usr/bin/mysql -u root mysql"
234 echo "mysql> show tables"
236 echo "Try 'mysqld --help' if you have problems with paths. Using --log"
237 echo "gives you a log in $MYSQL_DATA_DIR that may be helpful."
239 echo "The latest information about MySQL is available on the web at"
240 echo "http://www.mysql.com"
241 echo "Please consult the MySQL manual section: 'Problems running mysql_install_db',"
242 echo "and the manual section that describes problems on your OS."
243 echo "Another information source is the MySQL email archive."
244 echo "Please check all of the above before mailing us!"
245 echo "And if you do mail us, you MUST use the /usr/bin/mysqlbug script!"
249 echo "Semms that database is initialized now. Remove by hand $MYSQL_DATA_DIR/mysql"
250 echo "before initialize database.".
254 echo "Usage: mysql {start|stop|status|restart|reload|init}"