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 touch /var/lock/subsys/mysql
49 echo "Mysql already is running"
53 show Stopping mysql service
56 rm -f /var/state/mysql/mysqld.pid
57 rm -f /var/lock/subsys/mysql
67 # Check if not exist init database
68 if [ ! -d "$MYSQL_DATA_DIR/mysql" ]; then
69 show Creating privilege mysql tables
72 hostname=`hostname` # Install this too in the user table
74 # Check if hostname is valid
75 resolved=`/usr/bin/resolveip $hostname 2>&1`
77 resolved=`/usr/bin/resolveip localhost 2>&1`
81 echo "Sorry, the host '$hostname' could not be looked up."
82 echo "Please configure the 'hostname' command to return a correct hostname."
87 # Create database directories mysql & test
88 if test ! -d $MYSQL_DATA_DIR/mysql; then
89 install -d -m700 -o $MYSQL_USER $MYSQL_DATA_DIR/mysql;
92 #mysqld --bootstrap need whole (and only one) sql command in one line
94 create_db="$create_db CREATE TABLE db ( "
95 create_db="$create_db Host char(60) DEFAULT '' NOT NULL, "
96 create_db="$create_db Db char(32) DEFAULT '' NOT NULL,"
97 create_db="$create_db User char(16) DEFAULT '' NOT NULL, "
98 create_db="$create_db Select_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
99 create_db="$create_db Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
100 create_db="$create_db Update_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
101 create_db="$create_db Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
102 create_db="$create_db Create_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
103 create_db="$create_db Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
104 create_db="$create_db Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
105 create_db="$create_db References_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
106 create_db="$create_db Index_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
107 create_db="$create_db Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
108 create_db="$create_db PRIMARY KEY Host (Host,Db,User), "
109 create_db="$create_db KEY User (User)"
110 create_db="$create_db );"
113 create_host="$create_host CREATE TABLE host ( "
114 create_host="$create_host Host char(60) DEFAULT '' NOT NULL, "
115 create_host="$create_host Db char(32) DEFAULT '' NOT NULL, "
116 create_host="$create_host Select_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
117 create_host="$create_host Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
118 create_host="$create_host Update_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
119 create_host="$create_host Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
120 create_host="$create_host Create_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
121 create_host="$create_host Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
122 create_host="$create_host Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
123 create_host="$create_host References_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
124 create_host="$create_host Index_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
125 create_host="$create_host Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
126 create_host="$create_host PRIMARY KEY Host (Host,Db)"
127 create_host="$create_host );"
130 create_user="$create_user CREATE TABLE user ("
131 create_user="$create_user Host char(60) DEFAULT '' NOT NULL, "
132 create_user="$create_user User char(16) DEFAULT '' NOT NULL, "
133 create_user="$create_user Password char(16) DEFAULT '' NOT NULL, "
134 create_user="$create_user Select_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
135 create_user="$create_user Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
136 create_user="$create_user Update_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
137 create_user="$create_user Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
138 create_user="$create_user Create_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
139 create_user="$create_user Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
140 create_user="$create_user Reload_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
141 create_user="$create_user Shutdown_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
142 create_user="$create_user Process_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
143 create_user="$create_user File_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
144 create_user="$create_user Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
145 create_user="$create_user References_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
146 create_user="$create_user Index_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
147 create_user="$create_user Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL, "
148 create_user="$create_user PRIMARY KEY Host (Host,User)"
149 create_user="$create_user );"
152 create_func="$create_func CREATE TABLE func ( "
153 create_func="$create_func name char(64) DEFAULT '' NOT NULL, "
154 create_func="$create_func ret tinyint(1) DEFAULT '0' NOT NULL,"
155 create_func="$create_func dl char(128) DEFAULT '' NOT NULL, "
156 create_func="$create_func type enum ('function','aggregate') NOT NULL, "
157 create_func="$create_func PRIMARY KEY (name)"
158 create_func="$create_func ); "
161 create_tblpriv="$create_tblpriv CREATE TABLE tables_priv ( "
162 create_tblpriv="$create_tblpriv Host char(60) DEFAULT '' NOT NULL, "
163 create_tblpriv="$create_tblpriv Db char(60) DEFAULT '' NOT NULL, "
164 create_tblpriv="$create_tblpriv User char(16) DEFAULT '' NOT NULL,"
165 create_tblpriv="$create_tblpriv Table_name char(60) DEFAULT '' NOT NULL, "
166 create_tblpriv="$create_tblpriv Grantor char(77) DEFAULT '' NOT NULL, "
167 create_tblpriv="$create_tblpriv Timestamp timestamp(14), "
168 create_tblpriv="$create_tblpriv Table_priv set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter') DEFAULT '' NOT NULL, "
169 create_tblpriv="$create_tblpriv Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
170 create_tblpriv="$create_tblpriv PRIMARY KEY (Host,Db,User,Table_name), "
171 create_tblpriv="$create_tblpriv KEY Grantor (Grantor)"
172 create_tblpriv="$create_tblpriv );"
175 create_colpriv="$create_colpriv CREATE TABLE columns_priv ( "
176 create_colpriv="$create_colpriv Host char(60) DEFAULT '' NOT NULL, "
177 create_colpriv="$create_colpriv Db char(60) DEFAULT '' NOT NULL, "
178 create_colpriv="$create_colpriv User char(16) DEFAULT '' NOT NULL, "
179 create_colpriv="$create_colpriv Table_name char(60) DEFAULT '' NOT NULL, "
180 create_colpriv="$create_colpriv Column_name char(60) DEFAULT '' NOT NULL, "
181 create_colpriv="$create_colpriv Timestamp timestamp(14), "
182 create_colpriv="$create_colpriv Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL, "
183 create_colpriv="$create_colpriv PRIMARY KEY (Host,Db,User,Table_name,Column_name)"
184 create_colpriv="$create_colpriv );"
186 if /usr/sbin/mysqld --bootstrap --skip-grant-tables \
187 --basedir=/usr --datadir=$MYSQL_DATA_DIR --user=$MYSQL_USER << END_OF_DATA
198 INSERT INTO db VALUES ('%','test','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y');
199 INSERT INTO db VALUES ('%','test\_%','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y');
200 INSERT INTO user VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
201 INSERT INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
202 REPLACE INTO user VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
203 REPLACE INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
204 INSERT INTO user VALUES ('localhost','','','N','N','N','N','N','N','N','N','N','N','N','N','N','N');
205 INSERT INTO user VALUES ('$hostname','','','N','N','N','N','N','N','N','N','N','N','N','N','N','N');
211 echo " PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !"
212 echo " This is done with:"
213 echo " /usr/bin/mysqladmin -u root password 'new-password'"
214 echo " See the manual for more instructions."
219 echo " Installation of grant tables failed!"
221 echo " Examine the logs in $MYSQL_DATA_DIR for more information."
222 echo " You can also try to start the mysqld demon with:"
223 echo " /usr/sbin/mysqld --skip-grant &"
224 echo " You can use the command line tool"
225 echo " /usr/bin/mysql to connect to the mysql"
226 echo " database and look at the grant tables:"
228 echo "shell> /usr/bin/mysql -u root mysql"
229 echo "mysql> show tables"
231 echo "Try 'mysqld --help' if you have problems with paths. Using --log"
232 echo "gives you a log in $MYSQL_DATA_DIR that may be helpful."
234 echo "The latest information about MySQL is available on the web at"
235 echo "http://www.mysql.com"
236 echo "Please consult the MySQL manual section: 'Problems running mysql_install_db',"
237 echo "and the manual section that describes problems on your OS."
238 echo "Another information source is the MySQL email archive."
239 echo "Please check all of the above before mailing us!"
240 echo "And if you do mail us, you MUST use the /usr/bin/mysqlbug script!"
244 echo "Semms that database is initialized now. Remove by hand $MYSQL_DATA_DIR/mysql"
245 echo "before initialize database.".
249 echo "Usage: mysql {start|stop|status|restart|reload|init}"