diff options
author | unknown <jcole@tetra.spaceapes.com> | 2001-08-09 22:58:45 -0500 |
---|---|---|
committer | unknown <jcole@tetra.spaceapes.com> | 2001-08-09 22:58:45 -0500 |
commit | 51bc6118a4ba799b059046bd4bb3c5e065b8d846 (patch) | |
tree | 3f4ed8b75390ce47f6ecdeb5b6a5abdcc55d4dae /Docs/manual.texi | |
parent | eff8ec494f54791b95e32d080a7ae14510b6b92e (diff) | |
download | mariadb-git-51bc6118a4ba799b059046bd4bb3c5e065b8d846.tar.gz |
DocTOC Chapter 9 Complete!
(Chapter 8 Skipped, will be next.)
Diffstat (limited to 'Docs/manual.texi')
-rw-r--r-- | Docs/manual.texi | 4930 |
1 files changed, 2512 insertions, 2418 deletions
diff --git a/Docs/manual.texi b/Docs/manual.texi index 685933b67af..b07e9a889de 100644 --- a/Docs/manual.texi +++ b/Docs/manual.texi @@ -115,15 +115,11 @@ distribution for that version. * MySQL Optimization:: * Reference:: @strong{MySQL} language reference * Table types:: @strong{MySQL} table types -* Maintenance:: Maintaining a @strong{MySQL} installation -* Adding functions:: Adding new functions to @strong{MySQL} -* Adding procedures:: Adding new procedures to @strong{MySQL} +* Extending MySQL:: * ODBC:: @strong{MySQL} ODBC Support * Common programs:: Using @strong{MySQL} with some common programs -* Problems:: Problems -* Common problems:: Solving some common problems with @strong{MySQL} * Clients:: @strong{MySQL} client tools and APIs -* MySQL internals:: @strong{MySQL} internals +* Problems:: Problems * Environment variables:: @strong{MySQL} environment variables * Users:: Some @strong{MySQL} users * MySQL customer usage:: @@ -746,7 +742,8 @@ memory leakage detector. @item Includes @code{myisamchk}, a very fast utility for table checking, optimization, and repair. All of the functionality of @code{myisamchk} -is also available through the SQL interface as well. @xref{Maintenance}. +is also available through the SQL interface as well. +@xref{MySQL Database Administration}. @item Full support for several different character sets, including @@ -2569,7 +2566,7 @@ upgrading to a newer version of @strong{MySQL}. @xref{News}. If you have a problem such that your data appears corrupt or you get errors when you access some particular table, you should first check and then try repairing your tables with @code{myisamchk} or @code{CHECK TABLE} and -@code{REPAIR TABLE}. @xref{Maintenance}. +@code{REPAIR TABLE}. @xref{MySQL Database Administration}. @item If you often get corrupted tables you should try to find out when and why this @@ -20573,7 +20570,7 @@ The list below briefly describes the @strong{MySQL} programs: @item myisamchk Utility to describe, check, optimize, and repair @strong{MySQL} tables. Because @code{myisamchk} has many functions, it is described in its own -chapter. @xref{Maintenance}. +chapter. @xref{MySQL Database Administration}. @cindex @code{make_binary_distribution} @item make_binary_distribution @@ -21435,6 +21432,7 @@ binaries includes: * Client-Side Overview:: * mysql:: * mysqladmin:: +* Using mysqlcheck:: * mysqldump:: * mysqlhotcopy:: * mysqlimport:: @@ -21501,7 +21499,7 @@ The list below briefly describes the @strong{MySQL} programs: @item myisamchk Utility to describe, check, optimize, and repair @strong{MySQL} tables. Because @code{myisamchk} has many functions, it is described in its own -chapter. @xref{Maintenance}. +chapter. @xref{MySQL Database Administration}. @cindex @code{make_binary_distribution} @item make_binary_distribution @@ -21924,7 +21922,7 @@ file, but want to be able to turn the feature off sometimes. @end itemize -@node mysqladmin, mysqldump, mysql, Client-Side Scripts +@node mysqladmin, Using mysqlcheck, mysql, Client-Side Scripts @subsection mysqladmin, Administrating a MySQL Server @cindex administration, server @@ -22044,7 +22042,131 @@ wait until the @strong{MySQL} @code{pid-file} is removed to ensure that the @code{mysqld} server has stopped properly. -@node mysqldump, mysqlhotcopy, mysqladmin, Client-Side Scripts +@node Using mysqlcheck, mysqldump, mysqladmin, Client-Side Scripts +@subsection Using @code{mysqlcheck} for Table Maintenance and Crash Recovery + +Since @strong{MySQL} version 3.23.38 you will be able to use a new +checking and repairing tool for @code{MyISAM} tables. The difference to +@code{myisamchk} is that @code{mysqlcheck} should be used when the +@code{mysqld} server is running, where as @code{myisamchk} should be used +when it is not. The benefit is that you no longer have to take the +server down for checking or repairing your tables. + +@code{mysqlcheck} uses @strong{MySQL} server commands @code{CHECK}, +@code{REPAIR}, @code{ANALYZE} and @code{OPTIMIZE} in a convenient way +for the user. + +There are three alternative ways to invoke @code{mysqlcheck}: + +@example +shell> mysqlcheck [OPTIONS] database [tables] +shell> mysqlcheck [OPTIONS] --databases DB1 [DB2 DB3...] +shell> mysqlcheck [OPTIONS] --all-databases +@end example + +So it can be used in a similar way as @code{mysqldump} when it +comes to what databases and tables you want to choose. + +@code{mysqlcheck} does have a special feature compared to the other +clients; the default behavior, checking tables (-c), can be changed by +renaming the binary. So if you want to have a tool that repairs tables +by default, you should just copy @code{mysqlcheck} to your harddrive +with a new name, @code{mysqlrepair}, or alternatively make a symbolic +link to @code{mysqlrepair} and name the symbolic link as +@code{mysqlrepair}. If you invoke @code{mysqlrepair} now, it will repair +tables by default. + +The names that you can use to change @code{mysqlcheck} default behavior +are here: + +@example +mysqlrepair: The default option will be -r +mysqlanalyze: The default option will be -a +mysqloptimize: The default option will be -o +@end example + +The options available for @code{mysqlcheck} are listed here, please +check what your version supports with @code{mysqlcheck --help}. + +@table @code +@item -A, --all-databases +Check all the databases. This will be same as --databases with all +databases selected +@item -1, --all-in-1 +Instead of making one query for each table, execute all queries in 1 +query separately for each database. Table names will be in a comma +separated list. +@item -a, --analyze +Analyze given tables. +@item --auto-repair +If a checked table is corrupted, automatically fix it. Repairing will be +done after all tables have been checked, if corrupted ones were found. +@item -#, --debug=... +Output debug log. Often this is 'd:t:o,filename' +@item --character-sets-dir=... +Directory where character sets are +@item -c, --check +Check table for errors +@item -C, --check-only-changed +Check only tables that have changed since last check or haven't been +closed properly. +@item --compress +Use compression in server/client protocol. +@item -?, --help +Display this help message and exit. +@item -B, --databases +To check several databases. Note the difference in usage; In this case +no tables are given. All name arguments are regarded as database names. +@item --default-character-set=... +Set the default character set +@item -F, --fast +Check only tables that hasn't been closed properly +@item -f, --force +Continue even if we get an sql-error. +@item -e, --extended +If you are using this option with CHECK TABLE, it will ensure that the +table is 100 percent consistent, but will take a long time. + +If you are using this option with REPAIR TABLE, it will run an extended +repair on the table, which may not only take a long time to execute, but +may produce a lot of garbage rows also! +@item -h, --host=... +Connect to host. +@item -m, --medium-check +Faster than extended-check, but only finds 99.99 percent of all +errors. Should be good enough for most cases. +@item -o, --optimize +Optimize table +@item -p, --password[=...] +Password to use when connecting to server. If password is not given +it's solicited on the tty. +@item -P, --port=... +Port number to use for connection. +@item -q, --quick +If you are using this option with CHECK TABLE, it prevents the check +from scanning the rows to check for wrong links. This is the fastest +check. + +If you are using this option with REPAIR TABLE, it will try to repair +only the index tree. This is the fastest repair method for a table. +@item -r, --repair +Can fix almost anything except unique keys that aren't unique. +@item -s, --silent +Print only error messages. +@item -S, --socket=... +Socket file to use for connection. +@item --tables +Overrides option --databases (-B). +@item -u, --user=# +User for login if not current user. +@item -v, --verbose +Print info about the various stages. +@item -V, --version +Output version information and exit. +@end table + + +@node mysqldump, mysqlhotcopy, Using mysqlcheck, Client-Side Scripts @subsection mysqldump, Dumping Table Structure and Data @cindex dumping, databases @@ -22911,9 +23033,6 @@ the new updates. Note that if you are replicating a database, all updates to this database should be done through the master! -On older servers one can use the update log to do simple replication. -@xref{Log Replication}. - Another benefit of using replication is that one can get live backups of the system by doing a backup on a slave instead of doing it on the master. @xref{Backup}. @@ -34613,7 +34732,9 @@ parameters to @code{FULLTEXT} in @code{CREATE/ALTER TABLE}). @end itemize -@node Table types, Maintenance, Reference, Top + + +@node Table types, Extending MySQL, Reference, Top @chapter MySQL Table Types @cindex table types, choosing @@ -34696,10 +34817,11 @@ of both worlds. * MERGE:: MERGE tables * ISAM:: ISAM tables * HEAP:: HEAP tables -* InnoDB:: InnoDB tables * BDB:: BDB or Berkeley_db tables +* InnoDB:: InnoDB tables @end menu + @node MyISAM, MERGE, Table types, Table types @section MyISAM Tables @@ -34877,10 +34999,12 @@ backup media. * MyISAM table problems:: @end menu -@cindex key space, MyISAM + @node Key space, MyISAM table formats, MyISAM, MyISAM @subsection Space Needed for Keys +@cindex key space, MyISAM + @strong{MySQL} can support different index types, but the normal type is ISAM or MyISAM. These use a B-tree index, and you can roughly calculate the size for the index file as @code{(key_length+4)/0.67}, summed over @@ -34900,6 +35024,7 @@ In @code{MyISAM} tables, you can also prefix compress numbers by specifying many integer keys that have an identical prefix when the numbers are stored high-byte first. + @node MyISAM table formats, MyISAM table problems, Key space, MyISAM @subsection MyISAM Table Formats @@ -34913,6 +35038,7 @@ compressed tables, can only be created with the @code{myisampack} tool. * Compressed format:: Compressed table characteristics @end menu + @node Static format, Dynamic format, MyISAM table formats, MyISAM table formats @subsubsection Static (Fixed-length) Table Characteristics @@ -34952,11 +35078,13 @@ system. Usually requires more disk space than dynamic tables. @end itemize -@cindex dynamic table characteristics -@cindex tables, dynamic + @node Dynamic format, Compressed format, Static format, MyISAM table formats @subsubsection Dynamic Table Characteristics +@cindex dynamic table characteristics +@cindex tables, dynamic + This format is used if the table contains any @code{VARCHAR}, @code{BLOB}, or @code{TEXT} columns or if the table was created with @code{ROW_FORMAT=dynamic}. @@ -35016,10 +35144,12 @@ If not, there will be another link. You may check how many links there are with @code{myisamchk -ed}. All links may be removed with @code{myisamchk -r}. @end itemize -@cindex tables, compressed format + @node Compressed format, , Dynamic format, MyISAM table formats @subsubsection Compressed Table Characteristics +@cindex tables, compressed format + This is a read-only type that is generated with the optional @code{myisampack} tool (@code{pack_isam} for @code{ISAM} tables): @@ -35062,6 +35192,7 @@ columns. Can be uncompressed with @code{myisamchk}. @end itemize + @node MyISAM table problems, , MyISAM table formats, MyISAM @subsection MyISAM table problems. @@ -35074,6 +35205,7 @@ to become corrupted. * MyISAM table close:: @end menu + @node Corrupted MyISAM tables, MyISAM table close, MyISAM table problems, MyISAM table problems @subsubsection Corrupted MyISAM tables. @@ -35121,6 +35253,7 @@ checking if there is a recent row @code{restarted mysqld} in the mysqld error file). If this isn't the case, then you should try to make a test case of this. @xref{Reproduceable test case}. + @node MyISAM table close, , Corrupted MyISAM tables, MyISAM table problems @subsubsection Clients is using or hasn't closed the table properly @@ -35179,11 +35312,13 @@ be avoided as it currently replaces the data file with a new one, which is not signaled to the other servers. @end itemize -@cindex tables, merging -@cindex MERGE tables, defined + @node MERGE, ISAM, MyISAM, Table types @section MERGE Tables +@cindex tables, merging +@cindex MERGE tables, defined + @code{MERGE} tables are new in @strong{MySQL} Version 3.23.25. The code is still in gamma, but should be resonable stable. @@ -35332,10 +35467,12 @@ Change the @code{.MRG} file and issue a @code{FLUSH TABLE} on the read the new definition file. @end itemize -@cindex tables, ISAM + @node ISAM, HEAP, MERGE, Table types @section ISAM Tables +@cindex tables, ISAM + You can also use the deprecated ISAM table type. This will disappear rather soon because @code{MyISAM} is a better implementation of the same thing. ISAM uses a @code{B-tree} index. The index is stored in a file @@ -35374,10 +35511,12 @@ TABLE} statement: mysql> ALTER TABLE tbl_name TYPE = MYISAM; @end example -@cindex tables, @code{HEAP} -@node HEAP, InnoDB, ISAM, Table types + +@node HEAP, BDB, ISAM, Table types @section HEAP Tables +@cindex tables, @code{HEAP} + @code{HEAP} tables use a hashed index and are stored in memory. This makes them very fast, but if @strong{MySQL} crashes you will lose all data stored in them. @code{HEAP} is very useful for temporary tables! @@ -35452,7 +35591,303 @@ SUM_OVER_ALL_KEYS(max_length_of_key + sizeof(char*) * 2) @code{sizeof(char*)} is 4 on 32-bit machines and 8 on 64-bit machines. -@node InnoDB, BDB, HEAP, Table types + +@node BDB, InnoDB, HEAP, Table types +@section BDB or Berkeley_DB Tables + +@cindex tables, @code{BDB} +@cindex tables, @code{Berkeley DB} + +@menu +* BDB overview:: Overview of BDB Tables +* BDB install:: Installing BDB +* BDB start:: BDB startup options +* BDB characteristic:: Some characteristic of @code{BDB} tables: +* BDB TODO:: Some things we need to fix for BDB in the near future: +* BDB portability:: Operating systems supported by @strong{BDB} +* BDB errors:: Errors You May Get When Using BDB Tables +@end menu + +@node BDB overview, BDB install, BDB, BDB +@subsection Overview of BDB Tables + +Support for BDB tables is included in the @strong{MySQL} source distribution +starting from Version 3.23.34 and is activated in the @strong{MySQL}-Max +binary. + +BerkeleyDB, available at @uref{http://www.sleepycat.com/} has provided +@strong{MySQL} with a transactional table handler. By using BerkeleyDB +tables, your tables may have a greater chance of surviving crashes, and also +provides @code{COMMIT} and @code{ROLLBACK} on transactions. The +@strong{MySQL} source distribution comes with a BDB distribution that has a +couple of small patches to make it work more smoothly with @strong{MySQL}. +You can't use a non-patched @code{BDB} version with @strong{MySQL}. + +We at @strong{MySQL AB} are working in close cooperation with Sleepycat to +keep the quality of the @strong{MySQL}/BDB interface high. + +When it comes to supporting BDB tables, we are committed to help our +users to locate the problem and help creating a reproducable test case +for any problems involving BDB tables. Any such test case will be +forwarded to Sleepycat who in turn will help us find and fix the +problem. As this is a two stage operation, any problems with BDB tables +may take a little longer for us to fix than for other table handlers. +However, as the BerkeleyDB code itself has been used by many other +applications than @strong{MySQL}, we don't envision any big problems with +this. @xref{Table handler support}. + + +@node BDB install, BDB start, BDB overview, BDB +@subsection Installing BDB + +If you have downloaded a binary version of @strong{MySQL} that includes +support for BerkeleyDB, simply follow the instructions for installing a +binary version of @strong{MySQL}. +@xref{Installing binary}. @xref{mysqld-max, , @code{mysqld-max}}. + +To compile @strong{MySQL} with Berkeley DB support, download @strong{MySQL} +Version 3.23.34 or newer and configure @code{MySQL} with the +@code{--with-berkeley-db} option. @xref{Installing source}. + +@example +cd /path/to/source/of/mysql-3.23.34 +./configure --with-berkeley-db +@end example + +Please refer to the manual provided with the @code{BDB} distribution for +more updated information. + +Even though Berkeley DB is in itself very tested and reliable, +the @strong{MySQL} interface is still considered beta quality. +We are actively improving and optimizing it to get it stable very +soon. + + +@node BDB start, BDB characteristic, BDB install, BDB +@subsection BDB startup options + +If you are running with @code{AUTOCOMMIT=0} then your changes in @code{BDB} +tables will not be updated until you execute @code{COMMIT}. Instead of commit +you can execute @code{ROLLBACK} to forget your changes. @xref{COMMIT}. + +If you are running with @code{AUTOCOMMIT=1} (the default), your changes +will be committed immediately. You can start an extended transaction with +the @code{BEGIN WORK} SQL command, after which your changes will not be +committed until you execute @code{COMMIT} (or decide to @code{ROLLBACK} +the changes). + +The following options to @code{mysqld} can be used to change the behavior of +BDB tables: + +@multitable @columnfractions .30 .70 +@item @strong{Option} @tab @strong{Meaning} +@item @code{--bdb-home=directory} @tab Base directory for BDB tables. This should be the same directory you use for --datadir. +@item @code{--bdb-lock-detect=#} @tab Berkeley lock detect. One of (DEFAULT, OLDEST, RANDOM, or YOUNGEST). +@item @code{--bdb-logdir=directory} @tab Berkeley DB log file directory. +@item @code{--bdb-no-sync} @tab Don't synchronously flush logs. +@item @code{--bdb-no-recover} @tab Don't start Berkeley DB in recover mode. +@item @code{--bdb-shared-data} @tab Start Berkeley DB in multi-process mode (Don't use @code{DB_PRIVATE} when initializing Berkeley DB) +@item @code{--bdb-tmpdir=directory} @tab Berkeley DB tempfile name. +@item @code{--skip-bdb} @tab Don't use berkeley db. +@item @code{-O bdb_max_lock=1000} @tab Set the maximum number of locks possible. @xref{SHOW VARIABLES}. +@end multitable + +If you use @code{--skip-bdb}, @strong{MySQL} will not initialize the +Berkeley DB library and this will save a lot of memory. Of course, +you cannot use @code{BDB} tables if you are using this option. + +Normally you should start @code{mysqld} without @code{--bdb-no-recover} if you +intend to use BDB tables. This may, however, give you problems when you +try to start @code{mysqld} if the BDB log files are corrupted. @xref{Starting +server}. + +With @code{bdb_max_lock} you can specify the maximum number of locks +(10000 by default) you can have active on a BDB table. You should +increase this if you get errors of type @code{bdb: Lock table is out of +available locks} or @code{Got error 12 from ...} when you have do long +transactions or when @code{mysqld} has to examine a lot of rows to +calculate the query. + +You may also want to change @code{binlog_cache_size} and +@code{max_binlog_cache_size} if you are using big multi-line transactions. +@xref{COMMIT}. + + +@node BDB characteristic, BDB TODO, BDB start, BDB +@subsection Some characteristic of @code{BDB} tables: + +@itemize @bullet +@item +To be able to rollback transactions BDB maintain log files. For maximum +performance you should place these on another disk than your databases +by using the @code{--bdb_log_dir} options. +@item +@strong{MySQL} performs a checkpoint each time a new BDB log +file is started, and removes any log files that are not needed for +current transactions. One can also run @code{FLUSH LOGS} at any time +to checkpoint the Berkeley DB tables. + +For disaster recovery, one should use table backups plus +@strong{MySQL}'s binary log. @xref{Backup}. + +@strong{Warning}: If you delete old log files that are in use, BDB will +not be able to do recovery at all and you may lose data if something +goes wrong. +@item +@strong{MySQL} requires a @code{PRIMARY KEY} in each BDB table to be +able to refer to previously read rows. If you don't create one, +@strong{MySQL} will create an maintain a hidden @code{PRIMARY KEY} for +you. The hidden key has a length of 5 bytes and is incremented for each +insert attempt. +@item +If all columns you access in a @code{BDB} table are part of the same index or +part of the primary key, then @strong{MySQL} can execute the query +without having to access the actual row. In a @code{MyISAM} table the +above holds only if the columns are part of the same index. +@item +The @code{PRIMARY KEY} will be faster than any other key, as the +@code{PRIMARY KEY} is stored together with the row data. As the other keys are +stored as the key data + the @code{PRIMARY KEY}, it's important to keep the +@code{PRIMARY KEY} as short as possible to save disk and get better speed. +@item +@code{LOCK TABLES} works on @code{BDB} tables as with other tables. If +you don't use @code{LOCK TABLE}, @strong{MYSQL} will issue an internal +multiple-write lock on the table to ensure that the table will be +properly locked if another thread issues a table lock. +@item +Internal locking in @code{BDB} tables is done on page level. +@item +@code{SELECT COUNT(*) FROM table_name} is slow as @code{BDB} tables doesn't +maintain a count of the number of rows in the table. +@item +Scanning is slower than with @code{MyISAM} tables as one has data in BDB +tables stored in B-trees and not in a separate data file. +@item +The application must always be prepared to handle cases where +any change of a @code{BDB} table may make an automatic rollback and any +read may fail with a deadlock error. +@item +Keys are not compressed to previous keys as with ISAM or MyISAM +tables. In other words, the key information will take a little more +space in @code{BDB} tables compared to MyISAM tables which don't use +@code{PACK_KEYS=0}. +@item +There is often holes in the BDB table to allow you to insert new rows in +the middle of the key tree. This makes BDB tables somewhat larger than +MyISAM tables. +@item +The optimizer needs to know an approximation of the number of rows in +the table. @strong{MySQL} solves this by counting inserts and +maintaining this in a separate segment in each BDB table. If you don't +do a lot of @code{DELETE} or @code{ROLLBACK}:s this number should be +accurate enough for the @strong{MySQL} optimizer, but as @strong{MySQL} +only store the number on close, it may be wrong if @strong{MySQL} dies +unexpectedly. It should not be fatal even if this number is not 100 % +correct. One can update the number of rows by executing @code{ANALYZE +TABLE} or @code{OPTIMIZE TABLE}. @xref{ANALYZE TABLE} . @xref{OPTIMIZE +TABLE}. +@item +If you get full disk with a @code{BDB} table, you will get an error +(probably error 28) and the transaction should roll back. This is in +contrast with @code{MyISAM} and @code{ISAM} tables where @code{mysqld} will +wait for enough free disk before continuing. +@end itemize + + +@node BDB TODO, BDB portability, BDB characteristic, BDB +@subsection Some things we need to fix for BDB in the near future: + +@itemize @bullet +@item +It's very slow to open many BDB tables at the same time. If you are +going to use BDB tables, you should not have a very big table cache (> +256 ?) and you should use @code{--no-auto-rehash} with the @code{mysql} +client. We plan to partly fix this in 4.0. +@item +@code{SHOW TABLE STATUS} doesn't yet provide that much information for BDB +tables. +@item +Optimize performance. +@item +Change to not use page locks at all when we are scanning tables. +@end itemize + + +@node BDB portability, BDB errors, BDB TODO, BDB +@subsection Operating systems supported by @strong{BDB} + +If you after having built @strong{MySQL} with support for BDB tables get +the following error in the log file when you start @code{mysqld}: + +@example +bdb: architecture lacks fast mutexes: applications cannot be threaded +Can't init dtabases +@end example + +This means that @code{BDB} tables are not supported for your architecture. +In this case you have to rebuild @strong{MySQL} without BDB table support. + +NOTE: The following list is not complete; We will update this as we get +more information about this. + +Currently we know that BDB tables works with the following operating +system. + +@itemize @bullet +@item +Linux 2.x intel +@item +Solaris sparc +@item +SCO OpenServer +@item +SCO UnixWare 7.0.1 +@end itemize + +It doesn't work with the following operating systems: + +@itemize @bullet +@item +Linux 2.x Alpha +@item +Max OS X +@end itemize + + +@node BDB errors, , BDB portability, BDB +@subsection Errors You May Get When Using BDB Tables + +@itemize @bullet +@item +If you get the following error in the @code{hostname.err log} when +starting @code{mysqld}: + +@example +bdb: Ignoring log file: .../log.XXXXXXXXXX: unsupported log version # +@end example +it means that the new @code{BDB} version doesn't support the old log +file format. In this case you have to delete all @code{BDB} log BDB +from your database directory (the files that has the format +@code{log.XXXXXXXXXX} ) and restart @code{mysqld}. We would also +recommend you to do a @code{mysqldump --opt} of your old @code{BDB} +tables, delete the old table and restore the dump. +@item +If you are running in not @code{auto_commit} mode and delete a table you +are using by another thread you may get the following error messages in +the @strong{MySQL} error file: + +@example +001119 23:43:56 bdb: Missing log fileid entry +001119 23:43:56 bdb: txn_abort: Log undo failed for LSN: 1 3644744: Invalid +@end example + +This is not fatal but we don't recommend that you delete tables if you are +not in @code{auto_commit} mode, until this problem is fixed (the fix is +not trivial). +@end itemize + + +@node InnoDB, , BDB, Table types @section InnoDB Tables @menu @@ -35472,6 +35907,7 @@ SUM_OVER_ALL_KEYS(max_length_of_key + sizeof(char*) * 2) * InnoDB contact information:: InnoDB contact information. @end menu + @node InnoDB overview, InnoDB start, InnoDB, InnoDB @subsection InnoDB tables overview @@ -35518,6 +35954,7 @@ may consist of several files. This is different from, for example, InnoDB is distributed under the GNU GPL License Version 2 (of June 1991). In the source distribution of @strong{MySQL}, InnoDB appears as a subdirectory. + @node InnoDB start, InnoDB init, InnoDB overview, InnoDB @subsection InnoDB startup options @@ -35671,6 +36108,7 @@ performance it is wisest not to specify this parameter at all, in which case it will get the default value. @end multitable + @node InnoDB init, Using InnoDB tables, InnoDB start, InnoDB @subsection Creating InnoDB table space @@ -35736,6 +36174,7 @@ mysqld: ready for connections * Error creating InnoDB:: @end menu + @node Error creating InnoDB, , InnoDB init, InnoDB init @subsubsection If something goes wrong in database creation @@ -35746,6 +36185,7 @@ create some InnoDB tables, delete also the corresponding @file{.frm} files for these tables from the @strong{MySQL} database directories. Then you can try the InnoDB database creation again. + @node Using InnoDB tables, Adding and removing, InnoDB init, InnoDB @subsection Creating InnoDB tables @@ -35829,6 +36269,7 @@ it is better that you kill the database process and delete all InnoDB data and log files and all InnoDB table @file{.frm} files, and start your job again, rather than wait for millions of disk i/os to complete. + @node Adding and removing, Backing up, Using InnoDB tables, InnoDB @subsection Adding and removing InnoDB data and log files @@ -35851,6 +36292,7 @@ database. Delete then the old log files from the log file directory, edit @file{my.cnf}, and start @strong{MySQL} again. InnoDB will tell you at the startup that it is creating new log files. + @node Backing up, Moving, Adding and removing, InnoDB @subsection Backing up and recovering an InnoDB database @@ -35937,6 +36379,7 @@ MySQL manual. * InnoDB checkpoints:: @end menu + @node InnoDB checkpoints, , Backing up, Backing up @subsubsection Checkpoints @@ -35968,6 +36411,7 @@ the total size of the log files as big as the buffer pool or even bigger. The drawback in big log files is that crash recovery can last longer because there will be more log to apply to the database. + @node Moving, InnoDB transaction model, Backing up, InnoDB @subsection Moving an InnoDB database to another machine @@ -35988,6 +36432,7 @@ the big rollback segment the big import transaction will generate. Do the commit only after importing a whole table or a segment of a table. + @node InnoDB transaction model, Implementation, Moving, InnoDB @subsection InnoDB transaction model @@ -36022,6 +36467,7 @@ transaction. * InnoDB Deadlock detection:: @end menu + @node InnoDB consistent read, InnoDB locking reads, InnoDB transaction model, InnoDB transaction model @subsubsection Consistent read @@ -36047,6 +36493,7 @@ on the tables it accesses, and therefore other users are free to modify those tables at the same time a consistent read is being performed on the table. + @node InnoDB locking reads, InnoDB Next-key locking, InnoDB consistent read, InnoDB transaction model @subsubsection Locking reads @@ -36104,6 +36551,7 @@ available data setting exclusive locks on each row it reads. Thus it sets the same locks a searched SQL @code{UPDATE} would set on the rows. + @node InnoDB Next-key locking, InnoDB Locks set, InnoDB locking reads, InnoDB transaction model @subsubsection Next-key locking: avoiding the phantom problem @@ -36158,6 +36606,7 @@ anyone meanwhile inserting a duplicate for your row. Thus the next-key locking allows you to 'lock' the non-existence of something in your table. + @node InnoDB Locks set, InnoDB Deadlock detection, InnoDB Next-key locking, InnoDB transaction model @subsubsection Locks set by different SQL statements in InnoDB @@ -36210,6 +36659,7 @@ get a table lock on a table where another user currently has row level locks. But that does not put transaction integerity into danger. @end itemize + @node InnoDB Deadlock detection, , InnoDB Locks set, InnoDB transaction model @subsubsection Deadlock detection and rollback @@ -36326,6 +36776,7 @@ INSERT INTO yourtable VALUES (1, 2), (5, 5); This tip is of course valid for inserts into any table type, not just InnoDB. + @node Implementation, Table and index, InnoDB transaction model, InnoDB @subsection Implementation of multiversioning @@ -36375,6 +36826,7 @@ its index records from the database. This removal operation is called a purge, and it is quite fast, usually taking the same order of time as the SQL statement which did the deletion. + @node Table and index, File space management, Implementation, InnoDB @subsection Table and index structures @@ -36410,6 +36862,7 @@ will use more space. * InnoDB Physical record:: @end menu + @node InnoDB physical structure, InnoDB Insert buffering, Table and index, Table and index @subsubsection Physical structure of an index @@ -36425,6 +36878,7 @@ If records are inserted in a random order, then the pages will be 1/2 - 15/16 full. If the fillfactor of an index page drops below 1/2, InnoDB will try to contract the index tree to free the page. + @node InnoDB Insert buffering, InnoDB Adaptive hash, InnoDB physical structure, Table and index @subsubsection Insert buffering @@ -36452,6 +36906,7 @@ same page in of the index tree, and hence save disk i/o's. It has been measured that the insert buffer can speed up insertions to a table up to 15 times. + @node InnoDB Adaptive hash, InnoDB Physical record, InnoDB Insert buffering, Table and index @subsubsection Adaptive hash indexes @@ -36475,6 +36930,7 @@ In a sense, through the adaptive hash index mechanism InnoDB adapts itself to ample main memory, coming closer to the architecture of main memory databases. + @node InnoDB Physical record, , InnoDB Adaptive hash, Table and index @subsubsection Physical record structure @@ -36532,6 +36988,7 @@ The behavior of auto-increment is not defined if a user gives a negative value to the column or if the value becomes bigger than the maximum integer that can be stored in the specified integer type. + @node File space management, Error handling, Table and index, InnoDB @subsection File space management and disk i/o @@ -36541,6 +36998,7 @@ integer that can be stored in the specified integer type. * InnoDB File Defragmenting:: @end menu + @node InnoDB Disk i/o, InnoDB File space, File space management, File space management @subsubsection Disk i/o @@ -36572,6 +37030,7 @@ in a tablespace seems to be in the process of being fully read into the buffer pool. Then InnoDB posts the remaining reads to the i/o system. + @node InnoDB File space, InnoDB File Defragmenting, InnoDB Disk i/o, File space management @subsubsection File space management @@ -36619,6 +37078,7 @@ but remember that deleted rows can be physically removed only in a purge operation after they are no longer needed in transaction rollback or consistent read. + @node InnoDB File Defragmenting, , InnoDB File space, File space management @subsubsection Defragmenting a table @@ -36642,6 +37102,7 @@ records are deleted only from the end, then the the file space management algorithm of InnoDB guarantees that fragmentation in the index will not occur. + @node Error handling, InnoDB restrictions, File space management, InnoDB @subsection Error handling @@ -36674,6 +37135,7 @@ Other errors are mostly detected by the @strong{MySQL} layer of code, and they roll back the corresponding SQL statement. @end itemize + @node InnoDB restrictions, InnoDB contact information, Error handling, InnoDB @subsection Some restrictions on InnoDB tables @@ -36734,6 +37196,7 @@ The maximum tablespace size is 4 billion database pages. This is also the maximum size for a table. The minimum tablespace size is 10 MB. @end itemize + @node InnoDB contact information, , InnoDB restrictions, InnoDB @subsection InnoDB contact information @@ -36751,449 +37214,27 @@ P.O.Box 800 Finland @end example -@cindex tables, @code{BDB} -@cindex tables, @code{Berkeley DB} -@node BDB, , InnoDB, Table types -@section BDB or Berkeley_DB Tables - -@menu -* BDB overview:: Overview of BDB Tables -* BDB install:: Installing BDB -* BDB start:: BDB startup options -* BDB characteristic:: Some characteristic of @code{BDB} tables: -* BDB TODO:: Some things we need to fix for BDB in the near future: -* BDB portability:: Operating systems supported by @strong{BDB} -* BDB errors:: Errors You May Get When Using BDB Tables -@end menu - -@node BDB overview, BDB install, BDB, BDB -@subsection Overview of BDB Tables - -Support for BDB tables is included in the @strong{MySQL} source distribution -starting from Version 3.23.34 and is activated in the @strong{MySQL}-Max -binary. - -BerkeleyDB, available at @uref{http://www.sleepycat.com/} has provided -@strong{MySQL} with a transactional table handler. By using BerkeleyDB -tables, your tables may have a greater chance of surviving crashes, and also -provides @code{COMMIT} and @code{ROLLBACK} on transactions. The -@strong{MySQL} source distribution comes with a BDB distribution that has a -couple of small patches to make it work more smoothly with @strong{MySQL}. -You can't use a non-patched @code{BDB} version with @strong{MySQL}. - -We at @strong{MySQL AB} are working in close cooperation with Sleepycat to -keep the quality of the @strong{MySQL}/BDB interface high. - -When it comes to supporting BDB tables, we are committed to help our -users to locate the problem and help creating a reproducable test case -for any problems involving BDB tables. Any such test case will be -forwarded to Sleepycat who in turn will help us find and fix the -problem. As this is a two stage operation, any problems with BDB tables -may take a little longer for us to fix than for other table handlers. -However, as the BerkeleyDB code itself has been used by many other -applications than @strong{MySQL}, we don't envision any big problems with -this. @xref{Table handler support}. - -@node BDB install, BDB start, BDB overview, BDB -@subsection Installing BDB - -If you have downloaded a binary version of @strong{MySQL} that includes -support for BerkeleyDB, simply follow the instructions for installing a -binary version of @strong{MySQL}. -@xref{Installing binary}. @xref{mysqld-max, , @code{mysqld-max}}. - -To compile @strong{MySQL} with Berkeley DB support, download @strong{MySQL} -Version 3.23.34 or newer and configure @code{MySQL} with the -@code{--with-berkeley-db} option. @xref{Installing source}. - -@example -cd /path/to/source/of/mysql-3.23.34 -./configure --with-berkeley-db -@end example - -Please refer to the manual provided with the @code{BDB} distribution for -more updated information. -Even though Berkeley DB is in itself very tested and reliable, -the @strong{MySQL} interface is still considered beta quality. -We are actively improving and optimizing it to get it stable very -soon. -@node BDB start, BDB characteristic, BDB install, BDB -@subsection BDB startup options - -If you are running with @code{AUTOCOMMIT=0} then your changes in @code{BDB} -tables will not be updated until you execute @code{COMMIT}. Instead of commit -you can execute @code{ROLLBACK} to forget your changes. @xref{COMMIT}. - -If you are running with @code{AUTOCOMMIT=1} (the default), your changes -will be committed immediately. You can start an extended transaction with -the @code{BEGIN WORK} SQL command, after which your changes will not be -committed until you execute @code{COMMIT} (or decide to @code{ROLLBACK} -the changes). - -The following options to @code{mysqld} can be used to change the behavior of -BDB tables: - -@multitable @columnfractions .30 .70 -@item @strong{Option} @tab @strong{Meaning} -@item @code{--bdb-home=directory} @tab Base directory for BDB tables. This should be the same directory you use for --datadir. -@item @code{--bdb-lock-detect=#} @tab Berkeley lock detect. One of (DEFAULT, OLDEST, RANDOM, or YOUNGEST). -@item @code{--bdb-logdir=directory} @tab Berkeley DB log file directory. -@item @code{--bdb-no-sync} @tab Don't synchronously flush logs. -@item @code{--bdb-no-recover} @tab Don't start Berkeley DB in recover mode. -@item @code{--bdb-shared-data} @tab Start Berkeley DB in multi-process mode (Don't use @code{DB_PRIVATE} when initializing Berkeley DB) -@item @code{--bdb-tmpdir=directory} @tab Berkeley DB tempfile name. -@item @code{--skip-bdb} @tab Don't use berkeley db. -@item @code{-O bdb_max_lock=1000} @tab Set the maximum number of locks possible. @xref{SHOW VARIABLES}. -@end multitable - -If you use @code{--skip-bdb}, @strong{MySQL} will not initialize the -Berkeley DB library and this will save a lot of memory. Of course, -you cannot use @code{BDB} tables if you are using this option. - -Normally you should start @code{mysqld} without @code{--bdb-no-recover} if you -intend to use BDB tables. This may, however, give you problems when you -try to start @code{mysqld} if the BDB log files are corrupted. @xref{Starting -server}. - -With @code{bdb_max_lock} you can specify the maximum number of locks -(10000 by default) you can have active on a BDB table. You should -increase this if you get errors of type @code{bdb: Lock table is out of -available locks} or @code{Got error 12 from ...} when you have do long -transactions or when @code{mysqld} has to examine a lot of rows to -calculate the query. - -You may also want to change @code{binlog_cache_size} and -@code{max_binlog_cache_size} if you are using big multi-line transactions. -@xref{COMMIT}. - -@node BDB characteristic, BDB TODO, BDB start, BDB -@subsection Some characteristic of @code{BDB} tables: - -@itemize @bullet -@item -To be able to rollback transactions BDB maintain log files. For maximum -performance you should place these on another disk than your databases -by using the @code{--bdb_log_dir} options. -@item -@strong{MySQL} performs a checkpoint each time a new BDB log -file is started, and removes any log files that are not needed for -current transactions. One can also run @code{FLUSH LOGS} at any time -to checkpoint the Berkeley DB tables. - -For disaster recovery, one should use table backups plus -@strong{MySQL}'s binary log. @xref{Backup}. - -@strong{Warning}: If you delete old log files that are in use, BDB will -not be able to do recovery at all and you may lose data if something -goes wrong. -@item -@strong{MySQL} requires a @code{PRIMARY KEY} in each BDB table to be -able to refer to previously read rows. If you don't create one, -@strong{MySQL} will create an maintain a hidden @code{PRIMARY KEY} for -you. The hidden key has a length of 5 bytes and is incremented for each -insert attempt. -@item -If all columns you access in a @code{BDB} table are part of the same index or -part of the primary key, then @strong{MySQL} can execute the query -without having to access the actual row. In a @code{MyISAM} table the -above holds only if the columns are part of the same index. -@item -The @code{PRIMARY KEY} will be faster than any other key, as the -@code{PRIMARY KEY} is stored together with the row data. As the other keys are -stored as the key data + the @code{PRIMARY KEY}, it's important to keep the -@code{PRIMARY KEY} as short as possible to save disk and get better speed. -@item -@code{LOCK TABLES} works on @code{BDB} tables as with other tables. If -you don't use @code{LOCK TABLE}, @strong{MYSQL} will issue an internal -multiple-write lock on the table to ensure that the table will be -properly locked if another thread issues a table lock. -@item -Internal locking in @code{BDB} tables is done on page level. -@item -@code{SELECT COUNT(*) FROM table_name} is slow as @code{BDB} tables doesn't -maintain a count of the number of rows in the table. -@item -Scanning is slower than with @code{MyISAM} tables as one has data in BDB -tables stored in B-trees and not in a separate data file. -@item -The application must always be prepared to handle cases where -any change of a @code{BDB} table may make an automatic rollback and any -read may fail with a deadlock error. -@item -Keys are not compressed to previous keys as with ISAM or MyISAM -tables. In other words, the key information will take a little more -space in @code{BDB} tables compared to MyISAM tables which don't use -@code{PACK_KEYS=0}. -@item -There is often holes in the BDB table to allow you to insert new rows in -the middle of the key tree. This makes BDB tables somewhat larger than -MyISAM tables. -@item -The optimizer needs to know an approximation of the number of rows in -the table. @strong{MySQL} solves this by counting inserts and -maintaining this in a separate segment in each BDB table. If you don't -do a lot of @code{DELETE} or @code{ROLLBACK}:s this number should be -accurate enough for the @strong{MySQL} optimizer, but as @strong{MySQL} -only store the number on close, it may be wrong if @strong{MySQL} dies -unexpectedly. It should not be fatal even if this number is not 100 % -correct. One can update the number of rows by executing @code{ANALYZE -TABLE} or @code{OPTIMIZE TABLE}. @xref{ANALYZE TABLE} . @xref{OPTIMIZE -TABLE}. -@item -If you get full disk with a @code{BDB} table, you will get an error -(probably error 28) and the transaction should roll back. This is in -contrast with @code{MyISAM} and @code{ISAM} tables where @code{mysqld} will -wait for enough free disk before continuing. -@end itemize - -@node BDB TODO, BDB portability, BDB characteristic, BDB -@subsection Some things we need to fix for BDB in the near future: - -@itemize @bullet -@item -It's very slow to open many BDB tables at the same time. If you are -going to use BDB tables, you should not have a very big table cache (> -256 ?) and you should use @code{--no-auto-rehash} with the @code{mysql} -client. We plan to partly fix this in 4.0. -@item -@code{SHOW TABLE STATUS} doesn't yet provide that much information for BDB -tables. -@item -Optimize performance. -@item -Change to not use page locks at all when we are scanning tables. -@end itemize - -@node BDB portability, BDB errors, BDB TODO, BDB -@subsection Operating systems supported by @strong{BDB} - -If you after having built @strong{MySQL} with support for BDB tables get -the following error in the log file when you start @code{mysqld}: - -@example -bdb: architecture lacks fast mutexes: applications cannot be threaded -Can't init dtabases -@end example - -This means that @code{BDB} tables are not supported for your architecture. -In this case you have to rebuild @strong{MySQL} without BDB table support. - -NOTE: The following list is not complete; We will update this as we get -more information about this. - -Currently we know that BDB tables works with the following operating -system. - -@itemize @bullet -@item -Linux 2.x intel -@item -Solaris sparc -@item -SCO OpenServer -@item -SCO UnixWare 7.0.1 -@end itemize - -It doesn't work with the following operating systems: - -@itemize @bullet -@item -Linux 2.x Alpha -@item -Max OS X -@end itemize - -@node BDB errors, , BDB portability, BDB -@subsection Errors You May Get When Using BDB Tables - -@itemize @bullet -@item -If you get the following error in the @code{hostname.err log} when -starting @code{mysqld}: - -@example -bdb: Ignoring log file: .../log.XXXXXXXXXX: unsupported log version # -@end example -it means that the new @code{BDB} version doesn't support the old log -file format. In this case you have to delete all @code{BDB} log BDB -from your database directory (the files that has the format -@code{log.XXXXXXXXXX} ) and restart @code{mysqld}. We would also -recommend you to do a @code{mysqldump --opt} of your old @code{BDB} -tables, delete the old table and restore the dump. -@item -If you are running in not @code{auto_commit} mode and delete a table you -are using by another thread you may get the following error messages in -the @strong{MySQL} error file: - -@example -001119 23:43:56 bdb: Missing log fileid entry -001119 23:43:56 bdb: txn_abort: Log undo failed for LSN: 1 3644744: Invalid -@end example - -This is not fatal but we don't recommend that you delete tables if you are -not in @code{auto_commit} mode, until this problem is fixed (the fix is -not trivial). -@end itemize - - - - -@node Maintenance, Adding functions, Table types, Top -@chapter Maintaining a MySQL Installation - -@cindex installation maintenance -@cindex maintaining, tables -@cindex tables, maintaining -@cindex databases, maintaining -@cindex @code{myisamchk} -@cindex @code{mysqlcheck} -@cindex crash, recovery -@cindex recovery, from crash +@node Extending MySQL, ODBC, Table types, Top +@chapter Extending MySQL @menu -* Using mysqlcheck:: Using mysqlcheck for maintenance and recovery +* Adding functions:: +* Adding procedures:: +* MySQL internals:: @end menu -This chapter covers what you should know about maintaining a @strong{MySQL} -distribution. You will learn how to care for your tables on a regular -basis, and what to do when disaster strikes. - -@node Using mysqlcheck, , Maintenance, Maintenance -@section Using @code{mysqlcheck} for Table Maintenance and Crash Recovery - -Since @strong{MySQL} version 3.23.38 you will be able to use a new -checking and repairing tool for @code{MyISAM} tables. The difference to -@code{myisamchk} is that @code{mysqlcheck} should be used when the -@code{mysqld} server is running, where as @code{myisamchk} should be used -when it is not. The benefit is that you no longer have to take the -server down for checking or repairing your tables. - -@code{mysqlcheck} uses @strong{MySQL} server commands @code{CHECK}, -@code{REPAIR}, @code{ANALYZE} and @code{OPTIMIZE} in a convenient way -for the user. - -There are three alternative ways to invoke @code{mysqlcheck}: - -@example -shell> mysqlcheck [OPTIONS] database [tables] -shell> mysqlcheck [OPTIONS] --databases DB1 [DB2 DB3...] -shell> mysqlcheck [OPTIONS] --all-databases -@end example - -So it can be used in a similar way as @code{mysqldump} when it -comes to what databases and tables you want to choose. - -@code{mysqlcheck} does have a special feature compared to the other -clients; the default behavior, checking tables (-c), can be changed by -renaming the binary. So if you want to have a tool that repairs tables -by default, you should just copy @code{mysqlcheck} to your harddrive -with a new name, @code{mysqlrepair}, or alternatively make a symbolic -link to @code{mysqlrepair} and name the symbolic link as -@code{mysqlrepair}. If you invoke @code{mysqlrepair} now, it will repair -tables by default. - -The names that you can use to change @code{mysqlcheck} default behavior -are here: - -@example -mysqlrepair: The default option will be -r -mysqlanalyze: The default option will be -a -mysqloptimize: The default option will be -o -@end example - -The options available for @code{mysqlcheck} are listed here, please -check what your version supports with @code{mysqlcheck --help}. - -@table @code -@item -A, --all-databases -Check all the databases. This will be same as --databases with all -databases selected -@item -1, --all-in-1 -Instead of making one query for each table, execute all queries in 1 -query separately for each database. Table names will be in a comma -separated list. -@item -a, --analyze -Analyze given tables. -@item --auto-repair -If a checked table is corrupted, automatically fix it. Repairing will be -done after all tables have been checked, if corrupted ones were found. -@item -#, --debug=... -Output debug log. Often this is 'd:t:o,filename' -@item --character-sets-dir=... -Directory where character sets are -@item -c, --check -Check table for errors -@item -C, --check-only-changed -Check only tables that have changed since last check or haven't been -closed properly. -@item --compress -Use compression in server/client protocol. -@item -?, --help -Display this help message and exit. -@item -B, --databases -To check several databases. Note the difference in usage; In this case -no tables are given. All name arguments are regarded as database names. -@item --default-character-set=... -Set the default character set -@item -F, --fast -Check only tables that hasn't been closed properly -@item -f, --force -Continue even if we get an sql-error. -@item -e, --extended -If you are using this option with CHECK TABLE, it will ensure that the -table is 100 percent consistent, but will take a long time. - -If you are using this option with REPAIR TABLE, it will run an extended -repair on the table, which may not only take a long time to execute, but -may produce a lot of garbage rows also! -@item -h, --host=... -Connect to host. -@item -m, --medium-check -Faster than extended-check, but only finds 99.99 percent of all -errors. Should be good enough for most cases. -@item -o, --optimize -Optimize table -@item -p, --password[=...] -Password to use when connecting to server. If password is not given -it's solicited on the tty. -@item -P, --port=... -Port number to use for connection. -@item -q, --quick -If you are using this option with CHECK TABLE, it prevents the check -from scanning the rows to check for wrong links. This is the fastest -check. - -If you are using this option with REPAIR TABLE, it will try to repair -only the index tree. This is the fastest repair method for a table. -@item -r, --repair -Can fix almost anything except unique keys that aren't unique. -@item -s, --silent -Print only error messages. -@item -S, --socket=... -Socket file to use for connection. -@item --tables -Overrides option --databases (-B). -@item -u, --user=# -User for login if not current user. -@item -v, --verbose -Print info about the various stages. -@item -V, --version -Output version information and exit. -@end table - +@node Adding functions, Adding procedures, Extending MySQL, Extending MySQL +@section Adding New Functions to MySQL @cindex functions, new @cindex adding, new functions @cindex user-defined functions, adding @cindex UDFs, defined @cindex functions, user-defined -@node Adding functions, Adding procedures, Maintenance, Top -@chapter Adding New Functions to MySQL There are two ways to add new functions to @strong{MySQL}: @@ -37228,15 +37269,60 @@ Whichever method you use to add new functions, they may be used just like native functions such as @code{ABS()} or @code{SOUNDEX()}. @menu +* CREATE FUNCTION:: * Adding UDF:: Adding a new user-definable function * Adding native function:: Adding a new native function @end menu + +@node CREATE FUNCTION, Adding UDF, Adding functions, Adding functions +@subsection @code{CREATE FUNCTION/DROP FUNCTION} Syntax + +@findex CREATE FUNCTION +@findex DROP FUNCTION +@findex UDF functions +@findex User-defined functions +@findex Functions, user-defined + +@example +CREATE [AGGREGATE] FUNCTION function_name RETURNS @{STRING|REAL|INTEGER@} + SONAME shared_library_name + +DROP FUNCTION function_name +@end example + +A user-definable function (UDF) is a way to extend @strong{MySQL} with a new +function that works like native (built in) @strong{MySQL} functions such as +@code{ABS()} and @code{CONCAT()}. + +@code{AGGREGATE} is a new option for @strong{MySQL} Version 3.23. An +@code{AGGREGATE} function works exactly like a native @strong{MySQL} +@code{GROUP} function like @code{SUM} or @code{COUNT()}. + +@code{CREATE FUNCTION} saves the function's name, type, and shared library +name in the @code{mysql.func} system table. You must have the +@strong{insert} and @strong{delete} privileges for the @code{mysql} database +to create and drop functions. + +All active functions are reloaded each time the server starts, unless +you start @code{mysqld} with the @code{--skip-grant-tables} option. In +this case, UDF initialization is skipped and UDFs are unavailable. +(An active function is one that has been loaded with @code{CREATE FUNCTION} +and not removed with @code{DROP FUNCTION}.) + +For instructions on writing user-definable functions, see @ref{Adding +functions}. For the UDF mechanism to work, functions must be written in C or +C++, your operating system must support dynamic loading and you must have +compiled @code{mysqld} dynamically (not statically). + + + +@node Adding UDF, Adding native function, CREATE FUNCTION, Adding functions +@subsection Adding a New User-definable Function + @cindex adding, user-definable functions @cindex user-defined functions, adding @cindex functions, user-definable, adding -@node Adding UDF, Adding native function, Adding functions, Adding functions -@section Adding a New User-definable Function @menu * UDF calling sequences:: UDF calling sequences @@ -37245,6 +37331,7 @@ native functions such as @code{ABS()} or @code{SOUNDEX()}. * UDF compiling:: Compiling and installing user-definable functions @end menu + For the UDF mechanism to work, functions must be written in C or C++ and your operating system must support dynamic loading. The @strong{MySQL} source distribution includes a file @file{sql/udf_example.cc} that defines 5 new @@ -37322,9 +37409,11 @@ that you are not allowed to allocate any global or static variables that change! If you need memory, you should allocate it in @code{xxx_init()} and free it in @code{xxx_deinit()}. -@cindex calling sequences, UDF + @node UDF calling sequences, UDF arguments, Adding UDF, Adding UDF -@subsection UDF Calling Sequences +@subsubsection UDF Calling Sequences + +@cindex calling sequences, UDF The main function should be declared as shown below. Note that the return type and parameters differ, depending on whether you will declare the SQL @@ -37404,10 +37493,12 @@ In @code{xxx()} and @code{xxx_deinit()}, refer to @code{initid->ptr} to use or deallocate the memory. @end table + +@node UDF arguments, UDF return values, UDF calling sequences, Adding UDF +@subsubsection Argument Processing + @cindex argument processing @cindex processing, arguments -@node UDF arguments, UDF return values, UDF calling sequences, Adding UDF -@subsection Argument Processing The @code{args} parameter points to a @code{UDF_ARGS} structure that thas the members listed below: @@ -37508,12 +37599,14 @@ types @code{INT_RESULT} or @code{REAL_RESULT}, @code{lengths} still contains the maximum length of the argument (as for the initialization function). @end table + +@node UDF return values, UDF compiling, UDF arguments, Adding UDF +@subsubsection Return Values and Error Handling + @cindex UDFs, return values @cindex return values, UDFs @cindex errors, handling for UDFs @cindex handling, errors -@node UDF return values, UDF compiling, UDF arguments, Adding UDF -@subsection Return Values and Error Handling The initialization function should return @code{0} if no error occurred and @code{1} otherwise. If an error occurs, @code{xxx_init()} should store a @@ -37567,11 +37660,13 @@ and @code{*is_null}: *is_null = 1; @end example + +@node UDF compiling, , UDF return values, Adding UDF +@subsubsection Compiling and Installing User-definable Functions + @cindex compiling, user-defined functions @cindex UDFs, compiling @cindex installing, user-defined functions -@node UDF compiling, , UDF return values, Adding UDF -@subsection Compiling and Installing User-definable Functions Files implementing UDFs must be compiled and installed on the host where the server runs. This process is described below for the example UDF file @@ -37669,11 +37764,13 @@ initialization is skipped and UDFs are unavailable. (An active function is one that has been loaded with @code{CREATE FUNCTION} and not removed with @code{DROP FUNCTION}.) + +@node Adding native function, , Adding UDF, Adding functions +@subsection Adding a New Native Function + @cindex adding, native functions @cindex native functions, adding @cindex functions, native, adding -@node Adding native function, , Adding UDF, Adding functions -@section Adding a New Native Function The procedure for adding a new native function is described below. Note that you cannot add native functions to a binary distribution because @@ -37760,11 +37857,13 @@ All current string functions try to avoid allocating any memory unless absolutely necessary! @end itemize + +@node Adding procedures, MySQL internals, Adding functions, Extending MySQL +@section Adding New Procedures to MySQL + @cindex procedures, adding @cindex adding, procedures @cindex new procedures, adding -@node Adding procedures, ODBC, Adding functions, Top -@chapter Adding New Procedures to MySQL In @strong{MySQL}, you can define a procedure in C++ that can access and modify the data in a query before it is sent to the client. The modification @@ -37781,8 +37880,9 @@ language to load a procedure at runtime into @code{mysqld}. * Writing a procedure:: Writing a procedure. @end menu + @node procedure analyse, Writing a procedure, Adding procedures, Adding procedures -@section Procedure Analyse +@subsection Procedure Analyse @code{analyse([max elements,[max memory]])} @@ -37804,8 +37904,9 @@ allocate per column while trying to find all distinct values. SELECT ... FROM ... WHERE ... PROCEDURE ANALYSE([max elements,[max memory]]) @end example + @node Writing a procedure, , procedure analyse, Adding procedures -@section Writing a Procedure +@subsection Writing a Procedure For the moment, the only documentation for this is the source. @@ -37818,11 +37919,298 @@ You can find all information about procedures by examining the following files: @item @file{sql/sql_select.cc} @end itemize + +@node MySQL internals, , Adding procedures, Extending MySQL +@section MySQL Internals + +@cindex internals +@cindex threads + +This chapter describes a lot of things that you need to know when +working on the @strong{MySQL} code. If you plan to contribute to MySQL +development, want to have access to the bleeding-edge in-between +versions code, or just want to keep track of development, follow the +instructions in @xref{Installing source tree}. If you are interested in MySQL +internals, you should also subscribe to @email{internals@@lists.mysql.com}. +This is a relatively low traffic list, in comparison with +@email{mysql@@lists.mysql.com}. + +@menu +* MySQL threads:: MySQL threads +* MySQL test suite:: MySQL test suite +@end menu + + +@node MySQL threads, MySQL test suite, MySQL internals, MySQL internals +@subsection MySQL Threads + +The @strong{MySQL} server creates the following threads: + +@itemize @bullet + +@item +The TCP/IP connection thread handles all connection requests and +creates a new dedicated thread to handle the authentication and +and SQL query processing for each connection. + +@item +On Windows NT there is a named pipe handler thread that does the same work as +the TCP/IP connection thread on named pipe connect requests. + +@item +The signal thread handles all signals. This thread also normally handles +alarms and calls @code{process_alarm()} to force timeouts on connections +that have been idle too long. + +@item +If @code{mysqld} is compiled with @code{-DUSE_ALARM_THREAD}, a dedicated +thread that handles alarms is created. This is only used on some systems where +there are problems with @code{sigwait()} or if one wants to use the +@code{thr_alarm()} code in ones application without a dedicated signal +handling thread. + +@item +If one uses the @code{--flush_time=#} option, a dedicated thread is created +to flush all tables at the given interval. + +@item +Every connection has its own thread. + +@item +Every different table on which one uses @code{INSERT DELAYED} gets its +own thread. + +@item +If you use @code{--master-host}, a slave replication thread will be +started to read and apply updates from the master. +@end itemize + +@code{mysqladmin processlist} only shows the connection, @code{INSERT DELAYED}, +and replication threads. + + +@node MySQL test suite, , MySQL threads, MySQL internals +@subsection MySQL Test Suite + +@cindex mysqltest, MySQL Test Suite +@cindex testing mysqld, mysqltest + +Until recently, our main full-coverage test suite was based on proprietary +customer data and for that reason has not been publicly available. The only +publicly available part of our testing process consisted of the @code{crash-me} +test, a Perl DBI/DBD benchmark found in the @code{sql-bench} directory, and +miscellaneous tests located in @code{tests} directory. The lack of a +standardized publicly available test suite has made it difficult for our users, +as well developers, to do regression tests on the @strong{MySQL} code. To +address this problem, we have created a new test system that is included in +the source and binary distributions starting in Version 3.23.29. + +The current set of test cases doesn't test everything in @strong{MySQL}, but it +should catch most obvious bugs in the SQL processing code, OS/library +issues, and is quite thorough in testing replication. Our eventual goal +is to have the tests cover 100% of the code. We welcome contributions +to our test suite. You may especially want to contribute tests that +examine the functionality critical to your system, as this will ensure +that all future @strong{MySQL} releases will work well with your +applications. + +@menu +* running mysqltest:: +* extending mysqltest:: +* Reporting mysqltest bugs:: +@end menu + + +@node running mysqltest, extending mysqltest, MySQL test suite, MySQL test suite +@subsubsection Running the MySQL Test Suite + +The test system consist of a test language interpreter +(@code{mysqltest}), a shell script to run all +tests(@code{mysql-test-run}), the actual test cases written in a special +test language, and their expected results. To run the test suite on +your system after a build, type @code{make test} or +@code{mysql-test/mysql-test-run} from the source root. If you have +installed a binary distribution, @code{cd} to the install root +(eg. @code{/usr/local/mysql}), and do @code{scripts/mysql-test-run}. +All tests should succeed. If not, you should try to find out why and +report the problem if this is a bug in @strong{MySQL}. +@xref{Reporting mysqltest bugs}. + +If you have a copy of @code{mysqld} running on the machine where you want to +run the test suite you do not have to stop it, as long as it is not using +ports @code{9306} and @code{9307}. If one of those ports is taken, you should +edit @code{mysql-test-run} and change the values of the master and/or slave +port to one that is available. + +You can run one individual test case with +@code{mysql-test/mysql-test-run test_name}. + +If one test fails, you should test running @code{mysql-test-run} with +the @code{--force} option to check if any other tests fails. + + +@node extending mysqltest, Reporting mysqltest bugs, running mysqltest, MySQL test suite +@subsubsection Extending the MySQL Test Suite + +You can use the @code{mysqltest} language to write your own test cases. +Unfortunately, we have not yet written full documentation for it - we plan to +do this shortly. You can, however, look at our current test cases and use +them as an example. The following points should help you get started: + +@itemize +@item +The tests are located in @code{mysql-test/t/*.test} + +@item +A test case consists of @code{;} terminated statements and is similar to the +input of @code{mysql} command line client. A statement by default is a query +to be sent to @strong{MySQL} server, unless it is recognized as internal +command ( eg. @code{sleep} ). + +@item +All queries that produce results, e.g. @code{SELECT}, @code{SHOW}, +@code{EXPLAIN}, etc., must be preceded with @code{@@/path/to/result/file}. The +file must contain the expected results. An easy way to generate the result +file is to run @code{mysqltest -r < t/test-case-name.test} from +@code{mysql-test} directory, and then edit the generated result files, if +needed, to adjust them to the expected output. In that case, be very careful +about not adding or deleting any invisible characters - make sure to only +change the text and/or delete lines. If you have to insert a line, make sure +the fields are separated with a hard tab, and there is a hard tab at the end. +You may want to use @code{od -c} to make sure your text editor has not messed +anything up during edit. We, of course, hope that you will never have to edit +the output of @code{mysqltest -r} as you only have to do it when you find a +bug. + +@item +To be consistent with our setup, you should put your result files in +@code{mysql-test/r} directory and name them @code{test_name.result}. If the +test produces more than one result, you should use @code{test_name.a.result}, +@code{test_name.b.result}, etc. + +@item +If a statement returns an error, you should on the line before the statement +specify with the @code{--error error-number}. The error number can be +a list of possible error numbers separated with @code{','}. + +@item +If you are writing a replication test case, you should on the first line of +the test file, put @code{source include/master-slave.inc;}. To switch between +master and slave, use @code{connection master;} and @code{connection slave;}. +If you need to do something on an alternate connection, you can do +@code{connection master1;} for the master, and @code{connection slave1;} for +the slave. + +@item +If you need to do something in a loop, you can use something like this: +@example +let $1=1000; +while ($1) +@{ + # do your queries here + dec $1; +@} +@end example + +@item +To sleep between queries, use the @code{sleep} command. It supports fractions +of a second, so you can do @code{sleep 1.3;}, for example, to sleep 1.3 +seconds. + +@item +To run the slave with additional options for your test case, put them +in the command-line format in @code{mysql-test/t/test_name-slave.opt}. For +the master, put them in @code{mysql-test/t/test_name-master.opt}. + +@item +If you have a question about the test suite, or have a test case to contribute, +e-mail to @email{internals@@lists.mysql.com}. As the list does not accept +attachments, you should ftp all the relevant files to: +@url{ftp://support.mysql.com/pub/mysql/Incoming} + +@end itemize + + +@node Reporting mysqltest bugs, , extending mysqltest, MySQL test suite +@subsubsection Reporting Bugs in the MySQL Test Suite + +If your @strong{MySQL} version doesn't pass the test suite you should +do the following: + +@itemize @bullet +@item +Don't send a bug report before you have found out as much as possible of +what when wrong! When you do it, please use the @code{mysqlbug} script +so that we can get information about your system and @code{MySQL} +version. @xref{Bug reports}. +@item +Make sure to include the output of @code{mysql-test-run}, as well as +contents of all @code{.reject} files in @code{mysql-test/r} directory. +@item +If a test in the test suite fails, check if the test fails also when run +by its own: + +@example +cd mysql-test +mysql-test-run --local test-name +@end example + +If this fails, then you should configure @strong{MySQL} with +@code{--with-debug} and run @code{mysql-test-run} with the +@code{--debug} option. If this also fails send the trace file +@file{var/tmp/master.trace} to ftp://support.mysql.com/pub/mysql/secret +so that we can examine it. Please remember to also include a full +description of your system, the version of the mysqld binary and how you +compiled it. + +@item +Try also to run @code{mysql-test-run} with the @code{--force} option to +see if there is any other test that fails. + +@item +If you have compiled @strong{MySQL} yourself, check our manual for how +to compile @strong{MySQL} on your platform or, preferable, use one of +the binaries we have compiled for you at +@uref{http://www.mysql.com/downloads/}. All our standard binaries should +pass the test suite ! + +@item +If you get an error, like @code{Result length mismatch} or @code{Result +content mismatch} it means that the output of the test didn't match +exactly the expected output. This could be a bug in @strong{MySQL} or +that your mysqld version produces slight different results under some +circumstances. + +Failed test results are put in a file with the same base name as the +result file with the @code{.reject} extension. If your test case is +failing, you should do a diff on the two files. If you cannot see how +they are different, examine both with @code{od -c} and also check their +lengths. + +@item +If a test fails totally, you should check the logs file in the +@code{mysql-test/var/log} directory for hints of what went wrong. + +@item +If you have compiled @strong{MySQL} with debugging you can try to debug this +by running @code{mysql-test-run} with the @code{--gdb} and/or @code{--debug} +options. +@xref{Making trace files}. + +If you have not compiled @strong{MySQL} for debugging you should probably +do that. Just specify the @code{--with-debug} options to @code{configure}! +@xref{Installing source}. +@end itemize + + + + +@node ODBC, Common programs, Extending MySQL, Top +@chapter MySQL ODBC Support + @cindex ODBC @cindex Windows @cindex MyODBC -@node ODBC, Common programs, Adding procedures, Top -@chapter MySQL ODBC Support @menu * Installing MyODBC:: How to install MyODBC @@ -37834,11 +38222,13 @@ You can find all information about procedures by examining the following files: * MyODBC bug report:: Reporting problems with MyODBC @end menu + @strong{MySQL} provides support for ODBC by means of the @strong{MyODBC} program. This chapter will teach you how to install @strong{MyODBC}, and how to use it. Here, you will also find a list of common programs that are known to work with @strong{MyODBC}. + @node Installing MyODBC, ODBC administrator, ODBC, ODBC @section How To Install MyODBC @@ -37927,10 +38317,12 @@ Notice that there are other configuration options on the screen of @strong{MySQL} (trace, don't prompt on connect, etc) that you can try if you run into problems. -@cindex ODBC, administrator + @node ODBC administrator, MyODBC connect parameters, Installing MyODBC, ODBC @section How to Fill in the Various Fields in the ODBC Administrator Program +@cindex ODBC, administrator + There are three possibilities for specifying the server name on Windows95: @@ -37982,6 +38374,7 @@ If you specify the option @code{Read options from C:\my.cnf}, the groups You can use all options that are usable by @code{mysql_options()}. @xref{mysql_options, , @code{mysql_options}}. + @node MyODBC connect parameters, ODBC Problems, ODBC administrator, ODBC @section Connect parameters for MyODBC @@ -38039,6 +38432,7 @@ you want to to debug @strong{MyODBC} (for example to enable tracing), you should instead use @code{MYODBCD.DLL}. To install this file, copy @file{MYODBCD.DLL} over the installed @code{MYODBC.DLL} file. + @node ODBC Problems, MyODBC clients, MyODBC connect parameters, ODBC @section How to Report Problems with MyODBC @@ -38069,6 +38463,7 @@ single floats. If the above doesn't help, you should do a @code{MyODBC} trace file and try to figure out why things go wrong. + @node MyODBC clients, ODBC and last_insert_id, ODBC Problems, ODBC @section Programs Known to Work with MyODBC @@ -38357,10 +38752,12 @@ columns to INT} option in the MyODBC connect screen. You should use the option flag @code{Don't optimize column widths}. @end table -@cindex AUTO-INCREMENT, ODBC + @node ODBC and last_insert_id, MyODBC bug report, MyODBC clients, ODBC @section How to Get the Value of an @code{AUTO_INCREMENT} Column in ODBC +@cindex AUTO-INCREMENT, ODBC + A common problem is how to get the value of an automatically generated ID from an @code{INSERT}. With ODBC, you can do something like this (assuming that @code{auto} is an @code{AUTO_INCREMENT} field): @@ -38385,11 +38782,13 @@ the following query can be used to find a newly inserted row: SELECT * FROM tbl_name WHERE auto IS NULL; @end example + +@node MyODBC bug report, , ODBC and last_insert_id, ODBC +@section Reporting Problems with MyODBC + @cindex reporting, MyODBC problems @cindex problems, ODBC @cindex MyODBC, reporting problems -@node MyODBC bug report, , ODBC and last_insert_id, ODBC -@section Reporting Problems with MyODBC If you encounter difficulties with @strong{MyODBC}, you should start by making a log file from the ODBC manager (the log you get when requesting @@ -38444,7 +38843,8 @@ file where you do exactly the same thing in the other SQL server. Remember that the more information you can supply to us, the more likely it is that we can fix the problem! -@node Common programs, Problems, ODBC, Top + +@node Common programs, Clients, ODBC, Top @chapter Using MySQL with Some Common Programs @menu @@ -38488,1711 +38888,10 @@ Only call @code{mysql_init()} with @code{NULL} as an argument, not a pre-allocated MYSQL struct. @end itemize -@cindex problems, common errors -@cindex errors, common -@node Problems, Common problems, Common programs, Top -@chapter Problems and Common Errors - -@menu -* What is crashing:: How to determine what is causing problems -* Crashing:: What to do if @strong{MySQL} keeps crashing -* Link errors:: Problems when linking with the @strong{MySQL} client library -* Common errors:: Some common errors when using @strong{MySQL} -* Full disk:: How @strong{MySQL} handles a full disk -* Temporary files:: Where @strong{MySQL} stores temporary files -* Problems with mysql.sock:: How to protect @file{/tmp/mysql.sock} -* Changing MySQL user:: How to run @strong{MySQL} as a normal user -* Resetting permissions:: How to reset a forgotten password. -* File permissions :: Problems with file permissions -* Not enough file handles:: File not found -* Using DATE:: Problems using @code{DATE} columns -* Timezone problems:: Timezone problems -* Case sensitivity:: Case sensitivity in searches -* Problems with NULL:: Problems with @code{NULL} values -* Problems with alias:: Problems with @code{alias} -* Deleting from related tables:: Deleting rows from related tables -* No matching rows:: Solving problems with no matching rows -* ALTER TABLE problems:: Problems with @code{ALTER TABLE}. -* Change column order:: How to change the order of columns in a table -* Temporary table problems:: -@end menu - -This chapter lists some common problems and error messages that users have -run into. You will learn how to figure out what the problem is, and what -to do to solve it. You will also find proper solutions to some common -problems. - -@node What is crashing, Crashing, Problems, Problems -@section How to Determine What Is Causing Problems - -When you run into problems, the first thing you should do is to find out -which program / piece of equipment is causing problems: - -@itemize @bullet -@item -If you have one of the following symptoms, then it is probably a hardware -(like memory, motherboard, CPU, or hard disk) or kernel problem: -@itemize @minus -@item -The keyboard doesn't work. This can normally be checked by pressing -Caps Lock. If the Caps Lock light doesn't change you have to replace -your keyboard. (Before doing this, you should try to reboot -your computer and check all cables to the keyboard.) -@item -The mouse pointer doesn't move. -@item -The machine doesn't answer to a remote machine's pings. -@item -Different, unrelated programs don't behave correctly. -@item -If your system rebooted unexpectedly (a faulty user level program should -NEVER be able to take down your system). -@end itemize - -In this case you should start by checking all your cables and run some -diagnostic tool to check your hardware! -You should also check if there are any patches, updates, or service -packs for your operating system that could likely solve your problems. -Check also that all your libraries (like glibc) are up to date. - -It's always good to use a machine with ECC memory to discover -memory problems early! -@item -If your keyboard is locked up, you may be able to fix this by -logging into your machine from another machine and execute -@code{kbd_mode -a} on it. - -@item -Please examine your system log file (/var/log/messages or similar) for -reasons for your problems. If you think the problem is in @strong{MySQL} -then you should also examine @strong{MySQL}'s log files. @xref{Update log}. - -@item -If you don't think you have hardware problems, you should try to find -out which program is causing problems. - -Try using @code{top}, @code{ps}, @code{taskmanager}, or some similar program, -to check which program is taking all CPU or is locking the machine. - -@item -Check with @code{top}, @code{df}, or a similar program if you are out of -memory, disk space, open files, or some other critical resource. - -@item -If the problem is some runaway process, you can always try to kill it. If it -doesn't want to die, there is probably a bug in the operating system. -@end itemize - -If after you have examined all other possibilities and you have -concluded that it's the @strong{MySQL} server or a @strong{MySQL} client -that is causing the problem, it's time to do a bug report for our -mailing list or our support team. In the bug report, try to give a -very detailed description of how the system is behaving and what you think is -happening. You should also state why you think it's @strong{MySQL} that -is causing the problems. Take into consideration all the situations in -this chapter. State any problems exactly how they appear when you -examine your system. Use the 'cut and paste' method for any output -and/or error messages from programs and/or log files! - -Try to describe in detail which program is not working and all -symptoms you see! We have in the past received many bug reports that just -state "the system doesn't work". This doesn't provide us with any -information about what could be the problem. - -If a program fails, it's always useful to know: - -@itemize @bullet -@item -Has the program in question made a segmentation fault (core dumped)? -@item -Is the program taking the whole CPU? Check with @code{top}. Let the -program run for a while, it may be evaluating something heavy. -@item -If it's the @code{mysqld} server that is causing problems, can you -do @code{mysqladmin -u root ping} or @code{mysqladmin -u root processlist}? -@item -What does a client program say (try with @code{mysql}, for example) -when you try to connect to the @strong{MySQL} server? -Does the client jam? Do you get any output from the program? -@end itemize - -When sending a bug report, you should of follow the outlines -described in this manual. @xref{Asking questions}. - -@cindex crash, repeated -@node Crashing, Link errors, What is crashing, Problems -@section What to Do if MySQL Keeps Crashing - -All @strong{MySQL} versions are tested on many platforms before they are -released. This doesn't mean that there aren't any bugs in -@strong{MySQL}, but it means if there are bugs, they are very few and can be -hard to find. If you have a problem, it will always help if you try to -find out exactly what crashes your system, as you will have a much better -chance of getting this fixed quickly. - -First, you should try to find out whether the problem is that the -@code{mysqld} daemon dies or whether your problem has to do with your -client. You can check how long your @code{mysqld} server has been up by -executing @code{mysqladmin version}. If @code{mysqld} has died, you may -find the reason for this in the file -@file{mysql-data-directory/`hostname`.err}. @xref{Error log}. - -Many crashes of @strong{MySQL} are caused by corrupted index / data -files. @strong{MySQL} will update the data on disk, with the -@code{write()} system call, after every SQL statement and before the -client is notified about the result. (This is not true if you are running -with @code{delayed_key_writes}, in which case only the data is written.) -This means that the data is safe even if @code{mysqld} crashes, as the OS will -ensure that the not flushed data is written to disk. You can force -@strong{MySQL} to sync everything to disk after every SQL command by -starting @code{mysqld} with @code{--flush}. - -The above means that normally you shouldn't get corrupted tables unless: - -@itemize @bullet -@item -Someone/something killed @code{mysqld} or the machine in the middle -of an update. -@item -You have found a bug in @code{mysqld} that caused it to die in the -middle of an update. -@item -Someone is manipulating the data/index files outside of @strong{mysqld} -without locking the table properly. -@item -If you are running many @code{mysqld} servers on the same data on a -system that doesn't support good file system locks (normally handled by -the @code{lockd} daemon ) or if you are running -multiple servers with @code{--skip-locking} -@item -You have a crashed index/data file that contains very wrong data that -got @code{mysqld} confused. -@item -You have found a bug in the data storage code. This isn't that likely, -but it's at least possible. In this case you can try to change the file -type to another database handler by using @code{ALTER TABLE} on a -repaired copy of the table! -@end itemize - -Because it is very difficult to know why something is crashing, first try to -check whether or not things that work for others crash for you. Please try -the following things: - -@itemize @bullet -@item -Take down the @code{mysqld} daemon with @code{mysqladmin shutdown}, run -@code{myisamchk --silent --force */*.MYI} on all tables, and restart the -@code{mysqld} daemon. This will ensure that you are running from a clean -state. @xref{Maintenance}. - -@item -Use @code{mysqld --log} and try to determine from the information in the log -whether or not some specific query kills the server. About 95% of all bugs are -related to a particular query! Normally this is one of the last queries in -the log file just before @strong{MySQL} restarted. @xref{Query log}. -If you can repeatadly kill @strong{MySQL} with one of the queries, even -when you have checked all tables just before doing the query, then you -have been able to locate the bug and should do a bug report for this! -@xref{Bug reports}. - -@item -Try to make a test case that we can use to reproduce the problem. -@xref{Reproduceable test case}. - -@item -Try running the included mysql-test test and the @strong{MySQL} -benchmarks. @xref{MySQL test suite}. They should test @strong{MySQL} -rather well. You can also add code that to the benchmarks to simulates -your application! The benchmarks can be found in the @file{bench} -directory in the source distribution or, for a binary distribution, in -the @file{sql-bench} directory under your @strong{MySQL} installation -directory. - -@item -Try @code{fork_test.pl} and @code{fork2_test.pl}. - -@item -If you configure @strong{MySQL} for debugging, it will be much easier to -gather information about possible errors if something goes wrong. -Reconfigure @strong{MySQL} with the @code{--with-debug} option or -@code{--with-debug=full} to @code{configure} and then recompile. -@xref{Debugging server}. - -@item -Configuring @strong{MySQL} for debugging causes a safe memory allocator to be -included that can find some errors. It also provides a lot of output about -what is happening. - -@item -Have you applied the latest patches for your operating system? - -@item -Use the @code{--skip-locking} option to @code{mysqld}. On some systems, the -@code{lockd} lock manager does not work properly; the @code{--skip-locking} -option tells @code{mysqld} not to use external locking. (This means that you -cannot run 2 @code{mysqld} servers on the same data and that you must be -careful if you use @code{myisamchk}, but it may be instructive to try the -option as a test.) - -@item -Have you tried @code{mysqladmin -u root processlist} when @code{mysqld} -appears to be running but not responding? Sometimes @code{mysqld} is not -comatose even though you might think so. The problem may be that all -connections are in use, or there may be some internal lock problem. -@code{mysqladmin processlist} will usually be able to make a connection even -in these cases, and can provide useful information about the current number -of connections and their status. - -@item -Run the command @code{mysqladmin -i 5 status} or @code{mysqladmin -i 5 --r status} or in a separate window to produce statistics while you run -your other queries. - -@item -Try the following: -@enumerate -@item -Start @code{mysqld} from @code{gdb} (or in another debugger). -@xref{Using gdb on mysqld}. - -@item -Run your test scripts. - -@item -Print the backtrace and the local variables at the 3 lowest levels. In gdb you -can do this with the following commands when @code{mysqld} has crashed inside -gdb: - -@example -backtrace -info local -up -info local -up -info local -@end example - -With gdb you can also examine which threads exist with @code{info -threads} and switch to a specific thread with @code{thread #}, where -@code{#} is the thread id. -@end enumerate - -@item -Try to simulate your application with a Perl script to force -@strong{MySQL} to crash or misbehave. - -@item -Send a normal bug report. @xref{Bug reports}. Be even more detailed -than usual. Because @strong{MySQL} works for many people, it may be that the -crash results from something that exists only on your computer (for example, -an error that is related to your particular system libraries). -@item -If you have a problem with tables with dynamic-length rows and you are -not using @code{BLOB/TEXT} columns (but only @code{VARCHAR} columns), you -can try to change all @code{VARCHAR} to @code{CHAR} with @code{ALTER -TABLE}. This will force @strong{MySQL} to use fixed-size rows. -Fixed-size rows take a little extra space, but are much more tolerant to -corruption! - -The current dynamic row code has been in use at @strong{MySQL AB} for at -least 3 years without any problems, but by nature dynamic-length rows are -more prone to errors, so it may be a good idea to try the above to see if -it helps! -@end itemize - -@cindex linking, errors -@cindex errors, linking -@cindex problems, linking -@node Link errors, Common errors, Crashing, Problems -@section Problems When Linking with the MySQL Client Library - -If you are linking your program and you get errors for unreferenced -symbols that start with @code{mysql_}, like the following: - -@example -/tmp/ccFKsdPa.o: In function `main': -/tmp/ccFKsdPa.o(.text+0xb): undefined reference to `mysql_init' -/tmp/ccFKsdPa.o(.text+0x31): undefined reference to `mysql_real_connect' -/tmp/ccFKsdPa.o(.text+0x57): undefined reference to `mysql_real_connect' -/tmp/ccFKsdPa.o(.text+0x69): undefined reference to `mysql_error' -/tmp/ccFKsdPa.o(.text+0x9a): undefined reference to `mysql_close' -@end example - -you should be able to solve this by adding @code{-Lpath-to-the-mysql-library --lmysqlclient} @strong{LAST} on your link line. - -If you get @code{undefined reference} errors for the @code{uncompress} -or @code{compress} function, add @code{-lz} @strong{LAST} on your link -line and try again! - -If you get @code{undefined reference} errors for functions that should -exist on your system, like @code{connect}, check the man page for the -function in question, for which libraries you should add to the link -line! - -If you get @code{undefined reference} errors for functions that don't -exist on your system, like the following: - -@example -mf_format.o(.text+0x201): undefined reference to `__lxstat' -@end example - -it usually means that your library is compiled on a system that is not -100 % compatible with yours. In this case you should download the -latest @strong{MySQL} source distribution and compile this yourself. -@xref{Installing source}. - -If you are trying to run a program and you then get errors for -unreferenced symbols that start with @code{mysql_} or that the -@code{mysqlclient} library can't be found, this means that your system -can't find the share @code{libmysqlclient.so} library. - -The fix for this is to tell your system to search after shared -libraries where the library is located by one of the following methods: - -@itemize @bullet -@item -Add the path to the directory where you have @code{libmysqlclient.so} the -@code{LD_LIBRARY_PATH} environment variable. -@item -Add the path to the directory where you have @code{libmysqlclient.so} the -@code{LD_LIBRARY} environment variable. -@item -Copy @code{libmysqlclient.so} to some place that is searched by your system, -like @file{/lib}, and update the shared library information by executing -@code{ldconfig}. -@end itemize - -Another way to solve this problem is to link your program statically, with -@code{-static}, or by removing the dynamic @strong{MySQL} libraries -before linking your code. In the second case you should be -sure that no other programs are using the dynamic libraries! - -@cindex errors, list of -@node Common errors, Full disk, Link errors, Problems -@section Some Common Errors When Using MySQL - -@menu -* Error Access denied:: @code{Access denied} Error -* Gone away:: @code{MySQL server has gone away} error -* Can not connect to server:: @code{Can't connect to [local] MySQL server} error -* Blocked host:: @code{Host '...' is blocked} error -* Too many connections:: @code{Too many connections} error -* Non-transactional tables:: @code{Some non-transactional changed tables couldn't be rolled back} Error -* Out of memory:: @code{Out of memory} error -* Packet too large:: @code{Packet too large} error -* Communication errors:: Communication errors / Aborted connection -* Full table:: @code{The table is full} error -* Cannot create:: @code{Can't create/write to file} Error -* Commands out of sync:: @code{Commands out of sync} error in client -* Ignoring user:: @code{Ignoring user} error -* Cannot find table:: @code{Table 'xxx' doesn't exist} error -* Cannot initialize character set:: -@end menu - -This section lists some errors that users frequently get. You will find -descriptions of the errors, and how to solve the problem here. - -@cindex errors, access denied -@cindex problems, access denied errors -@cindex access denied errors -@node Error Access denied, Gone away, Common errors, Common errors -@subsection @code{Access denied} Error - -@xref{Privileges}, and especially. @xref{Access denied}. - -@node Gone away, Can not connect to server, Error Access denied, Common errors -@subsection @code{MySQL server has gone away} Error - -This section also covers the related @code{Lost connection to server -during query} error. - -The most common reason for the @code{MySQL server has gone away} error -is that the server timed out and closed the connection. By default, the -server closes the connection after 8 hours if nothing has happened. You -can change the time limit by setting the @code{wait_timeout} variable when -you start @code{mysqld}. - -Another common reason to receive the @code{MySQL server has gone away} error -is because you have issued a ``close'' on your @strong{MySQL} connection -and then tried to run a query on the closed connection. - -You can check that the @strong{MySQL} hasn't died by executing -@code{mysqladmin version} and examining the uptime. - -If you have a script, you just have to issue the query again for the client -to do an automatic reconnection. - -You normally can get the following error codes in this case -(which one you get is OS-dependent): - -@multitable @columnfractions .3 .7 -@item @code{CR_SERVER_GONE_ERROR} @tab The client couldn't send a question to the -server. -@item @code{CR_SERVER_LOST} @tab The client didn't get an error when writing -to the server, but it didn't get a full answer (or any answer) to the question. -@end multitable - -You can also get these errors if you send a query to the server that is -incorrect or too large. If @code{mysqld} gets a packet that is too large -or out of order, it assumes that something has gone wrong with the client and -closes the connection. If you need big queries (for example, if you are -working with big @code{BLOB} columns), you can increase the query limit by -starting @code{mysqld} with the @code{-O max_allowed_packet=#} option -(default 1M). The extra memory is allocated on demand, so @code{mysqld} will -use more memory only when you issue a big query or when @code{mysqld} must -return a big result row! - -@node Can not connect to server, Blocked host, Gone away, Common errors -@subsection @code{Can't connect to [local] MySQL server} error - -A @strong{MySQL} client on Unix can connect to the @code{mysqld} server in two -different ways: Unix sockets, which connect through a file in the file -system (default @file{/tmp/mysqld.sock}) or TCP/IP, which connects -through a port number. Unix sockets are faster than TCP/IP but can only -be used when connecting to a server on the same computer. Unix sockets -are used if you don't specify a hostname or if you specify the special -hostname @code{localhost}. - -On Windows you can connect only with TCP/IP if the @code{mysqld} server -is running on Win95/Win98. If it's running on NT, you can also connect -with named pipes. The name of the named pipe is @strong{MySQL}. If you -don't give a hostname when connecting to @code{mysqld}, a @strong{MySQL} client -will first try to connect to the named pipe, and if this doesn't work it -will connect to the TCP/IP port. You can force the use of named pipes -on Windows by using @code{.} as the hostname. - -The error (2002) @code{Can't connect to ...} normally means that there -isn't a @strong{MySQL} server running on the system or that you are -using a wrong socket file or TCP/IP port when trying to connect to the -@code{mysqld} server. - -Start by checking (using @code{ps} or the task manager on Windows) that -there is a process running named @code{mysqld} on your server! If there -isn't any @code{mysqld} process, you should start one. @xref{Starting -server}. - -If a @code{mysqld} process is running, you can check the server by -trying these different connections (the port number and socket pathname -might be different in your setup, of course): - -@example -shell> mysqladmin version -shell> mysqladmin variables -shell> mysqladmin -h `hostname` version variables -shell> mysqladmin -h `hostname` --port=3306 version -shell> mysqladmin -h 'ip for your host' version -shell> mysqladmin --socket=/tmp/mysql.sock version -@end example - -Note the use of backquotes rather than forward quotes with the @code{hostname} -command; these cause the output of @code{hostname} (that is, the current -hostname) to be substituted into the @code{mysqladmin} command. - -Here are some reasons the @code{Can't connect to local MySQL server} -error might occur: - -@itemize @bullet -@item -@code{mysqld} is not running. -@item -You are running on a system that uses MIT-pthreads. -If you are running on a system that doesn't have native threads, -@code{mysqld} uses the MIT-pthreads package. @xref{Which OS}. However, -all MIT-pthreads versions doesn't support Unix sockets. On a system -without sockets support you must always specify the hostname explicitly -when connecting to the server. Try using this command to check the -connection to the server: -@example -shell> mysqladmin -h `hostname` version -@end example -@item -Someone has removed the Unix socket that @code{mysqld} uses (default -@file{/tmp/mysqld.sock}). You might have a @code{cron} job that removes -the @strong{MySQL} socket (for example, a job that removes old files -from the @file{/tmp} directory). You can always run @code{mysqladmin -version} and check that the socket @code{mysqladmin} is trying to use -really exists. The fix in this case is to change the @code{cron} job to -not remove @file{mysqld.sock} or to place the socket somewhere else. You -can specify a different socket location at @strong{MySQL} configuration -time with this command: -@example -shell> ./configure --with-unix-socket-path=/path/to/socket -@end example -You can also start @code{safe_mysqld} with the -@code{--socket=/path/to/socket} option and set the environment variable -@code{MYSQL_UNIX_PORT} to the socket pathname before starting your -@strong{MySQL} clients. -@item -You have started the @code{mysqld} server with -the @code{--socket=/path/to/socket} option. If you change the socket -pathname for the server, you must also notify the @strong{MySQL} clients -about the new path. You can do this by setting the environment variable -@code{MYSQL_UNIX_PORT} to the socket pathname or by providing the socket path -as an argument to the clients. You can test the socket with this command: - -@example -shell> mysqladmin --socket=/path/to/socket version -@end example -@item -You are using Linux and one thread has died (core dumped). In this case -you must kill the other @code{mysqld} threads (for example, with the -@code{mysql_zap} script before you can start a new @strong{MySQL} -server. @xref{Crashing}. -@item -You may not have read and write privilege to either the directory that holds -the socket file or privilege to the socket file itself. In this case you -have to either change the privilege for the directory / file or restart -@code{mysqld} so that it uses a directory that you can access. -@end itemize - -If you get the error message @code{Can't connect to MySQL server on -some_hostname}, you can try the following things to find out what the -problem is : - -@itemize @bullet -@item -Check if the server is up by doing @code{telnet your-host-name -tcp-ip-port-number} and press @code{RETURN} a couple of times. If there -is a @strong{MySQL} server running on this port you should get a -responses that includes the version number of the running @strong{MySQL} -server. If you get an error like @code{telnet: Unable to connect to -remote host: Connection refused}, then there is no server running on the -given port. -@item -Try connecting to the @code{mysqld} daemon on the local machine and check -the TCP/IP port that @code{mysqld} it's configured to use (variable @code{port}) with -@code{mysqladmin variables}. -@item -Check that your @code{mysqld} server is not started with the -@code{--skip-networking} option. -@end itemize - -@node Blocked host, Too many connections, Can not connect to server, Common errors -@subsection @code{Host '...' is blocked} Error - -If you get an error like this: - -@example -Host 'hostname' is blocked because of many connection errors. -Unblock with 'mysqladmin flush-hosts' -@end example - -this means that @code{mysqld} has gotten a lot (@code{max_connect_errors}) -of connect requests from the host @code{'hostname'} that have been interrupted -in the middle. After @code{max_connect_errors} failed requests, @code{mysqld} -assumes that something is wrong (like an attack from a cracker), and -blocks the site from further connections until someone executes the command -@code{mysqladmin flush-hosts}. - -By default, @code{mysqld} blocks a host after 10 connection errors. -You can easily adjust this by starting the server like this: - -@example -shell> safe_mysqld -O max_connect_errors=10000 & -@end example - -Note that if you get this error message for a given host, you should first -check that there isn't anything wrong with TCP/IP connections from that -host. If your TCP/IP connections aren't working, it won't do you any good to -increase the value of the @code{max_connect_errors} variable! - -@node Too many connections, Non-transactional tables, Blocked host, Common errors -@subsection @code{Too many connections} Error - -If you get the error @code{Too many connections} when you try to connect -to @strong{MySQL}, this means that there is already @code{max_connections} -clients connected to the @code{mysqld} server. - -If you need more connections than the default (100), then you should restart -@code{mysqld} with a bigger value for the @code{max_connections} variable. - -Note that @code{mysqld} actually allows (@code{max_connections}+1) -clients to connect. The last connection is reserved for a user with the -@strong{process} privilege. By not giving this privilege to normal -users (they shouldn't need this), an administrator with this privilege -can log in and use @code{SHOW PROCESSLIST} to find out what could be -wrong. @xref{SHOW}. - -The maximum number of connects @strong{MySQL} is depending on how good -the thread library is on a given platform. Linux or Solaris should be -able to support 500-1000 simultaneous connections, depending on how much -RAM you have and what your clients are doing. - -@cindex Non-transactional tables -@node Non-transactional tables, Out of memory, Too many connections, Common errors -@subsection @code{Some non-transactional changed tables couldn't be rolled back} Error - -If you get the error/warning: @code{Warning: Some non-transactional -changed tables couldn't be rolled back} when trying to do a -@code{ROLLBACK}, this means that some of the tables you used in the -transaction didn't support transactions. These non-transactional tables -will not be affected by the @code{ROLLBACK} statement. - -The most typical case when this happens is when you have tried to create -a table of a type that is not supported by your @code{mysqld} binary. -If @code{mysqld} doesn't support a table type (or if the table type is -disabled by a startup option) , it will instead create the table type -with the table type that is most resembles to the one you requested, -probably @code{MyISAM}. - -You can check the table type for a table by doing: - -@code{SHOW TABLE STATUS LIKE 'table_name'}. @xref{SHOW TABLE STATUS}. - -You can check the extensions your @code{mysqld} binary supports by doing: - -@code{show variables like 'have_%'}. @xref{SHOW VARIABLES}. - -@node Out of memory, Packet too large, Non-transactional tables, Common errors -@subsection @code{Out of memory} Error - -If you issue a query and get something like the following error: - -@example -mysql: Out of memory at line 42, 'malloc.c' -mysql: needed 8136 byte (8k), memory in use: 12481367 bytes (12189k) -ERROR 2008: MySQL client ran out of memory -@end example - -note that the error refers to the @strong{MySQL} client @code{mysql}. The -reason for this error is simply that the client does not have enough memory to -store the whole result. - -To remedy the problem, first check that your query is correct. Is it -reasonable that it should return so many rows? If so, -you can use @code{mysql --quick}, which uses @code{mysql_use_result()} -to retrieve the result set. This places less of a load on the client (but -more on the server). - -@node Packet too large, Communication errors, Out of memory, Common errors -@subsection @code{Packet too large} Error - -When a @strong{MySQL} client or the @code{mysqld} server gets a packet bigger -than @code{max_allowed_packet} bytes, it issues a @code{Packet too large} -error and closes the connection. - -If you are using the @code{mysql} client, you may specify a bigger buffer by -starting the client with @code{mysql --set-variable=max_allowed_packet=8M}. - -If you are using other clients that do not allow you to specify the maximum -packet size (such as @code{DBI}), you need to set the packet size when you -start the server. You cau use a command-line option to @code{mysqld} to set -@code{max_allowed_packet} to a larger size. For example, if you are -expecting to store the full length of a @code{BLOB} into a table, you'll need -to start the server with the @code{--set-variable=max_allowed_packet=16M} -option. - -@cindex aborted clients -@cindex aborted connection -@cindex connection, aborted -@node Communication errors, Full table, Packet too large, Common errors -@subsection Communication Errors / Aborted Connection - -Starting with @code{MySQL 3.23.40} you only get the @code{Aborted -connection} error of you start @code{mysqld} with @code{--warnings}. - -If you find errors like the following in your error log. - -@example -010301 14:38:23 Aborted connection 854 to db: 'users' user: 'josh' -@end example - -@xref{Error log}. - -This means that something of the following has happened: - -@itemize @bullet -@item -The client program did not call @code{mysql_close()} before exit. -@item -The client had been sleeping more than @code{wait_timeout} or -@code{interactive_timeout} without doing any requests. @xref{SHOW -VARIABLES}. -@item -The client program ended abruptly in the middle of the transfer. -@end itemize - -When the above happens, the server variable @code{Aborted_clients} is -incremented. - -The server variable @code{Aborted_connects} is incremented when: - -@itemize @bullet -@item -When a connection packet doesn't contain the right information. -@item -When the user didn't have privileges to connect to a database. -@item -When a user uses a wrong password. -@item -When it takes more than @code{connect_timeout} seconds to get -a connect package. -@end itemize - -Note that the above could indicate that someone is trying to break into -your database! - -@xref{SHOW VARIABLES}. - -Other reasons for problems with Aborted clients / Aborted connections. -@itemize @bullet -@item -Usage of duplex Ethernet protocol, both half and full with -Linux. Many Linux Ethernet drivers have this bug. You should test -for this bug by transferring a huge file via ftp between these two -machines. If a transfer goes in burst-pause-burst-pause ... mode then -you are experiencing a Linux duplex syndrome. The only solution to -this problem is switching of both half and full duplexing on hubs -and switches. -@item -Some problem with the thread library that causes interrupts on reads. -@item -Badly configured TCP/IP. -@item -Faulty Ethernets or hubs or switches, cables ... This can be diagnosed -properly only by replacing hardware. -@end itemize - - -@cindex table is full -@node Full table, Cannot create, Communication errors, Common errors -@subsection @code{The table is full} Error - -This error occurs in older @strong{MySQL} versions when an in-memory temporary -table becomes larger than @code{tmp_table_size} bytes. To avoid this -problem, you can use the @code{-O tmp_table_size=#} option to -@code{mysqld} to increase the temporary table size or use the SQL -option @code{SQL_BIG_TABLES} before you issue the problematic -query. @xref{SET OPTION, , @code{SET OPTION}}. - -You can also start @code{mysqld} with the @code{--big-tables} option. -This is exactly the same as using @code{SQL_BIG_TABLES} for all queries. - -In @strong{MySQL} Version 3.23, in-memory temporary tables will automatically be -converted to a disk-based @code{MyISAM} table after the table size gets -bigger than @code{tmp_table_size}. - -@cindex can't create/write to file -@node Cannot create, Commands out of sync, Full table, Common errors -@subsection @code{Can't create/write to file} Error - -If you get an error for some queries of type: - -@example -Can't create/write to file '\\sqla3fe_0.ism'. -@end example - -this means that @strong{MySQL} can't create a temporary file for the -result set in the given temporary directory. (The above error is a -typical error message on Windows, and the Unix error message is similar.) -The fix is to start @code{mysqld} with @code{--tmpdir=path} or to add to your option -file: - -@example -[mysqld] -tmpdir=C:/temp -@end example - -assuming that the @file{c:\\temp} directory exists. @xref{Option files}. - -Check also the error code that you get with @code{perror}. One reason -may also be a disk full error; - -@example -shell> perror 28 -Error code 28: No space left on device -@end example - -@cindex commands out of sync -@node Commands out of sync, Ignoring user, Cannot create, Common errors -@subsection @code{Commands out of sync} Error in Client - -If you get @code{Commands out of sync; You can't run this command now} -in your client code, you are calling client functions in the wrong order! - -This can happen, for example, if you are using @code{mysql_use_result()} and -try to execute a new query before you have called @code{mysql_free_result()}. -It can also happen if you try to execute two queries that return data without -a @code{mysql_use_result()} or @code{mysql_store_result()} in between. - -@node Ignoring user, Cannot find table, Commands out of sync, Common errors -@subsection @code{Ignoring user} Error - -If you get the following error: - -@code{Found wrong password for user: 'some_user@@some_host'; Ignoring user} - -this means that when @code{mysqld} was started or when it reloaded the -permissions tables, it found an entry in the @code{user} table with -an invalid password. As a result, the entry is simply ignored by the -permission system. - -Possible causes of and fixes for this problem: - -@itemize @bullet -@item -You may be running a new version of @code{mysqld} with an old -@code{user} table. -You can check this by executing @code{mysqlshow mysql user} to see if -the password field is shorter than 16 characters. If so, you can correct this -condition by running the @code{scripts/add_long_password} script. - -@item -The user has an old password (8 characters long) and you didn't start -@code{mysqld} with the @code{--old-protocol} option. -Update the user in the @code{user} table with a new password or -restart @code{mysqld} with @code{--old-protocol}. - -@item -@findex PASSWORD() -You have specified a password in the @code{user} table without using the -@code{PASSWORD()} function. Use @code{mysql} to update the user in the -@code{user} table with a new password. Make sure to use the @code{PASSWORD()} -function: - -@example -mysql> update user set password=PASSWORD('your password') - where user='XXX'; -@end example -@end itemize - -@node Cannot find table, Cannot initialize character set, Ignoring user, Common errors -@subsection @code{Table 'xxx' doesn't exist} Error -If you get the error @code{Table 'xxx' doesn't exist} or @code{Can't -find file: 'xxx' (errno: 2)}, this means that no table exists -in the current database with the name @code{xxx}. -Note that as @strong{MySQL} uses directories and files to store databases and -tables, the database and table names are @strong{case sensitive}! -(On Windows the databases and tables names are not case sensitive, but all -references to a given table within a query must use the same case!) - -You can check which tables you have in the current database with -@code{SHOW TABLES}. @xref{SHOW, , @code{SHOW}}. - -@cindex multibyte character sets -@node Cannot initialize character set, , Cannot find table, Common errors -@subsection @code{Can@'t initialize character set xxx} error. -If you get an error like: - -@example -MySQL Connection Failed: Can't initialize character set xxx -@end example - -This means one of the following things: - -@itemize @bullet -@item -The character set is a multi-byte character set and you have not support -for the character set in the client. - -In this case you need to recompile the client with -@code{--with-charset=xxx} or with @code{--with-extra-charsets=xxx}. -@xref{configure options}. - -All standard @strong{MySQL} binaries are compiled with -@code{--with-extra-character-sets=complex} which will enable support for -all multi-byte character sets. @xref{Character sets}. - -@item -The character set is a simple character set which is not compiled into -@code{mysqld} and the character set definition files is not in the place -where the client expect to find them. - -In this case you need to: - -@itemize @bullet -@item -Recompile the client with support for the character set. -@xref{configure options}. -@item -Specify to the client where the character set definition files are. For many -client you can do this with the -@code{--character-sets-dir=path-to-charset-dir} option. -@item -Copy the character definition files to the path where the client expect them -to be. -@end itemize -@end itemize - -@cindex full disk -@cindex disk full -@node Full disk, Temporary files, Common errors, Problems -@section How MySQL Handles a Full Disk - -@noindent -When a disk-full condition occurs, @strong{MySQL} does the following: - -@itemize @bullet -@item -It checks once every minute to see whether or not there is enough space to -write the current row. If there is enough space, it continues as if nothing had -happened. -@item -Every 6 minutes it writes an entry to the log file warning about the disk -full condition. -@end itemize - -@noindent -To alleviate the problem, you can take the following actions: - -@itemize @bullet -@item -To continue, you only have to free enough disk space to insert all records. -@item -To abort the thread, you must send a @code{mysqladmin kill} to the thread. -The thread will be aborted the next time it checks the disk (in 1 minute). -@item -Note that other threads may be waiting for the table that caused the disk -full condition. If you have several ``locked'' threads, killing the one -thread that is waiting on the disk-full condition will allow the other -threads to continue. -@end itemize - -Exceptions to the above behaveour is when you use @code{REPAIR} or -@code{OPTIMIZE} or when the indexes are created in a batch after an -@code{LOAD DATA INFILE} or after an @code{ALTER TABLE} statement. - -All of the above commands may use big temporary files that left to -themself would cause big problems for the rest of the system. If -@strong{MySQL} gets disk full while doing any of the above operations, -it will remove the big temporary files and mark the table as crashed -(except for @code{ALTER TABLE}, in which the old table will be left -unchanged). - - -@node Temporary files, Problems with mysql.sock, Full disk, Problems -@section Where MySQL Stores Temporary Files - -@strong{MySQL} uses the value of the @code{TMPDIR} environment variable as -the pathname of the directory in which to store temporary files. If you don't -have @code{TMPDIR} set, @strong{MySQL} uses the system default, which is -normally @file{/tmp} or @file{/usr/tmp}. If the file system containing your -temporary file directory is too small, you should edit @code{safe_mysqld} to -set @code{TMPDIR} to point to a directory in a file system where you have -enough space! You can also set the temporary directory using the -@code{--tmpdir} option to @code{mysqld}. - -@strong{MySQL} creates all temporary files as hidden files. This ensures -that the temporary files will be removed if @code{mysqld} is terminated. The -disadvantage of using hidden files is that you will not see a big temporary -file that fills up the file system in which the temporary file directory is -located. - -When sorting (@code{ORDER BY} or @code{GROUP BY}), @strong{MySQL} normally -uses one or two temporary files. The maximum disk-space needed is: - -@example -(length of what is sorted + sizeof(database pointer)) -* number of matched rows -* 2 -@end example - -@code{sizeof(database pointer)} is usually 4, but may grow in the future for -really big tables. - -For some @code{SELECT} queries, @strong{MySQL} also creates temporary SQL -tables. These are not hidden and have names of the form @file{SQL_*}. - -@code{ALTER TABLE} creates a temporary table in the same directory as -the original table. - -@cindex @code{mysql.sock}, protection -@cindex deletion, @code{mysql.sock} -@node Problems with mysql.sock, Changing MySQL user, Temporary files, Problems -@section How to Protect @file{/tmp/mysql.sock} from Being Deleted - -If you have problems with the fact that anyone can delete the -@strong{MySQL} communication socket @file{/tmp/mysql.sock}, you can, -on most versions of Unix, protect your @file{/tmp} file system by setting -the @code{sticky} bit on it. Log in as @code{root} and do the following: - -@example -shell> chmod +t /tmp -@end example - -This will protect your @file{/tmp} file system so that files can be deleted -only by their owners or the superuser (@code{root}). - -You can check if the @code{sticky} bit is set by executing @code{ls -ld /tmp}. -If the last permission bit is @code{t}, the bit is set. - -@cindex starting, @code{mysqld} -@cindex @code{mysqld}, starting -@node Changing MySQL user, Resetting permissions, Problems with mysql.sock, Problems -@section How to Run MySQL As a Normal User - -The @strong{MySQL} server @code{mysqld} can be started and run by any user. -In order to change @code{mysqld} to run as a Unix user @code{user_name}, you must -do the following: - -@enumerate -@item -Stop the server if it's running (use @code{mysqladmin shutdown}). - -@item -Change the database directories and files so that @code{user_name} has -privileges to read and write files in them (you may need to do this as -the Unix @code{root} user): - -@example -shell> chown -R user_name /path/to/mysql/datadir -@end example - -If directories or files within the @strong{MySQL} data directory are -symlinks, you'll also need to follow those links and change the directories -and files they point to. @code{chown -R} may not follow symlinks for -you. - -@item -Start the server as user @code{user_name}, or, if you are using -@strong{MySQL} Version 3.22 or later, start @code{mysqld} as the Unix @code{root} -user and use the @code{--user=user_name} option. @code{mysqld} will switch -to run as the Unix user @code{user_name} before accepting any connections. - -@item -To start the server as the given user name automatically at system -startup time, add a @code{user} line that specifies the user name to -the @code{[mysqld]} group of the @file{/etc/my.cnf} option file or the -@file{my.cnf} option file in the server's data directory. For example: - -@example -[mysqld] -user=user_name -@end example -@end enumerate - -At this point, your @code{mysqld} process should be running fine and dandy as -the Unix user @code{user_name}. One thing hasn't changed, though: the -contents of the permissions tables. By default (right after running the -permissions table install script @code{mysql_install_db}), the @strong{MySQL} -user @code{root} is the only user with permission to access the @code{mysql} -database or to create or drop databases. Unless you have changed those -permissions, they still hold. This shouldn't stop you from accessing -@strong{MySQL} as the @strong{MySQL} @code{root} user when you're logged in -as a Unix user other than @code{root}; just specify the @code{-u root} option -to the client program. - -Note that accessing @strong{MySQL} as @code{root}, by supplying @code{-u -root} on the command line, has @emph{nothing} to do with @strong{MySQL} running -as the Unix @code{root} user, or, indeed, as another Unix user. The access -permissions and user names of @strong{MySQL} are completely separate from -Unix user names. The only connection with Unix user names is that if you -don't provide a @code{-u} option when you invoke a client program, the client -will try to connect using your Unix login name as your @strong{MySQL} user -name. - -If your Unix box itself isn't secured, you should probably at least put a -password on the @strong{MySQL} @code{root} users in the access tables. -Otherwise, any user with an account on that machine can run @code{mysql -u -root db_name} and do whatever he likes. - -@cindex passwords, forgotten -@cindex passwords, resetting -@cindex root user, password resetting -@node Resetting permissions, File permissions , Changing MySQL user, Problems -@section How to Reset a Forgotten Password - -If you have forgotten the @code{root} user password for @strong{MySQL}, you -can restore it with the following procedure: - -@enumerate -@item -Take down the @code{mysqld} server by sending a @code{kill} (not @code{kill --9}) to the @code{mysqld} server. The pid is stored in a @code{.pid} -file, which is normally in the @strong{MySQL} database directory: - -@example -kill `cat /mysql-data-directory/hostname.pid` -@end example - -You must be either the Unix @code{root} user or the same user the server -runs as to do this. - -@item -Restart @code{mysqld} with the @code{--skip-grant-tables} option. -@item -Connect to the @code{mysqld} server with @code{mysql -h hostname mysql} and change -the password with a @code{GRANT} command. @xref{GRANT,,@code{GRANT}}. -You can also do this with -@code{mysqladmin -h hostname -u user password 'new password'} -@item -Load the privilege tables with: @code{mysqladmin -h hostname -flush-privileges} or with the SQL command @code{FLUSH PRIVILEGES}. -@end enumerate - -Note that after you started @code{mysqld} with @code{--skip-grant-tables}, -any usage of @code{GRANT} commands will give you an @code{Unknown command} -error until you have executed @code{FLUSH PRIVILEGES}. - -@cindex files, permissions -@cindex error mesaages, can't find file -@cindex files, not found message -@node File permissions , Not enough file handles, Resetting permissions, Problems -@section Problems with File Permissions - -If you have problems with file permissions, for example, if @code{mysql} -issues the following error message when you create a table: - -@example -ERROR: Can't find file: 'path/with/filename.frm' (Errcode: 13) -@end example - -@tindex UMASK environment variable -@tindex Environment variable, UMASK -then the environment variable @code{UMASK} might be set incorrectly when -@code{mysqld} starts up. The default umask value is @code{0660}. You can -change this behavior by starting @code{safe_mysqld} as follows: - -@example -shell> UMASK=384 # = 600 in octal -shell> export UMASK -shell> /path/to/safe_mysqld & -@end example - -@tindex UMASK_DIR environment variable -@tindex Environment variable, UMASK_DIR -By default @strong{MySQL} will create database and @code{RAID} -directories with permission type 0700. You can modify this behavior by -setting the @code{UMASK_DIR} variable. If you set this, new -directories are created with the combined @code{UMASK} and -@code{UMASK_DIR}. For example, if you want to give group access to -all new directories, you can do: - -@example -shell> UMASK_DIR=504 # = 770 in octal -shell> export UMASK_DIR -shell> /path/to/safe_mysqld & -@end example - -In @strong{MySQL} Version 3.23.25 and above, @strong{MySQL} assumes that the -value for @code{UMASK} and @code{UMASK_DIR} is in octal if it starts -with a zero. - -@xref{Environment variables}. - -@node Not enough file handles, Using DATE, File permissions , Problems -@section File Not Found - -If you get @code{ERROR '...' not found (errno: 23)}, @code{Can't open -file: ... (errno: 24)}, or any other error with @code{errno 23} or -@code{errno 24} from @strong{MySQL}, it means that you haven't allocated -enough file descriptors for @strong{MySQL}. You can use the -@code{perror} utility to get a description of what the error number -means: - -@example -shell> perror 23 -File table overflow -shell> perror 24 -Too many open files -shell> perror 11 -Resource temporarily unavailable -@end example - -The problem here is that @code{mysqld} is trying to keep open too many -files simultaneously. You can either tell @code{mysqld} not to open so -many files at once or increase the number of file descriptors -available to @code{mysqld}. - -To tell @code{mysqld} to keep open fewer files at a time, you can make -the table cache smaller by using the @code{-O table_cache=32} option to -@code{safe_mysqld} (the default value is 64). Reducing the value of -@code{max_connections} will also reduce the number of open files (the -default value is 90). - -@tindex ulimit -To change the number of file descriptors available to @code{mysqld}, you -can use the option @code{--open-files-limit=#} to @code{safe_mysqld} or -@code{-O open-files-limit=#} to @code{mysqld}. @xref{SHOW VARIABLES}. -The easiest way to do that is to add the option to your option file. -@xref{Option files}. If you have an old @code{mysqld} version that -doesn't support this, you can edit the @code{safe_mysqld} script. There -is a commented-out line @code{ulimit -n 256} in the script. You can -remove the @code{'#'} character to uncomment this line, and change the -number 256 to affect the number of file descriptors available to -@code{mysqld}. - -@code{ulimit} (and @code{open-files-limit}) can increase the number of -file descriptors, but only up to the limit imposed by the operating -system. There is also a 'hard' limit that can only be overrided if you -start @code{safe_mysqld} or @code{mysqld} as root (Just remember that -you need to also use the @code{--user=..} option in this case). If you -need to increase the OS limit on the number of file descriptors -available to each process, consult the documentation for your operating -system. - -Note that if you run the @code{tcsh} shell, @code{ulimit} will not work! -@code{tcsh} will also report incorrect values when you ask for the current -limits! In this case you should start @code{safe_mysqld} with @code{sh}! - -@findex DATE -@cindex DATE columns, problems -@cindex problems, @code{DATE} columns -@node Using DATE, Timezone problems, Not enough file handles, Problems -@section Problems Using @code{DATE} Columns - -The format of a @code{DATE} value is @code{'YYYY-MM-DD'}. According to ANSI -SQL, no other format is allowed. You should use this format in @code{UPDATE} -expressions and in the WHERE clause of @code{SELECT} statements. For -example: - -@example -mysql> SELECT * FROM tbl_name WHERE date >= '1997-05-05'; -@end example - -As a convenience, @strong{MySQL} automatically converts a date to a number if -the date is used in a numeric context (and vice versa). It is also smart -enough to allow a ``relaxed'' string form when updating and in a @code{WHERE} -clause that compares a date to a @code{TIMESTAMP}, @code{DATE}, or a -@code{DATETIME} column. (Relaxed form means that any punctuation character -may be used as the separator between parts. For example, @code{'1998-08-15'} -and @code{'1998#08#15'} are equivalent.) @strong{MySQL} can also convert a -string containing no separators (such as @code{'19980815'}), provided it -makes sense as a date. - -The special date @code{'0000-00-00'} can be stored and retrieved as -@code{'0000-00-00'.} When using a @code{'0000-00-00'} date through -@strong{MyODBC}, it will automatically be converted to @code{NULL} in -@strong{MyODBC} Version 2.50.12 and above, because ODBC can't handle this kind of -date. - -Because @strong{MySQL} performs the conversions described above, the following -statements work: - -@example -mysql> INSERT INTO tbl_name (idate) VALUES (19970505); -mysql> INSERT INTO tbl_name (idate) VALUES ('19970505'); -mysql> INSERT INTO tbl_name (idate) VALUES ('97-05-05'); -mysql> INSERT INTO tbl_name (idate) VALUES ('1997.05.05'); -mysql> INSERT INTO tbl_name (idate) VALUES ('1997 05 05'); -mysql> INSERT INTO tbl_name (idate) VALUES ('0000-00-00'); - -mysql> SELECT idate FROM tbl_name WHERE idate >= '1997-05-05'; -mysql> SELECT idate FROM tbl_name WHERE idate >= 19970505; -mysql> SELECT mod(idate,100) FROM tbl_name WHERE idate >= 19970505; -mysql> SELECT idate FROM tbl_name WHERE idate >= '19970505'; -@end example - -@noindent -However, the following will not work: - -@example -mysql> SELECT idate FROM tbl_name WHERE STRCMP(idate,'19970505')=0; -@end example - -@code{STRCMP()} is a string function, so it converts @code{idate} to -a string and performs a string comparison. It does not convert -@code{'19970505'} to a date and perform a date comparison. - -Note that @strong{MySQL} does no checking whether or not the date is -correct. If you store an incorrect date, such as @code{'1998-2-31'}, the -wrong date will be stored. If the date cannot be converted to any reasonable -value, a @code{0} is stored in the @code{DATE} field. This is mainly a speed -issue and we think it is up to the application to check the dates, and not -the server. - -@cindex timezone problems -@cindex problems, timezone -@tindex TZ environment variable -@tindex Environment variable, TZ -@node Timezone problems, Case sensitivity, Using DATE, Problems -@section Time Zone Problems - -If you have a problem with @code{SELECT NOW()} returning values in GMT and -not your local time, you have to set the @code{TZ} environment variable to -your current time zone. This should be done for the environment in which -the server runs, for example, in @code{safe_mysqld} or @code{mysql.server}. -@xref{Environment variables}. - -@node Case sensitivity, Problems with NULL, Timezone problems, Problems -@section Case Sensitivity in Searches - -@cindex case sensitivity, in searches -@cindex searching, and case-sensitivity -@cindex Chinese -@cindex Big5 Chinese character encoding - -By default, @strong{MySQL} searches are case-insensitive (although there are -some character sets that are never case insensitive, such as @code{czech}). -That means that if you search with @code{col_name LIKE 'a%'}, you will get all -column values that start with @code{A} or @code{a}. If you want to make this -search case-sensitive, use something like @code{INDEX(col_name, "A")=0} to -check a prefix. Or use @code{STRCMP(col_name, "A") = 0} if the column value -must be exactly @code{"A"}. - -Simple comparison operations (@code{>=, >, = , < , <=}, sorting and -grouping) are based on each character's ``sort value''. Characters with -the same sort value (like E, e and é) are treated as the same character! - -In older @strong{MySQL} versions @code{LIKE} comparisons where done on -the uppercase value of each character (E == e but E <> é). In newer -@strong{MySQL} versions @code{LIKE} works just like the other comparison -operators. - -If you want a column always to be treated in case-sensitive fashion, -declare it as @code{BINARY}. @xref{CREATE TABLE, , @code{CREATE TABLE}}. - -If you are using Chinese data in the so-called big5 encoding, you want to -make all character columns @code{BINARY}. This works because the sorting -order of big5 encoding characters is based on the order of ASCII codes. - -@cindex @code{NULL} values, vs. empty values -@tindex NULL -@node Problems with NULL, Problems with alias, Case sensitivity, Problems -@section Problems with @code{NULL} Values - -The concept of the @code{NULL} value is a common source of confusion for -newcomers to SQL, who often think that @code{NULL} is the same thing as an -empty string @code{''}. This is not the case! For example, the following -statements are completely different: - -@example -mysql> INSERT INTO my_table (phone) VALUES (NULL); -mysql> INSERT INTO my_table (phone) VALUES (""); -@end example - -Both statements insert a value into the @code{phone} column, but the first -inserts a @code{NULL} value and the second inserts an empty string. The -meaning of the first can be regarded as ``phone number is not known'' and the -meaning of the second can be regarded as ``she has no phone''. - -In SQL, the @code{NULL} value is always false in comparison to any -other value, even @code{NULL}. An expression that contains @code{NULL} -always produces a @code{NULL} value unless otherwise indicated in -the documentation for the operators and functions involved in the -expression. All columns in the following example return @code{NULL}: - -@example -mysql> SELECT NULL,1+NULL,CONCAT('Invisible',NULL); -@end example - -If you want to search for column values that are @code{NULL}, you -cannot use the @code{=NULL} test. The following statement returns no -rows, because @code{expr = NULL} is FALSE, for any expression: - -@example -mysql> SELECT * FROM my_table WHERE phone = NULL; -@end example - -To look for @code{NULL} values, you must use the @code{IS NULL} test. -The following shows how to find the @code{NULL} phone number and the -empty phone number: - -@example -mysql> SELECT * FROM my_table WHERE phone IS NULL; -mysql> SELECT * FROM my_table WHERE phone = ""; -@end example - -In @strong{MySQL}, as in many other SQL servers, you can't index -columns that can have @code{NULL} values. You must declare such columns -@code{NOT NULL}. Conversely, you cannot insert @code{NULL} into an indexed -column. - -@findex LOAD DATA INFILE -When reading data with @code{LOAD DATA INFILE}, empty columns are updated -with @code{''}. If you want a @code{NULL} value in a column, you should use -@code{\N} in the text file. The literal word @code{'NULL'} may also be used -under some circumstances. -@xref{LOAD DATA, , @code{LOAD DATA}}. - -When using @code{ORDER BY}, @code{NULL} values are presented first. If you -sort in descending order using @code{DESC}, @code{NULL} values are presented -last. When using @code{GROUP BY}, all @code{NULL} values are regarded as -equal. - -To help with @code{NULL} handling, you can use the @code{IS NULL} and -@code{IS NOT NULL} operators and the @code{IFNULL()} function. - -@cindex @code{TIMESTAMP}, and @code{NULL} values -@cindex @code{AUTO_INCREMENT}, and @code{NULL} values -@cindex @code{NULL} values, and @code{TIMESTAMP} columns -@cindex @code{NULL} values, and @code{AUTO_INCREMENT} columns -For some column types, @code{NULL} values are handled specially. If you -insert @code{NULL} into the first @code{TIMESTAMP} column of a table, the -current date and time is inserted. If you insert @code{NULL} into an -@code{AUTO_INCREMENT} column, the next number in the sequence is inserted. - -@tindex alias -@node Problems with alias, Deleting from related tables, Problems with NULL, Problems -@section Problems with @code{alias} - -You can use an alias to refer to a column in the @code{GROUP BY}, -@code{ORDER BY}, or in the @code{HAVING} part. Aliases can also be used -to give columns better names: - -@example -SELECT SQRT(a*b) as rt FROM table_name GROUP BY rt HAVING rt > 0; -SELECT id,COUNT(*) AS cnt FROM table_name GROUP BY id HAVING cnt > 0; -SELECT id AS "Customer identity" FROM table_name; -@end example - -Note that ANSI SQL doesn't allow you to refer to an alias in a -@code{WHERE} clause. This is because when the @code{WHERE} code is -executed the column value may not yet be determined. For example, the -following query is @strong{illegal}: - -@example -SELECT id,COUNT(*) AS cnt FROM table_name WHERE cnt > 0 GROUP BY id; -@end example - -The @code{WHERE} statement is executed to determine which rows should -be included in the @code{GROUP BY} part while @code{HAVING} is used to -decide which rows from the result set should be used. - -@cindex deleting, rows -@cindex rows, deleting -@cindex tables, deleting rows -@node Deleting from related tables, No matching rows, Problems with alias, Problems -@section Deleting Rows from Related Tables - -As @strong{MySQL} doesn't support sub-selects or use of more than one table -in the @code{DELETE} statement, you should use the following approach to -delete rows from 2 related tables: - -@enumerate -@item -@code{SELECT} the rows based on some @code{WHERE} condition in the main table. -@item -@code{DELETE} the rows in the main table based on the same condition. -@item -@code{DELETE FROM related_table WHERE related_column IN (selected_rows)}. -@end enumerate - -If the total number of characters in the query with -@code{related_column} is more than 1,048,576 (the default value of -@code{max_allowed_packet}, you should split it into smaller parts and -execute multiple @code{DELETE} statements. You will probably get the -fastest @code{DELETE} by only deleting 100-1000 @code{related_column} -id's per query if the @code{related_column} is an index. If the -@code{related_column} isn't an index, the speed is independent of the -number of arguments in the @code{IN} clause. - -@cindex no matching rows -@cindex rows, matching problems -@node No matching rows, ALTER TABLE problems, Deleting from related tables, Problems -@section Solving Problems with No Matching Rows - -If you have a complicated query that has many tables and that doesn't -return any rows, you should use the following procedure to find out what -is wrong with your query: - -@enumerate -@item -Test the query with @code{EXPLAIN} and check if you can find something that is -obviously wrong. @xref{EXPLAIN, , @code{EXPLAIN}}. - -@item -Select only those fields that are used in the @code{WHERE} clause. - -@item -Remove one table at a time from the query until it returns some rows. -If the tables are big, it's a good idea to use @code{LIMIT 10} with the query. - -@item -Do a @code{SELECT} for the column that should have matched a row against -the table that was last removed from the query. - -@item -If you are comparing @code{FLOAT} or @code{DOUBLE} columns with numbers that -have decimals, you can't use @code{=}! This problem is common in most -computer languages because floating-point values are not exact values: - -@example -mysql> SELECT * FROM table_name WHERE float_column=3.5; - -> -mysql> SELECT * FROM table_name WHERE float_column between 3.45 and 3.55; -@end example - -In most cases, changing the @code{FLOAT} to a @code{DOUBLE} will fix this! - -@item -If you still can't figure out what's wrong, create a minimal test that can -be run with @code{mysql test < query.sql} that shows your problems. -You can create a test file with @code{mysqldump --quick database tables > query.sql}. Open the file in an editor, remove some insert lines (if there are -too many of these), and add your select statement at the end of the file. - -Test that you still have your problem by doing: - -@example -shell> mysqladmin create test2 -shell> mysql test2 < query.sql -@end example - -Post the test file using @code{mysqlbug} to @email{mysql@@lists.mysql.com}. -@end enumerate - -@tindex ALTER TABLE -@node ALTER TABLE problems, Change column order, No matching rows, Problems -@section Problems with @code{ALTER TABLE}. - -@code{ALTER TABLE} changes a table to the current character set. -If you during @code{ALTER TABLE} get a duplicate key error, then the cause -is either that the new character sets maps to keys to the same value -or that the table is corrupted, in which case you should run -@code{REPAIR TABLE} on the table. - -If @code{ALTER TABLE} dies with an error like this: - -@example -Error on rename of './database/name.frm' to './database/B-a.frm' (Errcode: 17) -@end example - -the problem may be that @strong{MySQL} has crashed in a previous @code{ALTER -TABLE} and there is an old table named @file{A-something} or -@file{B-something} lying around. In this case, go to the @strong{MySQL} data -directory and delete all files that have names starting with @code{A-} or -@code{B-}. (You may want to move them elsewhere instead of deleting them.) - -@code{ALTER TABLE} works the following way: - -@itemize @bullet -@item Create a new table named @file{A-xxx} with the requested changes. -@item All rows from the old table are copied to @file{A-xxx}. -@item The old table is renamed @file{B-xxx}. -@item @file{A-xxx} is renamed to your old table name. -@item @file{B-xxx} is deleted. -@end itemize - -If something goes wrong with the renaming operation, @strong{MySQL} tries to -undo the changes. If something goes seriously wrong (this shouldn't happen, -of course), @strong{MySQL} may leave the old table as @file{B-xxx}, but a -simple rename on the system level should get your data back. - -@cindex reordering, columns -@cindex columns, changing -@cindex changing, column order -@cindex tables, changing column order -@node Change column order, Temporary table problems, ALTER TABLE problems, Problems -@section How To Change the Order of Columns in a Table - -The whole point of SQL is to abstract the application from the data -storage format. You should always specify the order in which you wish to -retrieve your data. For example: - -@example -SELECT col_name1, col_name2, col_name3 FROM tbl_name; -@end example - -will return columns in the order @code{col_name1}, @code{col_name2}, @code{col_name3}, whereas: - -@example -SELECT col_name1, col_name3, col_name2 FROM tbl_name; -@end example - -will return columns in the order @code{col_name1}, @code{col_name3}, @code{col_name2}. - -You should @strong{NEVER}, in an application, use @code{SELECT *} and -retrieve the columns based on their position, because the order in which -columns are returned @strong{CANNOT} be guaranteed over time. A simple -change to your database may cause your application to fail rather -dramatically. - -If you want to change the order of columns anyway, you can do it as follows: - -@enumerate -@item -Create a new table with the columns in the right order. -@item -Execute -@code{INSERT INTO new_table SELECT fields-in-new_table-order FROM old_table}. -@item -Drop or rename @code{old_table}. -@item -@code{ALTER TABLE new_table RENAME old_table}. -@end enumerate - -@cindex temporary tables, problems -@node Temporary table problems, , Change column order, Problems -@section TEMPORARY TABLE problems - -The following are a list of the limitations with @code{TEMPORARY TABLES}. - -@itemize @bullet -@item -A temporary table can only be of type @code{HEAP}, @code{ISAM} or -@code{MyISAM}. -@item -You can't use temporary tables more than once in the same query. -For example, the following doesn't work. - -@example -select * from temporary_table, temporary_table as t2; -@end example - -We plan to fix the above in 4.0. -@item -You can't use @code{RENAME} on a @code{TEMPORARY} table. -Note that @code{ALTER TABLE org_name RENAME new_name} works! - -We plan to fix the above in 4.0. -@end itemize - -@cindex problems, solving -@cindex solving, problems -@cindex databases, replicating -@node Common problems, Clients, Problems, Top -@chapter Solving Some Common Problems with MySQL - -@cindex replication -@menu -* Log Replication:: Database replication with update log -@end menu - -In this chapter, you will find information to solve some of the more common -tasks with @strong{MySQL}. This includes making backups, running more than -one @strong{MySQL} server daemon on a single machine, and replicating a -database using the update or binary logs. - -@cindex database replication -@cindex replication, database -@node Log Replication, , Common problems, Common problems -@section Database Replication with Update Log - -Now that master-slave internal replication is available starting in -Version 3.23.15, using the update log to implement replications is not -recommended. @xref{Replication}. - -However, it is still possible to replicate a database by using the -update log or the binary log. @xref{Update log}. This requires one -database that acts as a master (to which data changes are made) and one -or more other databases that act as slaves. To update a slave, just run -@code{mysql < update_log.*} or @code{mysqlbinlog binary_log.* | mysql}. -Supply host, user, and password options that are appropriate for the -slave database, and use the update log from the master database as -input. - -If you never delete anything from a table, you can use a @code{TIMESTAMP} -column to find out which rows have been inserted or changed in the table -since the last replication (by comparing the time when you did the -replication last time) and only copy these rows to the mirror. - -It is possible to make a two-way updating system using both the update -log (for deletes) and timestamps (on both sides). But in that case you -must be able to handle conflicts when the same data have been changed in -both ends. You probably want to keep the old version to help with -deciding what has been updated. - -Because replication in this case is done with SQL statements, you should not -use the following functions in statements that update the database; they may -not return the same value as in the original database: - -@itemize @bullet -@item @code{DATABASE()} -@item @code{GET_LOCK()} and @code{RELEASE_LOCK()} -@item @code{RAND()} -@item @code{USER()}, @code{SYSTEM_USER()} or @code{SESSION_USER()} -@item @code{VERSION()}, @code{CONNECT_ID()} -@end itemize - -All time functions are safe to use, as the timestamp is sent to the -mirror if needed. @code{LAST_INSERT_ID()} is also safe to use. - - -@node Clients, MySQL internals, Common problems, Top +@node Clients, Problems, Common programs, Top @chapter MySQL APIs @cindex client tools @@ -43661,284 +42360,1718 @@ interface that is based on msqltcl 1.50. -@cindex internals -@cindex threads -@node MySQL internals, Environment variables, Clients, Top -@chapter MySQL Internals -This chapter describes a lot of things that you need to know when -working on the @strong{MySQL} code. If you plan to contribute to MySQL -development, want to have access to the bleeding-edge in-between -versions code, or just want to keep track of development, follow the -instructions in @xref{Installing source tree}. If you are interested in MySQL -internals, you should also subscribe to @email{internals@@lists.mysql.com}. -This is a relatively low traffic list, in comparison with -@email{mysql@@lists.mysql.com}. +@node Problems, Environment variables, Clients, Top +@appendix Problems and Common Errors + +@cindex problems, common errors +@cindex errors, common @menu -* MySQL threads:: MySQL threads -* MySQL test suite:: MySQL test suite +* What is crashing:: How to determine what is causing problems +* Crashing:: What to do if @strong{MySQL} keeps crashing +* Link errors:: Problems when linking with the @strong{MySQL} client library +* Common errors:: Some common errors when using @strong{MySQL} +* Full disk:: How @strong{MySQL} handles a full disk +* Temporary files:: Where @strong{MySQL} stores temporary files +* Problems with mysql.sock:: How to protect @file{/tmp/mysql.sock} +* Changing MySQL user:: How to run @strong{MySQL} as a normal user +* Resetting permissions:: How to reset a forgotten password. +* File permissions :: Problems with file permissions +* Not enough file handles:: File not found +* Using DATE:: Problems using @code{DATE} columns +* Timezone problems:: Timezone problems +* Case sensitivity:: Case sensitivity in searches +* Problems with NULL:: Problems with @code{NULL} values +* Problems with alias:: Problems with @code{alias} +* Deleting from related tables:: Deleting rows from related tables +* No matching rows:: Solving problems with no matching rows +* ALTER TABLE problems:: Problems with @code{ALTER TABLE}. +* Change column order:: How to change the order of columns in a table +* Temporary table problems:: @end menu -@node MySQL threads, MySQL test suite, MySQL internals, MySQL internals -@section MySQL Threads +This chapter lists some common problems and error messages that users have +run into. You will learn how to figure out what the problem is, and what +to do to solve it. You will also find proper solutions to some common +problems. -The @strong{MySQL} server creates the following threads: + +@node What is crashing, Crashing, Problems, Problems +@appendixsec How to Determine What Is Causing Problems + +When you run into problems, the first thing you should do is to find out +which program / piece of equipment is causing problems: @itemize @bullet +@item +If you have one of the following symptoms, then it is probably a hardware +(like memory, motherboard, CPU, or hard disk) or kernel problem: +@itemize @minus +@item +The keyboard doesn't work. This can normally be checked by pressing +Caps Lock. If the Caps Lock light doesn't change you have to replace +your keyboard. (Before doing this, you should try to reboot +your computer and check all cables to the keyboard.) +@item +The mouse pointer doesn't move. +@item +The machine doesn't answer to a remote machine's pings. +@item +Different, unrelated programs don't behave correctly. +@item +If your system rebooted unexpectedly (a faulty user level program should +NEVER be able to take down your system). +@end itemize + +In this case you should start by checking all your cables and run some +diagnostic tool to check your hardware! +You should also check if there are any patches, updates, or service +packs for your operating system that could likely solve your problems. +Check also that all your libraries (like glibc) are up to date. +It's always good to use a machine with ECC memory to discover +memory problems early! @item -The TCP/IP connection thread handles all connection requests and -creates a new dedicated thread to handle the authentication and -and SQL query processing for each connection. +If your keyboard is locked up, you may be able to fix this by +logging into your machine from another machine and execute +@code{kbd_mode -a} on it. @item -On Windows NT there is a named pipe handler thread that does the same work as -the TCP/IP connection thread on named pipe connect requests. +Please examine your system log file (/var/log/messages or similar) for +reasons for your problems. If you think the problem is in @strong{MySQL} +then you should also examine @strong{MySQL}'s log files. @xref{Update log}. @item -The signal thread handles all signals. This thread also normally handles -alarms and calls @code{process_alarm()} to force timeouts on connections -that have been idle too long. +If you don't think you have hardware problems, you should try to find +out which program is causing problems. + +Try using @code{top}, @code{ps}, @code{taskmanager}, or some similar program, +to check which program is taking all CPU or is locking the machine. @item -If @code{mysqld} is compiled with @code{-DUSE_ALARM_THREAD}, a dedicated -thread that handles alarms is created. This is only used on some systems where -there are problems with @code{sigwait()} or if one wants to use the -@code{thr_alarm()} code in ones application without a dedicated signal -handling thread. +Check with @code{top}, @code{df}, or a similar program if you are out of +memory, disk space, open files, or some other critical resource. @item -If one uses the @code{--flush_time=#} option, a dedicated thread is created -to flush all tables at the given interval. +If the problem is some runaway process, you can always try to kill it. If it +doesn't want to die, there is probably a bug in the operating system. +@end itemize + +If after you have examined all other possibilities and you have +concluded that it's the @strong{MySQL} server or a @strong{MySQL} client +that is causing the problem, it's time to do a bug report for our +mailing list or our support team. In the bug report, try to give a +very detailed description of how the system is behaving and what you think is +happening. You should also state why you think it's @strong{MySQL} that +is causing the problems. Take into consideration all the situations in +this chapter. State any problems exactly how they appear when you +examine your system. Use the 'cut and paste' method for any output +and/or error messages from programs and/or log files! +Try to describe in detail which program is not working and all +symptoms you see! We have in the past received many bug reports that just +state "the system doesn't work". This doesn't provide us with any +information about what could be the problem. + +If a program fails, it's always useful to know: + +@itemize @bullet @item -Every connection has its own thread. +Has the program in question made a segmentation fault (core dumped)? +@item +Is the program taking the whole CPU? Check with @code{top}. Let the +program run for a while, it may be evaluating something heavy. +@item +If it's the @code{mysqld} server that is causing problems, can you +do @code{mysqladmin -u root ping} or @code{mysqladmin -u root processlist}? +@item +What does a client program say (try with @code{mysql}, for example) +when you try to connect to the @strong{MySQL} server? +Does the client jam? Do you get any output from the program? +@end itemize + +When sending a bug report, you should of follow the outlines +described in this manual. @xref{Asking questions}. + + +@node Crashing, Link errors, What is crashing, Problems +@appendixsec What to Do if MySQL Keeps Crashing + +@cindex crash, repeated +All @strong{MySQL} versions are tested on many platforms before they are +released. This doesn't mean that there aren't any bugs in +@strong{MySQL}, but it means if there are bugs, they are very few and can be +hard to find. If you have a problem, it will always help if you try to +find out exactly what crashes your system, as you will have a much better +chance of getting this fixed quickly. + +First, you should try to find out whether the problem is that the +@code{mysqld} daemon dies or whether your problem has to do with your +client. You can check how long your @code{mysqld} server has been up by +executing @code{mysqladmin version}. If @code{mysqld} has died, you may +find the reason for this in the file +@file{mysql-data-directory/`hostname`.err}. @xref{Error log}. + +Many crashes of @strong{MySQL} are caused by corrupted index / data +files. @strong{MySQL} will update the data on disk, with the +@code{write()} system call, after every SQL statement and before the +client is notified about the result. (This is not true if you are running +with @code{delayed_key_writes}, in which case only the data is written.) +This means that the data is safe even if @code{mysqld} crashes, as the OS will +ensure that the not flushed data is written to disk. You can force +@strong{MySQL} to sync everything to disk after every SQL command by +starting @code{mysqld} with @code{--flush}. + +The above means that normally you shouldn't get corrupted tables unless: + +@itemize @bullet @item -Every different table on which one uses @code{INSERT DELAYED} gets its -own thread. +Someone/something killed @code{mysqld} or the machine in the middle +of an update. +@item +You have found a bug in @code{mysqld} that caused it to die in the +middle of an update. +@item +Someone is manipulating the data/index files outside of @strong{mysqld} +without locking the table properly. +@item +If you are running many @code{mysqld} servers on the same data on a +system that doesn't support good file system locks (normally handled by +the @code{lockd} daemon ) or if you are running +multiple servers with @code{--skip-locking} +@item +You have a crashed index/data file that contains very wrong data that +got @code{mysqld} confused. +@item +You have found a bug in the data storage code. This isn't that likely, +but it's at least possible. In this case you can try to change the file +type to another database handler by using @code{ALTER TABLE} on a +repaired copy of the table! +@end itemize +Because it is very difficult to know why something is crashing, first try to +check whether or not things that work for others crash for you. Please try +the following things: + +@itemize @bullet @item -If you use @code{--master-host}, a slave replication thread will be -started to read and apply updates from the master. +Take down the @code{mysqld} daemon with @code{mysqladmin shutdown}, run +@code{myisamchk --silent --force */*.MYI} on all tables, and restart the +@code{mysqld} daemon. This will ensure that you are running from a clean +state. @xref{MySQL Database Administration}. + +@item +Use @code{mysqld --log} and try to determine from the information in the log +whether or not some specific query kills the server. About 95% of all bugs are +related to a particular query! Normally this is one of the last queries in +the log file just before @strong{MySQL} restarted. @xref{Query log}. +If you can repeatadly kill @strong{MySQL} with one of the queries, even +when you have checked all tables just before doing the query, then you +have been able to locate the bug and should do a bug report for this! +@xref{Bug reports}. + +@item +Try to make a test case that we can use to reproduce the problem. +@xref{Reproduceable test case}. + +@item +Try running the included mysql-test test and the @strong{MySQL} +benchmarks. @xref{MySQL test suite}. They should test @strong{MySQL} +rather well. You can also add code that to the benchmarks to simulates +your application! The benchmarks can be found in the @file{bench} +directory in the source distribution or, for a binary distribution, in +the @file{sql-bench} directory under your @strong{MySQL} installation +directory. + +@item +Try @code{fork_test.pl} and @code{fork2_test.pl}. + +@item +If you configure @strong{MySQL} for debugging, it will be much easier to +gather information about possible errors if something goes wrong. +Reconfigure @strong{MySQL} with the @code{--with-debug} option or +@code{--with-debug=full} to @code{configure} and then recompile. +@xref{Debugging server}. + +@item +Configuring @strong{MySQL} for debugging causes a safe memory allocator to be +included that can find some errors. It also provides a lot of output about +what is happening. + +@item +Have you applied the latest patches for your operating system? + +@item +Use the @code{--skip-locking} option to @code{mysqld}. On some systems, the +@code{lockd} lock manager does not work properly; the @code{--skip-locking} +option tells @code{mysqld} not to use external locking. (This means that you +cannot run 2 @code{mysqld} servers on the same data and that you must be +careful if you use @code{myisamchk}, but it may be instructive to try the +option as a test.) + +@item +Have you tried @code{mysqladmin -u root processlist} when @code{mysqld} +appears to be running but not responding? Sometimes @code{mysqld} is not +comatose even though you might think so. The problem may be that all +connections are in use, or there may be some internal lock problem. +@code{mysqladmin processlist} will usually be able to make a connection even +in these cases, and can provide useful information about the current number +of connections and their status. + +@item +Run the command @code{mysqladmin -i 5 status} or @code{mysqladmin -i 5 +-r status} or in a separate window to produce statistics while you run +your other queries. + +@item +Try the following: +@enumerate +@item +Start @code{mysqld} from @code{gdb} (or in another debugger). +@xref{Using gdb on mysqld}. + +@item +Run your test scripts. + +@item +Print the backtrace and the local variables at the 3 lowest levels. In gdb you +can do this with the following commands when @code{mysqld} has crashed inside +gdb: + +@example +backtrace +info local +up +info local +up +info local +@end example + +With gdb you can also examine which threads exist with @code{info +threads} and switch to a specific thread with @code{thread #}, where +@code{#} is the thread id. +@end enumerate + +@item +Try to simulate your application with a Perl script to force +@strong{MySQL} to crash or misbehave. + +@item +Send a normal bug report. @xref{Bug reports}. Be even more detailed +than usual. Because @strong{MySQL} works for many people, it may be that the +crash results from something that exists only on your computer (for example, +an error that is related to your particular system libraries). +@item +If you have a problem with tables with dynamic-length rows and you are +not using @code{BLOB/TEXT} columns (but only @code{VARCHAR} columns), you +can try to change all @code{VARCHAR} to @code{CHAR} with @code{ALTER +TABLE}. This will force @strong{MySQL} to use fixed-size rows. +Fixed-size rows take a little extra space, but are much more tolerant to +corruption! + +The current dynamic row code has been in use at @strong{MySQL AB} for at +least 3 years without any problems, but by nature dynamic-length rows are +more prone to errors, so it may be a good idea to try the above to see if +it helps! @end itemize -@code{mysqladmin processlist} only shows the connection, @code{INSERT DELAYED}, -and replication threads. -@cindex mysqltest, MySQL Test Suite -@cindex testing mysqld, mysqltest -@node MySQL test suite, , MySQL threads, MySQL internals -@section MySQL Test Suite +@node Link errors, Common errors, Crashing, Problems +@appendixsec Problems When Linking with the MySQL Client Library -Until recently, our main full-coverage test suite was based on proprietary -customer data and for that reason has not been publicly available. The only -publicly available part of our testing process consisted of the @code{crash-me} -test, a Perl DBI/DBD benchmark found in the @code{sql-bench} directory, and -miscellaneous tests located in @code{tests} directory. The lack of a -standardized publicly available test suite has made it difficult for our users, -as well developers, to do regression tests on the @strong{MySQL} code. To -address this problem, we have created a new test system that is included in -the source and binary distributions starting in Version 3.23.29. +@cindex linking, errors +@cindex errors, linking +@cindex problems, linking -The current set of test cases doesn't test everything in @strong{MySQL}, but it -should catch most obvious bugs in the SQL processing code, OS/library -issues, and is quite thorough in testing replication. Our eventual goal -is to have the tests cover 100% of the code. We welcome contributions -to our test suite. You may especially want to contribute tests that -examine the functionality critical to your system, as this will ensure -that all future @strong{MySQL} releases will work well with your -applications. +If you are linking your program and you get errors for unreferenced +symbols that start with @code{mysql_}, like the following: + +@example +/tmp/ccFKsdPa.o: In function `main': +/tmp/ccFKsdPa.o(.text+0xb): undefined reference to `mysql_init' +/tmp/ccFKsdPa.o(.text+0x31): undefined reference to `mysql_real_connect' +/tmp/ccFKsdPa.o(.text+0x57): undefined reference to `mysql_real_connect' +/tmp/ccFKsdPa.o(.text+0x69): undefined reference to `mysql_error' +/tmp/ccFKsdPa.o(.text+0x9a): undefined reference to `mysql_close' +@end example + +you should be able to solve this by adding @code{-Lpath-to-the-mysql-library +-lmysqlclient} @strong{LAST} on your link line. + +If you get @code{undefined reference} errors for the @code{uncompress} +or @code{compress} function, add @code{-lz} @strong{LAST} on your link +line and try again! + +If you get @code{undefined reference} errors for functions that should +exist on your system, like @code{connect}, check the man page for the +function in question, for which libraries you should add to the link +line! + +If you get @code{undefined reference} errors for functions that don't +exist on your system, like the following: + +@example +mf_format.o(.text+0x201): undefined reference to `__lxstat' +@end example + +it usually means that your library is compiled on a system that is not +100 % compatible with yours. In this case you should download the +latest @strong{MySQL} source distribution and compile this yourself. +@xref{Installing source}. + +If you are trying to run a program and you then get errors for +unreferenced symbols that start with @code{mysql_} or that the +@code{mysqlclient} library can't be found, this means that your system +can't find the share @code{libmysqlclient.so} library. + +The fix for this is to tell your system to search after shared +libraries where the library is located by one of the following methods: + +@itemize @bullet +@item +Add the path to the directory where you have @code{libmysqlclient.so} the +@code{LD_LIBRARY_PATH} environment variable. +@item +Add the path to the directory where you have @code{libmysqlclient.so} the +@code{LD_LIBRARY} environment variable. +@item +Copy @code{libmysqlclient.so} to some place that is searched by your system, +like @file{/lib}, and update the shared library information by executing +@code{ldconfig}. +@end itemize + +Another way to solve this problem is to link your program statically, with +@code{-static}, or by removing the dynamic @strong{MySQL} libraries +before linking your code. In the second case you should be +sure that no other programs are using the dynamic libraries! + + +@node Common errors, Full disk, Link errors, Problems +@appendixsec Some Common Errors When Using MySQL + +@cindex errors, list of @menu -* running mysqltest:: -* extending mysqltest:: -* Reporting mysqltest bugs:: +* Error Access denied:: @code{Access denied} Error +* Gone away:: @code{MySQL server has gone away} error +* Can not connect to server:: @code{Can't connect to [local] MySQL server} error +* Blocked host:: @code{Host '...' is blocked} error +* Too many connections:: @code{Too many connections} error +* Non-transactional tables:: @code{Some non-transactional changed tables couldn't be rolled back} Error +* Out of memory:: @code{Out of memory} error +* Packet too large:: @code{Packet too large} error +* Communication errors:: Communication errors / Aborted connection +* Full table:: @code{The table is full} error +* Cannot create:: @code{Can't create/write to file} Error +* Commands out of sync:: @code{Commands out of sync} error in client +* Ignoring user:: @code{Ignoring user} error +* Cannot find table:: @code{Table 'xxx' doesn't exist} error +* Cannot initialize character set:: @end menu -@node running mysqltest, extending mysqltest, MySQL test suite, MySQL test suite -@subsection Running the MySQL Test Suite -The test system consist of a test language interpreter -(@code{mysqltest}), a shell script to run all -tests(@code{mysql-test-run}), the actual test cases written in a special -test language, and their expected results. To run the test suite on -your system after a build, type @code{make test} or -@code{mysql-test/mysql-test-run} from the source root. If you have -installed a binary distribution, @code{cd} to the install root -(eg. @code{/usr/local/mysql}), and do @code{scripts/mysql-test-run}. -All tests should succeed. If not, you should try to find out why and -report the problem if this is a bug in @strong{MySQL}. -@xref{Reporting mysqltest bugs}. +This section lists some errors that users frequently get. You will find +descriptions of the errors, and how to solve the problem here. -If you have a copy of @code{mysqld} running on the machine where you want to -run the test suite you do not have to stop it, as long as it is not using -ports @code{9306} and @code{9307}. If one of those ports is taken, you should -edit @code{mysql-test-run} and change the values of the master and/or slave -port to one that is available. -You can run one individual test case with -@code{mysql-test/mysql-test-run test_name}. +@node Error Access denied, Gone away, Common errors, Common errors +@appendixsubsec @code{Access denied} Error -If one test fails, you should test running @code{mysql-test-run} with -the @code{--force} option to check if any other tests fails. +@cindex errors, access denied +@cindex problems, access denied errors +@cindex access denied errors -@node extending mysqltest, Reporting mysqltest bugs, running mysqltest, MySQL test suite -@subsection Extending the MySQL Test Suite +@xref{Privileges}, and especially. @xref{Access denied}. -You can use the @code{mysqltest} language to write your own test cases. -Unfortunately, we have not yet written full documentation for it - we plan to -do this shortly. You can, however, look at our current test cases and use -them as an example. The following points should help you get started: -@itemize +@node Gone away, Can not connect to server, Error Access denied, Common errors +@appendixsubsec @code{MySQL server has gone away} Error + +This section also covers the related @code{Lost connection to server +during query} error. + +The most common reason for the @code{MySQL server has gone away} error +is that the server timed out and closed the connection. By default, the +server closes the connection after 8 hours if nothing has happened. You +can change the time limit by setting the @code{wait_timeout} variable when +you start @code{mysqld}. + +Another common reason to receive the @code{MySQL server has gone away} error +is because you have issued a ``close'' on your @strong{MySQL} connection +and then tried to run a query on the closed connection. + +You can check that the @strong{MySQL} hasn't died by executing +@code{mysqladmin version} and examining the uptime. + +If you have a script, you just have to issue the query again for the client +to do an automatic reconnection. + +You normally can get the following error codes in this case +(which one you get is OS-dependent): + +@multitable @columnfractions .3 .7 +@item @code{CR_SERVER_GONE_ERROR} @tab The client couldn't send a question to the +server. +@item @code{CR_SERVER_LOST} @tab The client didn't get an error when writing +to the server, but it didn't get a full answer (or any answer) to the question. +@end multitable + +You can also get these errors if you send a query to the server that is +incorrect or too large. If @code{mysqld} gets a packet that is too large +or out of order, it assumes that something has gone wrong with the client and +closes the connection. If you need big queries (for example, if you are +working with big @code{BLOB} columns), you can increase the query limit by +starting @code{mysqld} with the @code{-O max_allowed_packet=#} option +(default 1M). The extra memory is allocated on demand, so @code{mysqld} will +use more memory only when you issue a big query or when @code{mysqld} must +return a big result row! + + +@node Can not connect to server, Blocked host, Gone away, Common errors +@appendixsubsec @code{Can't connect to [local] MySQL server} error + +A @strong{MySQL} client on Unix can connect to the @code{mysqld} server in two +different ways: Unix sockets, which connect through a file in the file +system (default @file{/tmp/mysqld.sock}) or TCP/IP, which connects +through a port number. Unix sockets are faster than TCP/IP but can only +be used when connecting to a server on the same computer. Unix sockets +are used if you don't specify a hostname or if you specify the special +hostname @code{localhost}. + +On Windows you can connect only with TCP/IP if the @code{mysqld} server +is running on Win95/Win98. If it's running on NT, you can also connect +with named pipes. The name of the named pipe is @strong{MySQL}. If you +don't give a hostname when connecting to @code{mysqld}, a @strong{MySQL} client +will first try to connect to the named pipe, and if this doesn't work it +will connect to the TCP/IP port. You can force the use of named pipes +on Windows by using @code{.} as the hostname. + +The error (2002) @code{Can't connect to ...} normally means that there +isn't a @strong{MySQL} server running on the system or that you are +using a wrong socket file or TCP/IP port when trying to connect to the +@code{mysqld} server. + +Start by checking (using @code{ps} or the task manager on Windows) that +there is a process running named @code{mysqld} on your server! If there +isn't any @code{mysqld} process, you should start one. @xref{Starting +server}. + +If a @code{mysqld} process is running, you can check the server by +trying these different connections (the port number and socket pathname +might be different in your setup, of course): + +@example +shell> mysqladmin version +shell> mysqladmin variables +shell> mysqladmin -h `hostname` version variables +shell> mysqladmin -h `hostname` --port=3306 version +shell> mysqladmin -h 'ip for your host' version +shell> mysqladmin --socket=/tmp/mysql.sock version +@end example + +Note the use of backquotes rather than forward quotes with the @code{hostname} +command; these cause the output of @code{hostname} (that is, the current +hostname) to be substituted into the @code{mysqladmin} command. + +Here are some reasons the @code{Can't connect to local MySQL server} +error might occur: + +@itemize @bullet @item -The tests are located in @code{mysql-test/t/*.test} +@code{mysqld} is not running. +@item +You are running on a system that uses MIT-pthreads. +If you are running on a system that doesn't have native threads, +@code{mysqld} uses the MIT-pthreads package. @xref{Which OS}. However, +all MIT-pthreads versions doesn't support Unix sockets. On a system +without sockets support you must always specify the hostname explicitly +when connecting to the server. Try using this command to check the +connection to the server: +@example +shell> mysqladmin -h `hostname` version +@end example +@item +Someone has removed the Unix socket that @code{mysqld} uses (default +@file{/tmp/mysqld.sock}). You might have a @code{cron} job that removes +the @strong{MySQL} socket (for example, a job that removes old files +from the @file{/tmp} directory). You can always run @code{mysqladmin +version} and check that the socket @code{mysqladmin} is trying to use +really exists. The fix in this case is to change the @code{cron} job to +not remove @file{mysqld.sock} or to place the socket somewhere else. You +can specify a different socket location at @strong{MySQL} configuration +time with this command: +@example +shell> ./configure --with-unix-socket-path=/path/to/socket +@end example +You can also start @code{safe_mysqld} with the +@code{--socket=/path/to/socket} option and set the environment variable +@code{MYSQL_UNIX_PORT} to the socket pathname before starting your +@strong{MySQL} clients. +@item +You have started the @code{mysqld} server with +the @code{--socket=/path/to/socket} option. If you change the socket +pathname for the server, you must also notify the @strong{MySQL} clients +about the new path. You can do this by setting the environment variable +@code{MYSQL_UNIX_PORT} to the socket pathname or by providing the socket path +as an argument to the clients. You can test the socket with this command: +@example +shell> mysqladmin --socket=/path/to/socket version +@end example @item -A test case consists of @code{;} terminated statements and is similar to the -input of @code{mysql} command line client. A statement by default is a query -to be sent to @strong{MySQL} server, unless it is recognized as internal -command ( eg. @code{sleep} ). +You are using Linux and one thread has died (core dumped). In this case +you must kill the other @code{mysqld} threads (for example, with the +@code{mysql_zap} script before you can start a new @strong{MySQL} +server. @xref{Crashing}. +@item +You may not have read and write privilege to either the directory that holds +the socket file or privilege to the socket file itself. In this case you +have to either change the privilege for the directory / file or restart +@code{mysqld} so that it uses a directory that you can access. +@end itemize + +If you get the error message @code{Can't connect to MySQL server on +some_hostname}, you can try the following things to find out what the +problem is : +@itemize @bullet @item -All queries that produce results, e.g. @code{SELECT}, @code{SHOW}, -@code{EXPLAIN}, etc., must be preceded with @code{@@/path/to/result/file}. The -file must contain the expected results. An easy way to generate the result -file is to run @code{mysqltest -r < t/test-case-name.test} from -@code{mysql-test} directory, and then edit the generated result files, if -needed, to adjust them to the expected output. In that case, be very careful -about not adding or deleting any invisible characters - make sure to only -change the text and/or delete lines. If you have to insert a line, make sure -the fields are separated with a hard tab, and there is a hard tab at the end. -You may want to use @code{od -c} to make sure your text editor has not messed -anything up during edit. We, of course, hope that you will never have to edit -the output of @code{mysqltest -r} as you only have to do it when you find a -bug. +Check if the server is up by doing @code{telnet your-host-name +tcp-ip-port-number} and press @code{RETURN} a couple of times. If there +is a @strong{MySQL} server running on this port you should get a +responses that includes the version number of the running @strong{MySQL} +server. If you get an error like @code{telnet: Unable to connect to +remote host: Connection refused}, then there is no server running on the +given port. +@item +Try connecting to the @code{mysqld} daemon on the local machine and check +the TCP/IP port that @code{mysqld} it's configured to use (variable @code{port}) with +@code{mysqladmin variables}. +@item +Check that your @code{mysqld} server is not started with the +@code{--skip-networking} option. +@end itemize + + +@node Blocked host, Too many connections, Can not connect to server, Common errors +@appendixsubsec @code{Host '...' is blocked} Error + +If you get an error like this: + +@example +Host 'hostname' is blocked because of many connection errors. +Unblock with 'mysqladmin flush-hosts' +@end example + +this means that @code{mysqld} has gotten a lot (@code{max_connect_errors}) +of connect requests from the host @code{'hostname'} that have been interrupted +in the middle. After @code{max_connect_errors} failed requests, @code{mysqld} +assumes that something is wrong (like an attack from a cracker), and +blocks the site from further connections until someone executes the command +@code{mysqladmin flush-hosts}. + +By default, @code{mysqld} blocks a host after 10 connection errors. +You can easily adjust this by starting the server like this: + +@example +shell> safe_mysqld -O max_connect_errors=10000 & +@end example + +Note that if you get this error message for a given host, you should first +check that there isn't anything wrong with TCP/IP connections from that +host. If your TCP/IP connections aren't working, it won't do you any good to +increase the value of the @code{max_connect_errors} variable! + + +@node Too many connections, Non-transactional tables, Blocked host, Common errors +@appendixsubsec @code{Too many connections} Error + +If you get the error @code{Too many connections} when you try to connect +to @strong{MySQL}, this means that there is already @code{max_connections} +clients connected to the @code{mysqld} server. + +If you need more connections than the default (100), then you should restart +@code{mysqld} with a bigger value for the @code{max_connections} variable. + +Note that @code{mysqld} actually allows (@code{max_connections}+1) +clients to connect. The last connection is reserved for a user with the +@strong{process} privilege. By not giving this privilege to normal +users (they shouldn't need this), an administrator with this privilege +can log in and use @code{SHOW PROCESSLIST} to find out what could be +wrong. @xref{SHOW}. + +The maximum number of connects @strong{MySQL} is depending on how good +the thread library is on a given platform. Linux or Solaris should be +able to support 500-1000 simultaneous connections, depending on how much +RAM you have and what your clients are doing. + +@node Non-transactional tables, Out of memory, Too many connections, Common errors +@appendixsubsec @code{Some non-transactional changed tables couldn't be rolled back} Error + +@cindex Non-transactional tables + +If you get the error/warning: @code{Warning: Some non-transactional +changed tables couldn't be rolled back} when trying to do a +@code{ROLLBACK}, this means that some of the tables you used in the +transaction didn't support transactions. These non-transactional tables +will not be affected by the @code{ROLLBACK} statement. + +The most typical case when this happens is when you have tried to create +a table of a type that is not supported by your @code{mysqld} binary. +If @code{mysqld} doesn't support a table type (or if the table type is +disabled by a startup option) , it will instead create the table type +with the table type that is most resembles to the one you requested, +probably @code{MyISAM}. + +You can check the table type for a table by doing: + +@code{SHOW TABLE STATUS LIKE 'table_name'}. @xref{SHOW TABLE STATUS}. + +You can check the extensions your @code{mysqld} binary supports by doing: + +@code{show variables like 'have_%'}. @xref{SHOW VARIABLES}. + + +@node Out of memory, Packet too large, Non-transactional tables, Common errors +@appendixsubsec @code{Out of memory} Error + +If you issue a query and get something like the following error: + +@example +mysql: Out of memory at line 42, 'malloc.c' +mysql: needed 8136 byte (8k), memory in use: 12481367 bytes (12189k) +ERROR 2008: MySQL client ran out of memory +@end example + +note that the error refers to the @strong{MySQL} client @code{mysql}. The +reason for this error is simply that the client does not have enough memory to +store the whole result. + +To remedy the problem, first check that your query is correct. Is it +reasonable that it should return so many rows? If so, +you can use @code{mysql --quick}, which uses @code{mysql_use_result()} +to retrieve the result set. This places less of a load on the client (but +more on the server). + + +@node Packet too large, Communication errors, Out of memory, Common errors +@appendixsubsec @code{Packet too large} Error + +When a @strong{MySQL} client or the @code{mysqld} server gets a packet bigger +than @code{max_allowed_packet} bytes, it issues a @code{Packet too large} +error and closes the connection. + +If you are using the @code{mysql} client, you may specify a bigger buffer by +starting the client with @code{mysql --set-variable=max_allowed_packet=8M}. + +If you are using other clients that do not allow you to specify the maximum +packet size (such as @code{DBI}), you need to set the packet size when you +start the server. You cau use a command-line option to @code{mysqld} to set +@code{max_allowed_packet} to a larger size. For example, if you are +expecting to store the full length of a @code{BLOB} into a table, you'll need +to start the server with the @code{--set-variable=max_allowed_packet=16M} +option. + + +@node Communication errors, Full table, Packet too large, Common errors +@appendixsubsec Communication Errors / Aborted Connection + +@cindex aborted clients +@cindex aborted connection +@cindex connection, aborted + +Starting with @code{MySQL 3.23.40} you only get the @code{Aborted +connection} error of you start @code{mysqld} with @code{--warnings}. + +If you find errors like the following in your error log. + +@example +010301 14:38:23 Aborted connection 854 to db: 'users' user: 'josh' +@end example + +@xref{Error log}. +This means that something of the following has happened: + +@itemize @bullet @item -To be consistent with our setup, you should put your result files in -@code{mysql-test/r} directory and name them @code{test_name.result}. If the -test produces more than one result, you should use @code{test_name.a.result}, -@code{test_name.b.result}, etc. +The client program did not call @code{mysql_close()} before exit. +@item +The client had been sleeping more than @code{wait_timeout} or +@code{interactive_timeout} without doing any requests. @xref{SHOW +VARIABLES}. +@item +The client program ended abruptly in the middle of the transfer. +@end itemize +When the above happens, the server variable @code{Aborted_clients} is +incremented. + +The server variable @code{Aborted_connects} is incremented when: + +@itemize @bullet @item -If a statement returns an error, you should on the line before the statement -specify with the @code{--error error-number}. The error number can be -a list of possible error numbers separated with @code{','}. +When a connection packet doesn't contain the right information. +@item +When the user didn't have privileges to connect to a database. +@item +When a user uses a wrong password. +@item +When it takes more than @code{connect_timeout} seconds to get +a connect package. +@end itemize +Note that the above could indicate that someone is trying to break into +your database! + +@xref{SHOW VARIABLES}. + +Other reasons for problems with Aborted clients / Aborted connections. +@itemize @bullet @item -If you are writing a replication test case, you should on the first line of -the test file, put @code{source include/master-slave.inc;}. To switch between -master and slave, use @code{connection master;} and @code{connection slave;}. -If you need to do something on an alternate connection, you can do -@code{connection master1;} for the master, and @code{connection slave1;} for -the slave. +Usage of duplex Ethernet protocol, both half and full with +Linux. Many Linux Ethernet drivers have this bug. You should test +for this bug by transferring a huge file via ftp between these two +machines. If a transfer goes in burst-pause-burst-pause ... mode then +you are experiencing a Linux duplex syndrome. The only solution to +this problem is switching of both half and full duplexing on hubs +and switches. +@item +Some problem with the thread library that causes interrupts on reads. +@item +Badly configured TCP/IP. +@item +Faulty Ethernets or hubs or switches, cables ... This can be diagnosed +properly only by replacing hardware. +@end itemize + + +@node Full table, Cannot create, Communication errors, Common errors +@appendixsubsec @code{The table is full} Error + +@cindex table is full + +This error occurs in older @strong{MySQL} versions when an in-memory temporary +table becomes larger than @code{tmp_table_size} bytes. To avoid this +problem, you can use the @code{-O tmp_table_size=#} option to +@code{mysqld} to increase the temporary table size or use the SQL +option @code{SQL_BIG_TABLES} before you issue the problematic +query. @xref{SET OPTION, , @code{SET OPTION}}. +You can also start @code{mysqld} with the @code{--big-tables} option. +This is exactly the same as using @code{SQL_BIG_TABLES} for all queries. + +In @strong{MySQL} Version 3.23, in-memory temporary tables will automatically be +converted to a disk-based @code{MyISAM} table after the table size gets +bigger than @code{tmp_table_size}. + + +@node Cannot create, Commands out of sync, Full table, Common errors +@appendixsubsec @code{Can't create/write to file} Error + +@cindex can't create/write to file + +If you get an error for some queries of type: + +@example +Can't create/write to file '\\sqla3fe_0.ism'. +@end example + +this means that @strong{MySQL} can't create a temporary file for the +result set in the given temporary directory. (The above error is a +typical error message on Windows, and the Unix error message is similar.) +The fix is to start @code{mysqld} with @code{--tmpdir=path} or to add to your option +file: + +@example +[mysqld] +tmpdir=C:/temp +@end example + +assuming that the @file{c:\\temp} directory exists. @xref{Option files}. + +Check also the error code that you get with @code{perror}. One reason +may also be a disk full error; + +@example +shell> perror 28 +Error code 28: No space left on device +@end example + + +@node Commands out of sync, Ignoring user, Cannot create, Common errors +@appendixsubsec @code{Commands out of sync} Error in Client + +@cindex commands out of sync + +If you get @code{Commands out of sync; You can't run this command now} +in your client code, you are calling client functions in the wrong order! + +This can happen, for example, if you are using @code{mysql_use_result()} and +try to execute a new query before you have called @code{mysql_free_result()}. +It can also happen if you try to execute two queries that return data without +a @code{mysql_use_result()} or @code{mysql_store_result()} in between. + + +@node Ignoring user, Cannot find table, Commands out of sync, Common errors +@appendixsubsec @code{Ignoring user} Error + +If you get the following error: + +@code{Found wrong password for user: 'some_user@@some_host'; Ignoring user} + +this means that when @code{mysqld} was started or when it reloaded the +permissions tables, it found an entry in the @code{user} table with +an invalid password. As a result, the entry is simply ignored by the +permission system. + +Possible causes of and fixes for this problem: + +@itemize @bullet @item -If you need to do something in a loop, you can use something like this: +You may be running a new version of @code{mysqld} with an old +@code{user} table. +You can check this by executing @code{mysqlshow mysql user} to see if +the password field is shorter than 16 characters. If so, you can correct this +condition by running the @code{scripts/add_long_password} script. + +@item +The user has an old password (8 characters long) and you didn't start +@code{mysqld} with the @code{--old-protocol} option. +Update the user in the @code{user} table with a new password or +restart @code{mysqld} with @code{--old-protocol}. + +@item +@findex PASSWORD() +You have specified a password in the @code{user} table without using the +@code{PASSWORD()} function. Use @code{mysql} to update the user in the +@code{user} table with a new password. Make sure to use the @code{PASSWORD()} +function: + @example -let $1=1000; -while ($1) -@{ - # do your queries here - dec $1; -@} +mysql> update user set password=PASSWORD('your password') + where user='XXX'; +@end example +@end itemize + + +@node Cannot find table, Cannot initialize character set, Ignoring user, Common errors +@appendixsubsec @code{Table 'xxx' doesn't exist} Error + +If you get the error @code{Table 'xxx' doesn't exist} or @code{Can't +find file: 'xxx' (errno: 2)}, this means that no table exists +in the current database with the name @code{xxx}. + +Note that as @strong{MySQL} uses directories and files to store databases and +tables, the database and table names are @strong{case sensitive}! +(On Windows the databases and tables names are not case sensitive, but all +references to a given table within a query must use the same case!) + +You can check which tables you have in the current database with +@code{SHOW TABLES}. @xref{SHOW, , @code{SHOW}}. + + +@node Cannot initialize character set, , Cannot find table, Common errors +@appendixsubsec @code{Can@'t initialize character set xxx} error. + +@cindex multibyte character sets + +If you get an error like: + +@example +MySQL Connection Failed: Can't initialize character set xxx @end example +This means one of the following things: + +@itemize @bullet @item -To sleep between queries, use the @code{sleep} command. It supports fractions -of a second, so you can do @code{sleep 1.3;}, for example, to sleep 1.3 -seconds. +The character set is a multi-byte character set and you have not support +for the character set in the client. + +In this case you need to recompile the client with +@code{--with-charset=xxx} or with @code{--with-extra-charsets=xxx}. +@xref{configure options}. + +All standard @strong{MySQL} binaries are compiled with +@code{--with-extra-character-sets=complex} which will enable support for +all multi-byte character sets. @xref{Character sets}. @item -To run the slave with additional options for your test case, put them -in the command-line format in @code{mysql-test/t/test_name-slave.opt}. For -the master, put them in @code{mysql-test/t/test_name-master.opt}. +The character set is a simple character set which is not compiled into +@code{mysqld} and the character set definition files is not in the place +where the client expect to find them. + +In this case you need to: +@itemize @bullet @item -If you have a question about the test suite, or have a test case to contribute, -e-mail to @email{internals@@lists.mysql.com}. As the list does not accept -attachments, you should ftp all the relevant files to: -@url{ftp://support.mysql.com/pub/mysql/Incoming} +Recompile the client with support for the character set. +@xref{configure options}. +@item +Specify to the client where the character set definition files are. For many +client you can do this with the +@code{--character-sets-dir=path-to-charset-dir} option. +@item +Copy the character definition files to the path where the client expect them +to be. +@end itemize +@end itemize + +@node Full disk, Temporary files, Common errors, Problems +@appendixsec How MySQL Handles a Full Disk + +@cindex full disk +@cindex disk full + +@noindent +When a disk-full condition occurs, @strong{MySQL} does the following: + +@itemize @bullet +@item +It checks once every minute to see whether or not there is enough space to +write the current row. If there is enough space, it continues as if nothing had +happened. +@item +Every 6 minutes it writes an entry to the log file warning about the disk +full condition. @end itemize -@node Reporting mysqltest bugs, , extending mysqltest, MySQL test suite -@subsection Reporting bugs in the MySQL Test Suite +@noindent +To alleviate the problem, you can take the following actions: -If your @strong{MySQL} version doesn't pass the test suite you should +@itemize @bullet +@item +To continue, you only have to free enough disk space to insert all records. +@item +To abort the thread, you must send a @code{mysqladmin kill} to the thread. +The thread will be aborted the next time it checks the disk (in 1 minute). +@item +Note that other threads may be waiting for the table that caused the disk +full condition. If you have several ``locked'' threads, killing the one +thread that is waiting on the disk-full condition will allow the other +threads to continue. +@end itemize + +Exceptions to the above behaveour is when you use @code{REPAIR} or +@code{OPTIMIZE} or when the indexes are created in a batch after an +@code{LOAD DATA INFILE} or after an @code{ALTER TABLE} statement. + +All of the above commands may use big temporary files that left to +themself would cause big problems for the rest of the system. If +@strong{MySQL} gets disk full while doing any of the above operations, +it will remove the big temporary files and mark the table as crashed +(except for @code{ALTER TABLE}, in which the old table will be left +unchanged). + + +@node Temporary files, Problems with mysql.sock, Full disk, Problems +@appendixsec Where MySQL Stores Temporary Files + +@strong{MySQL} uses the value of the @code{TMPDIR} environment variable as +the pathname of the directory in which to store temporary files. If you don't +have @code{TMPDIR} set, @strong{MySQL} uses the system default, which is +normally @file{/tmp} or @file{/usr/tmp}. If the file system containing your +temporary file directory is too small, you should edit @code{safe_mysqld} to +set @code{TMPDIR} to point to a directory in a file system where you have +enough space! You can also set the temporary directory using the +@code{--tmpdir} option to @code{mysqld}. + +@strong{MySQL} creates all temporary files as hidden files. This ensures +that the temporary files will be removed if @code{mysqld} is terminated. The +disadvantage of using hidden files is that you will not see a big temporary +file that fills up the file system in which the temporary file directory is +located. + +When sorting (@code{ORDER BY} or @code{GROUP BY}), @strong{MySQL} normally +uses one or two temporary files. The maximum disk-space needed is: + +@example +(length of what is sorted + sizeof(database pointer)) +* number of matched rows +* 2 +@end example + +@code{sizeof(database pointer)} is usually 4, but may grow in the future for +really big tables. + +For some @code{SELECT} queries, @strong{MySQL} also creates temporary SQL +tables. These are not hidden and have names of the form @file{SQL_*}. + +@code{ALTER TABLE} creates a temporary table in the same directory as +the original table. + + +@node Problems with mysql.sock, Changing MySQL user, Temporary files, Problems +@appendixsec How to Protect @file{/tmp/mysql.sock} from Being Deleted + +@cindex @code{mysql.sock}, protection +@cindex deletion, @code{mysql.sock} + +If you have problems with the fact that anyone can delete the +@strong{MySQL} communication socket @file{/tmp/mysql.sock}, you can, +on most versions of Unix, protect your @file{/tmp} file system by setting +the @code{sticky} bit on it. Log in as @code{root} and do the following: + +@example +shell> chmod +t /tmp +@end example + +This will protect your @file{/tmp} file system so that files can be deleted +only by their owners or the superuser (@code{root}). + +You can check if the @code{sticky} bit is set by executing @code{ls -ld /tmp}. +If the last permission bit is @code{t}, the bit is set. + + +@node Changing MySQL user, Resetting permissions, Problems with mysql.sock, Problems +@appendixsec How to Run MySQL As a Normal User + +@cindex starting, @code{mysqld} +@cindex @code{mysqld}, starting + +The @strong{MySQL} server @code{mysqld} can be started and run by any user. +In order to change @code{mysqld} to run as a Unix user @code{user_name}, you must do the following: -@itemize @bullet +@enumerate @item -Don't send a bug report before you have found out as much as possible of -what when wrong! When you do it, please use the @code{mysqlbug} script -so that we can get information about your system and @code{MySQL} -version. @xref{Bug reports}. +Stop the server if it's running (use @code{mysqladmin shutdown}). + @item -Make sure to include the output of @code{mysql-test-run}, as well as -contents of all @code{.reject} files in @code{mysql-test/r} directory. +Change the database directories and files so that @code{user_name} has +privileges to read and write files in them (you may need to do this as +the Unix @code{root} user): + +@example +shell> chown -R user_name /path/to/mysql/datadir +@end example + +If directories or files within the @strong{MySQL} data directory are +symlinks, you'll also need to follow those links and change the directories +and files they point to. @code{chown -R} may not follow symlinks for +you. + @item -If a test in the test suite fails, check if the test fails also when run -by its own: +Start the server as user @code{user_name}, or, if you are using +@strong{MySQL} Version 3.22 or later, start @code{mysqld} as the Unix @code{root} +user and use the @code{--user=user_name} option. @code{mysqld} will switch +to run as the Unix user @code{user_name} before accepting any connections. + +@item +To start the server as the given user name automatically at system +startup time, add a @code{user} line that specifies the user name to +the @code{[mysqld]} group of the @file{/etc/my.cnf} option file or the +@file{my.cnf} option file in the server's data directory. For example: @example -cd mysql-test -mysql-test-run --local test-name +[mysqld] +user=user_name @end example +@end enumerate -If this fails, then you should configure @strong{MySQL} with -@code{--with-debug} and run @code{mysql-test-run} with the -@code{--debug} option. If this also fails send the trace file -@file{var/tmp/master.trace} to ftp://support.mysql.com/pub/mysql/secret -so that we can examine it. Please remember to also include a full -description of your system, the version of the mysqld binary and how you -compiled it. +At this point, your @code{mysqld} process should be running fine and dandy as +the Unix user @code{user_name}. One thing hasn't changed, though: the +contents of the permissions tables. By default (right after running the +permissions table install script @code{mysql_install_db}), the @strong{MySQL} +user @code{root} is the only user with permission to access the @code{mysql} +database or to create or drop databases. Unless you have changed those +permissions, they still hold. This shouldn't stop you from accessing +@strong{MySQL} as the @strong{MySQL} @code{root} user when you're logged in +as a Unix user other than @code{root}; just specify the @code{-u root} option +to the client program. +Note that accessing @strong{MySQL} as @code{root}, by supplying @code{-u +root} on the command line, has @emph{nothing} to do with @strong{MySQL} running +as the Unix @code{root} user, or, indeed, as another Unix user. The access +permissions and user names of @strong{MySQL} are completely separate from +Unix user names. The only connection with Unix user names is that if you +don't provide a @code{-u} option when you invoke a client program, the client +will try to connect using your Unix login name as your @strong{MySQL} user +name. + +If your Unix box itself isn't secured, you should probably at least put a +password on the @strong{MySQL} @code{root} users in the access tables. +Otherwise, any user with an account on that machine can run @code{mysql -u +root db_name} and do whatever he likes. + + +@node Resetting permissions, File permissions , Changing MySQL user, Problems +@appendixsec How to Reset a Forgotten Password + +@cindex passwords, forgotten +@cindex passwords, resetting +@cindex root user, password resetting + +If you have forgotten the @code{root} user password for @strong{MySQL}, you +can restore it with the following procedure: + +@enumerate @item -Try also to run @code{mysql-test-run} with the @code{--force} option to -see if there is any other test that fails. +Take down the @code{mysqld} server by sending a @code{kill} (not @code{kill +-9}) to the @code{mysqld} server. The pid is stored in a @code{.pid} +file, which is normally in the @strong{MySQL} database directory: + +@example +kill `cat /mysql-data-directory/hostname.pid` +@end example + +You must be either the Unix @code{root} user or the same user the server +runs as to do this. @item -If you have compiled @strong{MySQL} yourself, check our manual for how -to compile @strong{MySQL} on your platform or, preferable, use one of -the binaries we have compiled for you at -@uref{http://www.mysql.com/downloads/}. All our standard binaries should -pass the test suite ! +Restart @code{mysqld} with the @code{--skip-grant-tables} option. +@item +Connect to the @code{mysqld} server with @code{mysql -h hostname mysql} and change +the password with a @code{GRANT} command. @xref{GRANT,,@code{GRANT}}. +You can also do this with +@code{mysqladmin -h hostname -u user password 'new password'} +@item +Load the privilege tables with: @code{mysqladmin -h hostname +flush-privileges} or with the SQL command @code{FLUSH PRIVILEGES}. +@end enumerate + +Note that after you started @code{mysqld} with @code{--skip-grant-tables}, +any usage of @code{GRANT} commands will give you an @code{Unknown command} +error until you have executed @code{FLUSH PRIVILEGES}. + + +@node File permissions , Not enough file handles, Resetting permissions, Problems +@appendixsec Problems with File Permissions + +@cindex files, permissions +@cindex error mesaages, can't find file +@cindex files, not found message + +If you have problems with file permissions, for example, if @code{mysql} +issues the following error message when you create a table: + +@example +ERROR: Can't find file: 'path/with/filename.frm' (Errcode: 13) +@end example + +@tindex UMASK environment variable +@tindex Environment variable, UMASK +then the environment variable @code{UMASK} might be set incorrectly when +@code{mysqld} starts up. The default umask value is @code{0660}. You can +change this behavior by starting @code{safe_mysqld} as follows: + +@example +shell> UMASK=384 # = 600 in octal +shell> export UMASK +shell> /path/to/safe_mysqld & +@end example + +@tindex UMASK_DIR environment variable +@tindex Environment variable, UMASK_DIR +By default @strong{MySQL} will create database and @code{RAID} +directories with permission type 0700. You can modify this behavior by +setting the @code{UMASK_DIR} variable. If you set this, new +directories are created with the combined @code{UMASK} and +@code{UMASK_DIR}. For example, if you want to give group access to +all new directories, you can do: + +@example +shell> UMASK_DIR=504 # = 770 in octal +shell> export UMASK_DIR +shell> /path/to/safe_mysqld & +@end example + +In @strong{MySQL} Version 3.23.25 and above, @strong{MySQL} assumes that the +value for @code{UMASK} and @code{UMASK_DIR} is in octal if it starts +with a zero. + +@xref{Environment variables}. + + +@node Not enough file handles, Using DATE, File permissions , Problems +@appendixsec File Not Found + +If you get @code{ERROR '...' not found (errno: 23)}, @code{Can't open +file: ... (errno: 24)}, or any other error with @code{errno 23} or +@code{errno 24} from @strong{MySQL}, it means that you haven't allocated +enough file descriptors for @strong{MySQL}. You can use the +@code{perror} utility to get a description of what the error number +means: + +@example +shell> perror 23 +File table overflow +shell> perror 24 +Too many open files +shell> perror 11 +Resource temporarily unavailable +@end example + +The problem here is that @code{mysqld} is trying to keep open too many +files simultaneously. You can either tell @code{mysqld} not to open so +many files at once or increase the number of file descriptors +available to @code{mysqld}. + +To tell @code{mysqld} to keep open fewer files at a time, you can make +the table cache smaller by using the @code{-O table_cache=32} option to +@code{safe_mysqld} (the default value is 64). Reducing the value of +@code{max_connections} will also reduce the number of open files (the +default value is 90). + +@tindex ulimit +To change the number of file descriptors available to @code{mysqld}, you +can use the option @code{--open-files-limit=#} to @code{safe_mysqld} or +@code{-O open-files-limit=#} to @code{mysqld}. @xref{SHOW VARIABLES}. +The easiest way to do that is to add the option to your option file. +@xref{Option files}. If you have an old @code{mysqld} version that +doesn't support this, you can edit the @code{safe_mysqld} script. There +is a commented-out line @code{ulimit -n 256} in the script. You can +remove the @code{'#'} character to uncomment this line, and change the +number 256 to affect the number of file descriptors available to +@code{mysqld}. + +@code{ulimit} (and @code{open-files-limit}) can increase the number of +file descriptors, but only up to the limit imposed by the operating +system. There is also a 'hard' limit that can only be overrided if you +start @code{safe_mysqld} or @code{mysqld} as root (Just remember that +you need to also use the @code{--user=..} option in this case). If you +need to increase the OS limit on the number of file descriptors +available to each process, consult the documentation for your operating +system. + +Note that if you run the @code{tcsh} shell, @code{ulimit} will not work! +@code{tcsh} will also report incorrect values when you ask for the current +limits! In this case you should start @code{safe_mysqld} with @code{sh}! + + +@node Using DATE, Timezone problems, Not enough file handles, Problems +@appendixsec Problems Using @code{DATE} Columns + +@findex DATE + +@cindex DATE columns, problems +@cindex problems, @code{DATE} columns + +The format of a @code{DATE} value is @code{'YYYY-MM-DD'}. According to ANSI +SQL, no other format is allowed. You should use this format in @code{UPDATE} +expressions and in the WHERE clause of @code{SELECT} statements. For +example: + +@example +mysql> SELECT * FROM tbl_name WHERE date >= '1997-05-05'; +@end example + +As a convenience, @strong{MySQL} automatically converts a date to a number if +the date is used in a numeric context (and vice versa). It is also smart +enough to allow a ``relaxed'' string form when updating and in a @code{WHERE} +clause that compares a date to a @code{TIMESTAMP}, @code{DATE}, or a +@code{DATETIME} column. (Relaxed form means that any punctuation character +may be used as the separator between parts. For example, @code{'1998-08-15'} +and @code{'1998#08#15'} are equivalent.) @strong{MySQL} can also convert a +string containing no separators (such as @code{'19980815'}), provided it +makes sense as a date. + +The special date @code{'0000-00-00'} can be stored and retrieved as +@code{'0000-00-00'.} When using a @code{'0000-00-00'} date through +@strong{MyODBC}, it will automatically be converted to @code{NULL} in +@strong{MyODBC} Version 2.50.12 and above, because ODBC can't handle this kind of +date. + +Because @strong{MySQL} performs the conversions described above, the following +statements work: + +@example +mysql> INSERT INTO tbl_name (idate) VALUES (19970505); +mysql> INSERT INTO tbl_name (idate) VALUES ('19970505'); +mysql> INSERT INTO tbl_name (idate) VALUES ('97-05-05'); +mysql> INSERT INTO tbl_name (idate) VALUES ('1997.05.05'); +mysql> INSERT INTO tbl_name (idate) VALUES ('1997 05 05'); +mysql> INSERT INTO tbl_name (idate) VALUES ('0000-00-00'); + +mysql> SELECT idate FROM tbl_name WHERE idate >= '1997-05-05'; +mysql> SELECT idate FROM tbl_name WHERE idate >= 19970505; +mysql> SELECT mod(idate,100) FROM tbl_name WHERE idate >= 19970505; +mysql> SELECT idate FROM tbl_name WHERE idate >= '19970505'; +@end example + +@noindent +However, the following will not work: + +@example +mysql> SELECT idate FROM tbl_name WHERE STRCMP(idate,'19970505')=0; +@end example + +@code{STRCMP()} is a string function, so it converts @code{idate} to +a string and performs a string comparison. It does not convert +@code{'19970505'} to a date and perform a date comparison. + +Note that @strong{MySQL} does no checking whether or not the date is +correct. If you store an incorrect date, such as @code{'1998-2-31'}, the +wrong date will be stored. If the date cannot be converted to any reasonable +value, a @code{0} is stored in the @code{DATE} field. This is mainly a speed +issue and we think it is up to the application to check the dates, and not +the server. + + +@node Timezone problems, Case sensitivity, Using DATE, Problems +@appendixsec Time Zone Problems + +@cindex timezone problems +@cindex problems, timezone + +@tindex TZ environment variable +@tindex Environment variable, TZ + +If you have a problem with @code{SELECT NOW()} returning values in GMT and +not your local time, you have to set the @code{TZ} environment variable to +your current time zone. This should be done for the environment in which +the server runs, for example, in @code{safe_mysqld} or @code{mysql.server}. +@xref{Environment variables}. + + +@node Case sensitivity, Problems with NULL, Timezone problems, Problems +@appendixsec Case Sensitivity in Searches + +@cindex case sensitivity, in searches +@cindex searching, and case-sensitivity +@cindex Chinese +@cindex Big5 Chinese character encoding + +By default, @strong{MySQL} searches are case-insensitive (although there are +some character sets that are never case insensitive, such as @code{czech}). +That means that if you search with @code{col_name LIKE 'a%'}, you will get all +column values that start with @code{A} or @code{a}. If you want to make this +search case-sensitive, use something like @code{INDEX(col_name, "A")=0} to +check a prefix. Or use @code{STRCMP(col_name, "A") = 0} if the column value +must be exactly @code{"A"}. + +Simple comparison operations (@code{>=, >, = , < , <=}, sorting and +grouping) are based on each character's ``sort value''. Characters with +the same sort value (like E, e and é) are treated as the same character! + +In older @strong{MySQL} versions @code{LIKE} comparisons where done on +the uppercase value of each character (E == e but E <> é). In newer +@strong{MySQL} versions @code{LIKE} works just like the other comparison +operators. + +If you want a column always to be treated in case-sensitive fashion, +declare it as @code{BINARY}. @xref{CREATE TABLE, , @code{CREATE TABLE}}. + +If you are using Chinese data in the so-called big5 encoding, you want to +make all character columns @code{BINARY}. This works because the sorting +order of big5 encoding characters is based on the order of ASCII codes. + + +@node Problems with NULL, Problems with alias, Case sensitivity, Problems +@appendixsec Problems with @code{NULL} Values + +@cindex @code{NULL} values, vs. empty values + +@tindex NULL + +The concept of the @code{NULL} value is a common source of confusion for +newcomers to SQL, who often think that @code{NULL} is the same thing as an +empty string @code{''}. This is not the case! For example, the following +statements are completely different: + +@example +mysql> INSERT INTO my_table (phone) VALUES (NULL); +mysql> INSERT INTO my_table (phone) VALUES (""); +@end example + +Both statements insert a value into the @code{phone} column, but the first +inserts a @code{NULL} value and the second inserts an empty string. The +meaning of the first can be regarded as ``phone number is not known'' and the +meaning of the second can be regarded as ``she has no phone''. + +In SQL, the @code{NULL} value is always false in comparison to any +other value, even @code{NULL}. An expression that contains @code{NULL} +always produces a @code{NULL} value unless otherwise indicated in +the documentation for the operators and functions involved in the +expression. All columns in the following example return @code{NULL}: + +@example +mysql> SELECT NULL,1+NULL,CONCAT('Invisible',NULL); +@end example + +If you want to search for column values that are @code{NULL}, you +cannot use the @code{=NULL} test. The following statement returns no +rows, because @code{expr = NULL} is FALSE, for any expression: + +@example +mysql> SELECT * FROM my_table WHERE phone = NULL; +@end example + +To look for @code{NULL} values, you must use the @code{IS NULL} test. +The following shows how to find the @code{NULL} phone number and the +empty phone number: + +@example +mysql> SELECT * FROM my_table WHERE phone IS NULL; +mysql> SELECT * FROM my_table WHERE phone = ""; +@end example + +In @strong{MySQL}, as in many other SQL servers, you can't index +columns that can have @code{NULL} values. You must declare such columns +@code{NOT NULL}. Conversely, you cannot insert @code{NULL} into an indexed +column. + +@findex LOAD DATA INFILE +When reading data with @code{LOAD DATA INFILE}, empty columns are updated +with @code{''}. If you want a @code{NULL} value in a column, you should use +@code{\N} in the text file. The literal word @code{'NULL'} may also be used +under some circumstances. +@xref{LOAD DATA, , @code{LOAD DATA}}. + +When using @code{ORDER BY}, @code{NULL} values are presented first. If you +sort in descending order using @code{DESC}, @code{NULL} values are presented +last. When using @code{GROUP BY}, all @code{NULL} values are regarded as +equal. + +To help with @code{NULL} handling, you can use the @code{IS NULL} and +@code{IS NOT NULL} operators and the @code{IFNULL()} function. + +@cindex @code{TIMESTAMP}, and @code{NULL} values +@cindex @code{AUTO_INCREMENT}, and @code{NULL} values +@cindex @code{NULL} values, and @code{TIMESTAMP} columns +@cindex @code{NULL} values, and @code{AUTO_INCREMENT} columns +For some column types, @code{NULL} values are handled specially. If you +insert @code{NULL} into the first @code{TIMESTAMP} column of a table, the +current date and time is inserted. If you insert @code{NULL} into an +@code{AUTO_INCREMENT} column, the next number in the sequence is inserted. + + +@node Problems with alias, Deleting from related tables, Problems with NULL, Problems +@appendixsec Problems with @code{alias} + +@tindex alias + +You can use an alias to refer to a column in the @code{GROUP BY}, +@code{ORDER BY}, or in the @code{HAVING} part. Aliases can also be used +to give columns better names: + +@example +SELECT SQRT(a*b) as rt FROM table_name GROUP BY rt HAVING rt > 0; +SELECT id,COUNT(*) AS cnt FROM table_name GROUP BY id HAVING cnt > 0; +SELECT id AS "Customer identity" FROM table_name; +@end example + +Note that ANSI SQL doesn't allow you to refer to an alias in a +@code{WHERE} clause. This is because when the @code{WHERE} code is +executed the column value may not yet be determined. For example, the +following query is @strong{illegal}: + +@example +SELECT id,COUNT(*) AS cnt FROM table_name WHERE cnt > 0 GROUP BY id; +@end example + +The @code{WHERE} statement is executed to determine which rows should +be included in the @code{GROUP BY} part while @code{HAVING} is used to +decide which rows from the result set should be used. + + +@node Deleting from related tables, No matching rows, Problems with alias, Problems +@appendixsec Deleting Rows from Related Tables + +@cindex deleting, rows +@cindex rows, deleting +@cindex tables, deleting rows +As @strong{MySQL} doesn't support sub-selects or use of more than one table +in the @code{DELETE} statement, you should use the following approach to +delete rows from 2 related tables: + +@enumerate @item -If you get an error, like @code{Result length mismatch} or @code{Result -content mismatch} it means that the output of the test didn't match -exactly the expected output. This could be a bug in @strong{MySQL} or -that your mysqld version produces slight different results under some -circumstances. +@code{SELECT} the rows based on some @code{WHERE} condition in the main table. +@item +@code{DELETE} the rows in the main table based on the same condition. +@item +@code{DELETE FROM related_table WHERE related_column IN (selected_rows)}. +@end enumerate -Failed test results are put in a file with the same base name as the -result file with the @code{.reject} extension. If your test case is -failing, you should do a diff on the two files. If you cannot see how -they are different, examine both with @code{od -c} and also check their -lengths. +If the total number of characters in the query with +@code{related_column} is more than 1,048,576 (the default value of +@code{max_allowed_packet}, you should split it into smaller parts and +execute multiple @code{DELETE} statements. You will probably get the +fastest @code{DELETE} by only deleting 100-1000 @code{related_column} +id's per query if the @code{related_column} is an index. If the +@code{related_column} isn't an index, the speed is independent of the +number of arguments in the @code{IN} clause. + +@node No matching rows, ALTER TABLE problems, Deleting from related tables, Problems +@appendixsec Solving Problems with No Matching Rows +@cindex no matching rows +@cindex rows, matching problems + +If you have a complicated query that has many tables and that doesn't +return any rows, you should use the following procedure to find out what +is wrong with your query: + +@enumerate @item -If a test fails totally, you should check the logs file in the -@code{mysql-test/var/log} directory for hints of what went wrong. +Test the query with @code{EXPLAIN} and check if you can find something that is +obviously wrong. @xref{EXPLAIN, , @code{EXPLAIN}}. @item -If you have compiled @strong{MySQL} with debugging you can try to debug this -by running @code{mysql-test-run} with the @code{--gdb} and/or @code{--debug} -options. -@xref{Making trace files}. +Select only those fields that are used in the @code{WHERE} clause. -If you have not compiled @strong{MySQL} for debugging you should probably -do that. Just specify the @code{--with-debug} options to @code{configure}! -@xref{Installing source}. +@item +Remove one table at a time from the query until it returns some rows. +If the tables are big, it's a good idea to use @code{LIMIT 10} with the query. + +@item +Do a @code{SELECT} for the column that should have matched a row against +the table that was last removed from the query. + +@item +If you are comparing @code{FLOAT} or @code{DOUBLE} columns with numbers that +have decimals, you can't use @code{=}! This problem is common in most +computer languages because floating-point values are not exact values: + +@example +mysql> SELECT * FROM table_name WHERE float_column=3.5; + -> +mysql> SELECT * FROM table_name WHERE float_column between 3.45 and 3.55; +@end example + +In most cases, changing the @code{FLOAT} to a @code{DOUBLE} will fix this! + +@item +If you still can't figure out what's wrong, create a minimal test that can +be run with @code{mysql test < query.sql} that shows your problems. +You can create a test file with @code{mysqldump --quick database tables > query.sql}. Open the file in an editor, remove some insert lines (if there are +too many of these), and add your select statement at the end of the file. + +Test that you still have your problem by doing: + +@example +shell> mysqladmin create test2 +shell> mysql test2 < query.sql +@end example + +Post the test file using @code{mysqlbug} to @email{mysql@@lists.mysql.com}. +@end enumerate + + +@node ALTER TABLE problems, Change column order, No matching rows, Problems +@appendixsec Problems with @code{ALTER TABLE}. + +@tindex ALTER TABLE + +@code{ALTER TABLE} changes a table to the current character set. +If you during @code{ALTER TABLE} get a duplicate key error, then the cause +is either that the new character sets maps to keys to the same value +or that the table is corrupted, in which case you should run +@code{REPAIR TABLE} on the table. + +If @code{ALTER TABLE} dies with an error like this: + +@example +Error on rename of './database/name.frm' to './database/B-a.frm' (Errcode: 17) +@end example + +the problem may be that @strong{MySQL} has crashed in a previous @code{ALTER +TABLE} and there is an old table named @file{A-something} or +@file{B-something} lying around. In this case, go to the @strong{MySQL} data +directory and delete all files that have names starting with @code{A-} or +@code{B-}. (You may want to move them elsewhere instead of deleting them.) + +@code{ALTER TABLE} works the following way: + +@itemize @bullet +@item Create a new table named @file{A-xxx} with the requested changes. +@item All rows from the old table are copied to @file{A-xxx}. +@item The old table is renamed @file{B-xxx}. +@item @file{A-xxx} is renamed to your old table name. +@item @file{B-xxx} is deleted. +@end itemize + +If something goes wrong with the renaming operation, @strong{MySQL} tries to +undo the changes. If something goes seriously wrong (this shouldn't happen, +of course), @strong{MySQL} may leave the old table as @file{B-xxx}, but a +simple rename on the system level should get your data back. + + +@node Change column order, Temporary table problems, ALTER TABLE problems, Problems +@appendixsec How To Change the Order of Columns in a Table + +@cindex reordering, columns +@cindex columns, changing +@cindex changing, column order +@cindex tables, changing column order + +The whole point of SQL is to abstract the application from the data +storage format. You should always specify the order in which you wish to +retrieve your data. For example: + +@example +SELECT col_name1, col_name2, col_name3 FROM tbl_name; +@end example + +will return columns in the order @code{col_name1}, @code{col_name2}, @code{col_name3}, whereas: + +@example +SELECT col_name1, col_name3, col_name2 FROM tbl_name; +@end example + +will return columns in the order @code{col_name1}, @code{col_name3}, @code{col_name2}. + +You should @strong{NEVER}, in an application, use @code{SELECT *} and +retrieve the columns based on their position, because the order in which +columns are returned @strong{CANNOT} be guaranteed over time. A simple +change to your database may cause your application to fail rather +dramatically. + +If you want to change the order of columns anyway, you can do it as follows: + +@enumerate +@item +Create a new table with the columns in the right order. +@item +Execute +@code{INSERT INTO new_table SELECT fields-in-new_table-order FROM old_table}. +@item +Drop or rename @code{old_table}. +@item +@code{ALTER TABLE new_table RENAME old_table}. +@end enumerate + + +@node Temporary table problems, , Change column order, Problems +@appendixsec TEMPORARY TABLE problems + +@cindex temporary tables, problems + +The following are a list of the limitations with @code{TEMPORARY TABLES}. + +@itemize @bullet +@item +A temporary table can only be of type @code{HEAP}, @code{ISAM} or +@code{MyISAM}. +@item +You can't use temporary tables more than once in the same query. +For example, the following doesn't work. + +@example +select * from temporary_table, temporary_table as t2; +@end example + +We plan to fix the above in 4.0. +@item +You can't use @code{RENAME} on a @code{TEMPORARY} table. +Note that @code{ALTER TABLE org_name RENAME new_name} works! + +We plan to fix the above in 4.0. @end itemize + + + + @page @cindex environment variables, list of -@node Environment variables, Users, MySQL internals, Top +@node Environment variables, Users, Problems, Top @appendix Environment Variables Here is a list of all the environment variables that are used directly or @@ -51104,8 +51237,8 @@ information even if you haven't compiled @strong{MySQL} for debugging! If the problem is that some tables are getting slower and slower you should try to optimize the table with @code{OPTIMIZE TABLE} or -@code{myisamchk}. @xref{Maintenance}. You should also check the slow -queries with @code{EXPLAIN}. +@code{myisamchk}. @xref{MySQL Database Administration}. You should also +check the slow queries with @code{EXPLAIN}. You should also read the OS-specific section in this manual for problems that may be unique to your environment. @@ -51362,7 +51495,8 @@ repeat the problem! @xref{Bug reports}. @appendixsubsec Using log files to find cause of errors in mysqld Note that before starting @code{mysqld} with @code{--log} you should -check all your tables with @code{myisamchk}. @xref{Maintenance}. +check all your tables with @code{myisamchk}. +@xref{MySQL Database Administration}. If @code{mysqld} dies or hangs, you should start @code{mysqld} with @code{--log}. When @code{mysqld} dies again, you can examine the end of @@ -51385,11 +51519,11 @@ You can find the queries that take a long time to execute by starting If you find the text @code{mysqld restarted} in the error log file (normally named @file{hostname.err}) you have probably found a query that causes @code{mysqld} to fail. If this happens you should check all -your tables with @code{myisamchk} (@pxref{Maintenance}), and test the -queries in the @strong{MySQL} log files to see if one doesn't work. If -you find such a query, try first upgrading to the newest @strong{MySQL} -version. If this doesn't help and you can't find anything in the -@code{mysql} mail archive, you should report the bug to +your tables with @code{myisamchk} (@pxref{MySQL Database Administration}), +and test the queries in the @strong{MySQL} log files to see if one doesn't +work. If you find such a query, try first upgrading to the newest +@strong{MySQL} version. If this doesn't help and you can't find anything +in the @code{mysql} mail archive, you should report the bug to @email{mysql@@lists.mysql.com}. Links to mail archives are available online at the @uref{http://www.mysql.com/documentation/, @strong{MySQL} documentation page}. @@ -53044,7 +53178,6 @@ That's all there is to it! * Building clients:: * Perl support:: * Group by functions:: -* CREATE FUNCTION:: @end menu @node Installing binary, Building clients, Placeholder, Placeholder @@ -53577,7 +53710,7 @@ Finally, you should install this new Perl. Again, the output of @code{make perl} indicates the command to use. -@node Group by functions, CREATE FUNCTION, Perl support, Placeholder +@node Group by functions, , Perl support, Placeholder @appendixsec Functions for Use with @code{GROUP BY} Clauses @findex GROUP BY functions @@ -53734,45 +53867,6 @@ mysql> SELECT id,FLOOR(value/100) FROM tbl_name ORDER BY RAND(); @end example -@node CREATE FUNCTION, , Group by functions, Placeholder -@appendixsec @code{CREATE FUNCTION/DROP FUNCTION} Syntax - -@findex CREATE FUNCTION -@findex DROP FUNCTION -@findex UDF functions -@findex User-defined functions -@findex Functions, user-defined - -@example -CREATE [AGGREGATE] FUNCTION function_name RETURNS @{STRING|REAL|INTEGER@} - SONAME shared_library_name - -DROP FUNCTION function_name -@end example - -A user-definable function (UDF) is a way to extend @strong{MySQL} with a new -function that works like native (built in) @strong{MySQL} functions such as -@code{ABS()} and @code{CONCAT()}. - -@code{AGGREGATE} is a new option for @strong{MySQL} Version 3.23. An -@code{AGGREGATE} function works exactly like a native @strong{MySQL} -@code{GROUP} function like @code{SUM} or @code{COUNT()}. - -@code{CREATE FUNCTION} saves the function's name, type, and shared library -name in the @code{mysql.func} system table. You must have the -@strong{insert} and @strong{delete} privileges for the @code{mysql} database -to create and drop functions. - -All active functions are reloaded each time the server starts, unless -you start @code{mysqld} with the @code{--skip-grant-tables} option. In -this case, UDF initialization is skipped and UDFs are unavailable. -(An active function is one that has been loaded with @code{CREATE FUNCTION} -and not removed with @code{DROP FUNCTION}.) - -For instructions on writing user-definable functions, see @ref{Adding -functions}. For the UDF mechanism to work, functions must be written in C or -C++, your operating system must support dynamic loading and you must have -compiled @code{mysqld} dynamically (not statically). |