home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 Autumn / INTERNET109.ISO / pc / software / windows / building / mysql / data1.cab / Development / scripts / mysql_install_db.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  2003-08-03  |  13.8 KB  |  392 lines

  1. #!/bin/sh
  2. # Copyright (C) 2002 MySQL AB
  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 @bindir@/my_print_defaults
  58. then
  59.   print_defaults="@bindir@/my_print_defaults"
  60. elif test -x @bindir@/mysql_print_defaults
  61. then
  62.   print_defaults="@bindir@/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=@localstatedir@
  77. if test -z "$basedir"
  78. then
  79.   basedir=@prefix@
  80.   bindir=@bindir@
  81.   execdir=@libexecdir@ 
  82. else
  83.   bindir="$basedir/bin"
  84. if test -x "$basedir/libexec/mysqld"
  85. then
  86.   execdir="$basedir/libexec"
  87. elif test -x "@libexecdir@/mysqld"
  88. then
  89.   execdir="@libexecdir@"
  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=`@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   Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  175.   c_d="$c_d   Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  176.   c_d="$c_d PRIMARY KEY Host (Host,Db,User),"
  177.   c_d="$c_d KEY User (User)"
  178.   c_d="$c_d )"
  179.   c_d="$c_d comment='Database privileges';"
  180.   
  181.   i_d="INSERT INTO db VALUES ('%','test','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y','Y','Y');
  182.   INSERT INTO db VALUES ('%','test\_%','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y','Y','Y');"
  183. fi
  184.  
  185. if test ! -f $mdata/host.frm
  186. then
  187.   echo "Preparing host table"
  188.  
  189.   c_h="$c_h CREATE TABLE host ("
  190.   c_h="$c_h  Host char(60) binary DEFAULT '' NOT NULL,"
  191.   c_h="$c_h  Db char(64) binary DEFAULT '' NOT NULL,"
  192.   c_h="$c_h  Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  193.   c_h="$c_h  Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  194.   c_h="$c_h  Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  195.   c_h="$c_h  Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  196.   c_h="$c_h  Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  197.   c_h="$c_h  Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  198.   c_h="$c_h  Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  199.   c_h="$c_h  References_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  200.   c_h="$c_h  Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  201.   c_h="$c_h  Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  202.   c_h="$c_h  Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  203.   c_h="$c_h  Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  204.   c_h="$c_h  PRIMARY KEY Host (Host,Db)"
  205.   c_h="$c_h )"
  206.   c_h="$c_h comment='Host privileges;  Merged with database privileges';"
  207. fi
  208.  
  209. if test ! -f $mdata/user.frm
  210. then
  211.   echo "Preparing user table"
  212.  
  213.   c_u="$c_u CREATE TABLE user ("
  214.   c_u="$c_u   Host char(60) binary DEFAULT '' NOT NULL,"
  215.   c_u="$c_u   User char(16) binary DEFAULT '' NOT NULL,"
  216.   c_u="$c_u   Password char(16) binary DEFAULT '' NOT NULL,"
  217.   c_u="$c_u   Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  218.   c_u="$c_u   Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  219.   c_u="$c_u   Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  220.   c_u="$c_u   Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  221.   c_u="$c_u   Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  222.   c_u="$c_u   Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  223.   c_u="$c_u   Reload_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  224.   c_u="$c_u   Shutdown_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  225.   c_u="$c_u   Process_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  226.   c_u="$c_u   File_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  227.   c_u="$c_u   Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  228.   c_u="$c_u   References_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  229.   c_u="$c_u   Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  230.   c_u="$c_u   Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  231.   c_u="$c_u   Show_db_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  232.   c_u="$c_u   Super_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  233.   c_u="$c_u   Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  234.   c_u="$c_u   Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  235.   c_u="$c_u   Execute_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  236.   c_u="$c_u   Repl_slave_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  237.   c_u="$c_u   Repl_client_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  238.   c_u="$c_u   ssl_type enum('','ANY','X509', 'SPECIFIED') DEFAULT '' NOT NULL,"
  239.   c_u="$c_u   ssl_cipher BLOB NOT NULL,"
  240.   c_u="$c_u   x509_issuer BLOB NOT NULL,"
  241.   c_u="$c_u   x509_subject BLOB NOT NULL,"
  242.   c_u="$c_u   max_questions int(11) unsigned DEFAULT 0  NOT NULL,"
  243.   c_u="$c_u   max_updates int(11) unsigned DEFAULT 0  NOT NULL,"
  244.   c_u="$c_u   max_connections int(11) unsigned DEFAULT 0  NOT NULL,"
  245.   c_u="$c_u   PRIMARY KEY Host (Host,User)"
  246.   c_u="$c_u )"
  247.   c_u="$c_u comment='Users and global privileges';"
  248.  
  249.   i_u="INSERT INTO user VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
  250.   INSERT INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
  251.   
  252.   REPLACE INTO user VALUES ('localhost','root','','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.   REPLACE INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
  254.   
  255.   INSERT INTO user (host,user) values ('localhost','');
  256.   INSERT INTO user (host,user) values ('$hostname','');"
  257. fi
  258.  
  259. if test ! -f $mdata/func.frm
  260. then
  261.   echo "Preparing func table"
  262.  
  263.   c_f="$c_f CREATE TABLE func ("
  264.   c_f="$c_f   name char(64) binary DEFAULT '' NOT NULL,"
  265.   c_f="$c_f   ret tinyint(1) DEFAULT '0' NOT NULL,"
  266.   c_f="$c_f   dl char(128) DEFAULT '' NOT NULL,"
  267.   c_f="$c_f   type enum ('function','aggregate') NOT NULL,"
  268.   c_f="$c_f   PRIMARY KEY (name)"
  269.   c_f="$c_f )"
  270.   c_f="$c_f   comment='User defined functions';"
  271. fi
  272.  
  273. if test ! -f $mdata/tables_priv.frm
  274. then
  275.   echo "Preparing tables_priv table"
  276.  
  277.   c_t="$c_t CREATE TABLE tables_priv ("
  278.   c_t="$c_t   Host char(60) binary DEFAULT '' NOT NULL,"
  279.   c_t="$c_t   Db char(64) binary DEFAULT '' NOT NULL,"
  280.   c_t="$c_t   User char(16) binary DEFAULT '' NOT NULL,"
  281.   c_t="$c_t   Table_name char(60) binary DEFAULT '' NOT NULL,"
  282.   c_t="$c_t   Grantor char(77) DEFAULT '' NOT NULL,"
  283.   c_t="$c_t   Timestamp timestamp(14),"
  284.   c_t="$c_t   Table_priv set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter') DEFAULT '' NOT NULL,"
  285.   c_t="$c_t   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
  286.   c_t="$c_t   PRIMARY KEY (Host,Db,User,Table_name),"
  287.   c_t="$c_t   KEY Grantor (Grantor)"
  288.   c_t="$c_t )"
  289.   c_t="$c_t   comment='Table privileges';"
  290. fi
  291.  
  292. if test ! -f $mdata/columns_priv.frm
  293. then
  294.   echo "Preparing columns_priv table"
  295.  
  296.   c_c="$c_c CREATE TABLE columns_priv ("
  297.   c_c="$c_c   Host char(60) binary DEFAULT '' NOT NULL,"
  298.   c_c="$c_c   Db char(64) binary DEFAULT '' NOT NULL,"
  299.   c_c="$c_c   User char(16) binary DEFAULT '' NOT NULL,"
  300.   c_c="$c_c   Table_name char(64) binary DEFAULT '' NOT NULL,"
  301.   c_c="$c_c   Column_name char(64) binary DEFAULT '' NOT NULL,"
  302.   c_c="$c_c   Timestamp timestamp(14),"
  303.   c_c="$c_c   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
  304.   c_c="$c_c   PRIMARY KEY (Host,Db,User,Table_name,Column_name)"
  305.   c_c="$c_c )"
  306.   c_c="$c_c   comment='Column privileges';"
  307. fi
  308.  
  309. echo "Installing all prepared tables"
  310. if eval "$execdir/mysqld $defaults --bootstrap --skip-grant-tables \
  311.          --basedir=$basedir --datadir=$ldata --skip-innodb --skip-bdb $args" << END_OF_DATA
  312. use mysql;
  313. $c_d
  314. $i_d
  315.  
  316. $c_h
  317. $i_h
  318.  
  319. $c_u
  320. $i_u
  321.  
  322. $c_f
  323. $i_f
  324.  
  325. $c_t
  326. $c_c
  327. END_OF_DATA
  328. then
  329.   echo ""
  330.   if test "$IN_RPM" -eq 0
  331.   then
  332.     echo "To start mysqld at boot time you have to copy support-files/mysql.server"
  333.     echo "to the right place for your system"
  334.     echo
  335.   fi
  336.   echo "PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !"
  337.   echo "This is done with:"
  338.   echo "$bindir/mysqladmin -u root password 'new-password'"
  339.   echo "$bindir/mysqladmin -u root -h $hostname password 'new-password'"
  340.   echo "See the manual for more instructions."
  341.   #
  342.   # Print message about upgrading unless we have created a new db table.
  343.   if test -z "$c_d"
  344.   then
  345.     echo
  346.     echo "NOTE:  If you are upgrading from a MySQL <= 3.22.10 you should run"
  347.     echo "the $bindir/mysql_fix_privilege_tables. Otherwise you will not be"
  348.     echo "able to use the new GRANT command!"
  349.   fi
  350.   echo
  351.   if test "$IN_RPM" -eq 0
  352.   then
  353.     echo "You can start the MySQL daemon with:"
  354.     echo "cd @prefix@ ; $bindir/mysqld_safe &"
  355.     echo
  356.     echo "You can test the MySQL daemon with the benchmarks in the 'sql-bench' directory:"
  357.     echo "cd sql-bench ; perl run-all-tests"
  358.     echo
  359.   fi
  360.   echo "Please report any problems with the @scriptdir@/mysqlbug script!"
  361.   echo
  362.   echo "The latest information about MySQL is available on the web at"
  363.   echo "http://www.mysql.com"
  364.   echo "Support MySQL by buying support/licenses at https://order.mysql.com"
  365.   echo 
  366.   exit 0
  367. else
  368.   echo "Installation of grant tables failed!"
  369.   echo
  370.   echo "Examine the logs in $ldata for more information."
  371.   echo "You can also try to start the mysqld daemon with:"
  372.   echo "$execdir/mysqld --skip-grant &"
  373.   echo "You can use the command line tool"
  374.   echo "$bindir/mysql to connect to the mysql"
  375.   echo "database and look at the grant tables:"
  376.   echo
  377.   echo "shell> $bindir/mysql -u root mysql"
  378.   echo "mysql> show tables"
  379.   echo
  380.   echo "Try 'mysqld --help' if you have problems with paths. Using --log"
  381.   echo "gives you a log in $ldata that may be helpful."
  382.   echo
  383.   echo "The latest information about MySQL is available on the web at"
  384.   echo "http://www.mysql.com"
  385.   echo "Please consult the MySQL manual section: 'Problems running mysql_install_db',"
  386.   echo "and the manual section that describes problems on your OS."
  387.   echo "Another information source is the MySQL email archive."
  388.   echo "Please check all of the above before mailing us!"
  389.   echo "And if you do mail us, you MUST use the @scriptdir@/mysqlbug script!"
  390.   exit 1
  391. fi
  392.