home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 May / INTERNET103.ISO / pc / software / windows / building / mysql / data1.cab / Development / scripts / mysql_install_db < prev    next >
Encoding:
Text File  |  2003-01-22  |  12.8 KB  |  374 lines

  1. #!/bin/sh
  2. # Copyright (C) 1997, 1998, 1999 TCX DataKonsult AB & Monty Program KB & Detron HB
  3. # For a more info consult the file COPYRIGHT distributed with this file.
  4.  
  5. # This scripts creates the privilege tables db, host, user, tables_priv,
  6. # columns_priv in the mysql database, as well as the func table.
  7. #
  8. # All unrecognized arguments to this script are passed to mysqld.
  9.  
  10. IN_RPM=0
  11. case "$1" in
  12.     -IN-RPM)
  13.       IN_RPM="1"; shift
  14.       ;;
  15. esac
  16. defaults=
  17. case "$1" in
  18.     --no-defaults|--defaults-file=*|--defaults-extra-file=*)
  19.       defaults="$1"; shift
  20.       ;;
  21. esac
  22.  
  23. parse_arguments() {
  24.   # We only need to pass arguments through to the server if we don't
  25.   # handle them here.  So, we collect unrecognized options (passed on
  26.   # the command line) into the args variable.
  27.   pick_args=
  28.   if test "$1" = PICK-ARGS-FROM-ARGV
  29.   then
  30.     pick_args=1
  31.     shift
  32.   fi
  33.  
  34.   for arg do
  35.     case "$arg" in
  36.       --force) force=1 ;;
  37.       --basedir=*) basedir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  38.       --ldata=*|--datadir=*) ldata=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  39.       --user=*) user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  40.       *)
  41.         if test -n "$pick_args"
  42.         then
  43.           # This sed command makes sure that any special chars are quoted,
  44.           # so the arg gets passed exactly to the server.
  45.           args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
  46.         fi
  47.         ;;
  48.     esac
  49.   done
  50. }
  51.  
  52. # Get first arguments from the my.cfg file, groups [mysqld] and
  53. # [mysql_install_db], and then merge with the command line arguments
  54. if test -x ./bin/my_print_defaults
  55. then
  56.   print_defaults="./bin/my_print_defaults"
  57. elif test -x /usr/local/mysql/bin/my_print_defaults
  58. then
  59.   print_defaults="/usr/local/mysql/bin/my_print_defaults"
  60. elif test -x /usr/local/mysql/bin/mysql_print_defaults
  61. then
  62.   print_defaults="/usr/local/mysql/bin/mysql_print_defaults"
  63. else
  64.   print_defaults="my_print_defaults"
  65. fi
  66.  
  67. args=
  68. ldata=
  69. execdir=
  70. bindir=
  71. basedir=
  72. force=0
  73. parse_arguments `$print_defaults $defaults mysqld mysql_install_db`
  74. parse_arguments PICK-ARGS-FROM-ARGV "$@"
  75.  
  76. test -z "$ldata" && ldata=/usr/local/mysql/var
  77. if test -z "$basedir"
  78. then
  79.   basedir=/usr/local/mysql
  80.   bindir=/usr/local/mysql/bin
  81.   execdir=/usr/local/mysql/libexec 
  82. else
  83.   bindir="$basedir/bin"
  84. if test -x "$basedir/libexec/mysqld"
  85. then
  86.   execdir="$basedir/libexec"
  87. elif test -x "/usr/local/mysql/libexec/mysqld"
  88. then
  89.   execdir="/usr/local/mysql/libexec"
  90. else
  91.   execdir="$basedir/bin"
  92. fi
  93. fi
  94.  
  95. mdata=$ldata/mysql
  96.  
  97. if test ! -x $execdir/mysqld
  98. then
  99.   if test "$IN_RPM" -eq 1
  100.   then
  101.     echo "FATAL ERROR $execdir/mysqld not found!"
  102.     exit 1
  103.   else
  104.     echo "Didn't find $execdir/mysqld"
  105.     echo "You should do a 'make install' before executing this script"
  106.     exit 1
  107.   fi
  108. fi
  109.  
  110. hostname=`/bin/hostname`        # Install this too in the user table
  111.  
  112. # Check if hostname is valid
  113. if test "$IN_RPM" -eq 0 -a $force -eq 0
  114. then
  115.   resolved=`$bindir/resolveip $hostname 2>&1`
  116.   if [ $? -ne 0 ]
  117.   then
  118.     resolved=`$bindir/resolveip localhost 2>&1`
  119.     if [ $? -eq 0 ]
  120.     then
  121.       echo "Sorry, the host '$hostname' could not be looked up."
  122.       echo "Please configure the 'hostname' command to return a correct hostname."
  123.       echo "If you want to solve this at a later stage, restart this script with"
  124.       echo "the --force option"
  125.       exit 1
  126.     fi
  127.     echo "WARNING: The host '$hostname' could not be looked up with resolveip."
  128.     echo "This probably means that your libc libraries are not 100 % compatible"
  129.     echo "with this binary MySQL version. The MySQL deamon, mysqld, should work"
  130.     echo "normally with the exception that host name resolving will not work."
  131.     echo "This means that you should use IP addresses instead of hostnames"
  132.     echo "when specifying MySQL privileges !"
  133.   fi
  134. fi
  135.  
  136. # Create database directories mysql & test
  137. if test "$IN_RPM" -eq 0
  138. then
  139.   if test ! -d $ldata; then mkdir $ldata; chmod 700 $ldata ; fi
  140.   if test ! -d $ldata/mysql; then mkdir $ldata/mysql;  chmod 700 $ldata/mysql ; fi
  141.   if test ! -d $ldata/test; then mkdir $ldata/test;  chmod 700 $ldata/test ; fi
  142.   if test -w / -a ! -z "$user"; then
  143.     chown $user $ldata $ldata/mysql $ldata/test;
  144.   fi
  145. fi
  146.  
  147. # Initialize variables
  148. c_d="" i_d=""
  149. c_h="" i_h=""
  150. c_u="" i_u=""
  151. c_f="" i_f=""
  152. c_t="" c_c=""
  153.  
  154. # Check for old tables
  155. if test ! -f $mdata/db.frm
  156. then
  157.   echo "Preparing db table"
  158.  
  159.   # mysqld --bootstrap wants one command/line
  160.   c_d="$c_d CREATE TABLE db ("
  161.   c_d="$c_d   Host char(60) binary DEFAULT '' NOT NULL,"
  162.   c_d="$c_d   Db char(64) binary DEFAULT '' NOT NULL,"
  163.   c_d="$c_d   User char(16) binary DEFAULT '' NOT NULL,"
  164.   c_d="$c_d   Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  165.   c_d="$c_d   Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  166.   c_d="$c_d   Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  167.   c_d="$c_d   Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  168.   c_d="$c_d   Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  169.   c_d="$c_d   Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  170.   c_d="$c_d   Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  171.   c_d="$c_d   References_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  172.   c_d="$c_d   Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  173.   c_d="$c_d   Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  174.   c_d="$c_d PRIMARY KEY Host (Host,Db,User),"
  175.   c_d="$c_d KEY User (User)"
  176.   c_d="$c_d )"
  177.   c_d="$c_d comment='Database privileges';"
  178.   
  179.   i_d="INSERT INTO db VALUES ('%','test','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y');
  180.   INSERT INTO db VALUES ('%','test\_%','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y');"
  181. fi
  182.  
  183. if test ! -f $mdata/host.frm
  184. then
  185.   echo "Preparing host table"
  186.  
  187.   c_h="$c_h CREATE TABLE host ("
  188.   c_h="$c_h  Host char(60) binary DEFAULT '' NOT NULL,"
  189.   c_h="$c_h  Db char(64) binary DEFAULT '' NOT NULL,"
  190.   c_h="$c_h  Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  191.   c_h="$c_h  Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  192.   c_h="$c_h  Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  193.   c_h="$c_h  Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  194.   c_h="$c_h  Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  195.   c_h="$c_h  Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  196.   c_h="$c_h  Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  197.   c_h="$c_h  References_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  198.   c_h="$c_h  Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  199.   c_h="$c_h  Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  200.   c_h="$c_h  PRIMARY KEY Host (Host,Db)"
  201.   c_h="$c_h )"
  202.   c_h="$c_h comment='Host privileges;  Merged with database privileges';"
  203. fi
  204.  
  205. if test ! -f $mdata/user.frm
  206. then
  207.   echo "Preparing user table"
  208.  
  209.   c_u="$c_u CREATE TABLE user ("
  210.   c_u="$c_u   Host char(60) binary DEFAULT '' NOT NULL,"
  211.   c_u="$c_u   User char(16) binary DEFAULT '' NOT NULL,"
  212.   c_u="$c_u   Password char(16) binary DEFAULT '' NOT NULL,"
  213.   c_u="$c_u   Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  214.   c_u="$c_u   Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  215.   c_u="$c_u   Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  216.   c_u="$c_u   Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  217.   c_u="$c_u   Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  218.   c_u="$c_u   Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  219.   c_u="$c_u   Reload_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  220.   c_u="$c_u   Shutdown_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  221.   c_u="$c_u   Process_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  222.   c_u="$c_u   File_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  223.   c_u="$c_u   Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  224.   c_u="$c_u   References_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  225.   c_u="$c_u   Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  226.   c_u="$c_u   Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  227.   c_u="$c_u   PRIMARY KEY Host (Host,User)"
  228.   c_u="$c_u )"
  229.   c_u="$c_u comment='Users and global privileges';"
  230.  
  231.   i_u="INSERT INTO user VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
  232.   INSERT INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
  233.   
  234.   REPLACE INTO user VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
  235.   REPLACE INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
  236.   
  237.   INSERT INTO user VALUES ('localhost','','','N','N','N','N','N','N','N','N','N','N','N','N','N','N');
  238.   INSERT INTO user VALUES ('$hostname','','','N','N','N','N','N','N','N','N','N','N','N','N','N','N');"
  239. fi
  240.  
  241. if test ! -f $mdata/func.frm
  242. then
  243.   echo "Preparing func table"
  244.  
  245.   c_f="$c_f CREATE TABLE func ("
  246.   c_f="$c_f   name char(64) binary DEFAULT '' NOT NULL,"
  247.   c_f="$c_f   ret tinyint(1) DEFAULT '0' NOT NULL,"
  248.   c_f="$c_f   dl char(128) DEFAULT '' NOT NULL,"
  249.   c_f="$c_f   type enum ('function','aggregate') NOT NULL,"
  250.   c_f="$c_f   PRIMARY KEY (name)"
  251.   c_f="$c_f )"
  252.   c_f="$c_f   comment='User defined functions';"
  253. fi
  254.  
  255. if test ! -f $mdata/tables_priv.frm
  256. then
  257.   echo "Preparing tables_priv table"
  258.  
  259.   c_t="$c_t CREATE TABLE tables_priv ("
  260.   c_t="$c_t   Host char(60) binary DEFAULT '' NOT NULL,"
  261.   c_t="$c_t   Db char(64) binary DEFAULT '' NOT NULL,"
  262.   c_t="$c_t   User char(16) binary DEFAULT '' NOT NULL,"
  263.   c_t="$c_t   Table_name char(60) binary DEFAULT '' NOT NULL,"
  264.   c_t="$c_t   Grantor char(77) DEFAULT '' NOT NULL,"
  265.   c_t="$c_t   Timestamp timestamp(14),"
  266.   c_t="$c_t   Table_priv set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter') DEFAULT '' NOT NULL,"
  267.   c_t="$c_t   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
  268.   c_t="$c_t   PRIMARY KEY (Host,Db,User,Table_name),"
  269.   c_t="$c_t   KEY Grantor (Grantor)"
  270.   c_t="$c_t )"
  271.   c_t="$c_t   comment='Table privileges';"
  272. fi
  273.  
  274. if test ! -f $mdata/columns_priv.frm
  275. then
  276.   echo "Preparing columns_priv table"
  277.  
  278.   c_c="$c_c CREATE TABLE columns_priv ("
  279.   c_c="$c_c   Host char(60) binary DEFAULT '' NOT NULL,"
  280.   c_c="$c_c   Db char(64) binary DEFAULT '' NOT NULL,"
  281.   c_c="$c_c   User char(16) binary DEFAULT '' NOT NULL,"
  282.   c_c="$c_c   Table_name char(64) binary DEFAULT '' NOT NULL,"
  283.   c_c="$c_c   Column_name char(64) binary DEFAULT '' NOT NULL,"
  284.   c_c="$c_c   Timestamp timestamp(14),"
  285.   c_c="$c_c   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
  286.   c_c="$c_c   PRIMARY KEY (Host,Db,User,Table_name,Column_name)"
  287.   c_c="$c_c )"
  288.   c_c="$c_c   comment='Column privileges';"
  289. fi
  290.  
  291. echo "Installing all prepared tables"
  292. if eval "$execdir/mysqld $defaults --bootstrap --skip-grant-tables \
  293.          --basedir=$basedir --datadir=$ldata --skip-innodb --skip-gemini --skip-bdb $args" << END_OF_DATA
  294. use mysql;
  295. $c_d
  296. $i_d
  297.  
  298. $c_h
  299. $i_h
  300.  
  301. $c_u
  302. $i_u
  303.  
  304. $c_f
  305. $i_f
  306.  
  307. $c_t
  308. $c_c
  309. END_OF_DATA
  310. then
  311.   echo ""
  312.   if test "$IN_RPM" -eq 0
  313.   then
  314.     echo "To start mysqld at boot time you have to copy support-files/mysql.server"
  315.     echo "to the right place for your system"
  316.     echo
  317.   fi
  318.   echo "PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !"
  319.   echo "This is done with:"
  320.   echo "$bindir/mysqladmin -u root  password 'new-password'"
  321.   echo "$bindir/mysqladmin -u root -h $hostname  password 'new-password'"
  322.   echo "See the manual for more instructions."
  323.   #
  324.   # Print message about upgrading unless we have created a new db table.
  325.   if test -z "$c_d"
  326.   then
  327.     echo
  328.     echo "NOTE:  If you are upgrading from a MySQL <= 3.22.10 you should run"
  329.     echo "the $bindir/mysql_fix_privilege_tables. Otherwise you will not be"
  330.     echo "able to use the new GRANT command!"
  331.   fi
  332.   echo
  333.   if test "$IN_RPM" -eq 0
  334.   then
  335.     echo "You can start the MySQL daemon with:"
  336.     echo "cd /usr/local/mysql ; $bindir/safe_mysqld &"
  337.     echo
  338.     echo "You can test the MySQL daemon with the benchmarks in the 'sql-bench' directory:"
  339.     echo "cd sql-bench ; run-all-tests"
  340.     echo
  341.   fi
  342.   echo "Please report any problems with the /usr/local/mysql/bin/mysqlbug script!"
  343.   echo
  344.   echo "The latest information about MySQL is available on the web at"
  345.   echo "http://www.mysql.com"
  346.   echo "Support MySQL by buying support/licenses at https://order.mysql.com"
  347.   echo 
  348.   exit 0
  349. else
  350.   echo "Installation of grant tables failed!"
  351.   echo
  352.   echo "Examine the logs in $ldata for more information."
  353.   echo "You can also try to start the mysqld daemon with:"
  354.   echo "$execdir/mysqld --skip-grant &"
  355.   echo "You can use the command line tool"
  356.   echo "$bindir/mysql to connect to the mysql"
  357.   echo "database and look at the grant tables:"
  358.   echo
  359.   echo "shell> $bindir/mysql -u root mysql"
  360.   echo "mysql> show tables"
  361.   echo
  362.   echo "Try 'mysqld --help' if you have problems with paths. Using --log"
  363.   echo "gives you a log in $ldata that may be helpful."
  364.   echo
  365.   echo "The latest information about MySQL is available on the web at"
  366.   echo "http://www.mysql.com"
  367.   echo "Please consult the MySQL manual section: 'Problems running mysql_install_db',"
  368.   echo "and the manual section that describes problems on your OS."
  369.   echo "Another information source is the MySQL email archive."
  370.   echo "Please check all of the above before mailing us!"
  371.   echo "And if you do mail us, you MUST use the /usr/local/mysql/bin/mysqlbug script!"
  372.   exit 1
  373. fi
  374.