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