The document discusses the MySQL client/server architecture and components. The key points are:
- MySQL uses a client/server model where client programs connect to the MySQL server to access and manipulate databases.
- The MySQL server (mysqld) manages database access and storage. It supports many simultaneous client connections.
- Client programs like MySQL Query Browser, mysql, and mysqldump allow users to interact with and administer databases managed by the MySQL server.
- The client and server can run on the same or different hosts and operating systems, allowing remote database access.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
240 views
MySQL Questions Answers
The document discusses the MySQL client/server architecture and components. The key points are:
- MySQL uses a client/server model where client programs connect to the MySQL server to access and manipulate databases.
- The MySQL server (mysqld) manages database access and storage. It supports many simultaneous client connections.
- Client programs like MySQL Query Browser, mysql, and mysqldump allow users to interact with and administer databases managed by the MySQL server.
- The client and server can run on the same or different hosts and operating systems, allowing remote database access.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18
Unit-1 Client/Server Concepts
1- Explain the General MySQL Client Server Architecture.
Answer- MySQL operates in a networked environment using client/server architecture. In other words, a central program acts as a server, and various client programs connect to the server to make requests. A MySQL installation has the following major components: MySQL Server, Client programs and MySQL non client utilities. MySQL Server MySQL Server, or mysqld, is the database server program. The server manages access to the actual database (schema) on disk and in memory. MySQL Server is multi-threaded and supports many simultaneous client connections. Clients can connect via several connection protocols. For managing database contents, the MySQL server features a modular architecture. Client Programs These are programs that are used for communicating with the server to manipulate the databases. MySQL AB provides following client programs: MySQL Query Browser and MySQL Administrator are graphical interfaces to the server. mysql is a command-line program that acts as a text-based front end for the server. Other command-line clients include mysql import for importing data files, mysqldump for making backups, mysql admin for server administration, and mysqlcheck for checking the integrity of the database files.
MySQL Client/Server Model MySQL runs on many varieties of Windows, Unix, and Linux. But client/server communication is not limited to environments where all computers run the same operating system. Client programs can connect to a server running on the same host or a different host, and the client and server host need not have the same operating system. For example, client programs can be used on Windows to connect to a server that is running on Linux. 2- What are MySQL client programs and how to run them? Explain with suitable examples. Answer-
Client Programs These are programs that are used for communicating with the server to manipulate the databases. MySQL AB provides following client programs: MySQL Query Browser and MySQL Administrator: MySQL Workbench provides DBAs and developers an integrated tools environment for: o Database Design &Modeling o SQL Development o Database Administration o Database Migration mysql:mysql is one of the MySQL client programs. It is a command-line program that acts as a text-based front end for the MySQL Server. It's used for issuing queries and viewing the results interactively from a terminal window. To determine the options supported by a MySQL program, invoke it with the --help option. shell>mysql help to connect to the server use the default hostname and username values with no password as follows: shell>mysql After successful connection, you can select or change the default database while running mysql, issue a USE db_name statement, where db_name is the name of the database you'd like to use.
The following statement makes world the default database: mysql> USE world;
You can get current database name like below: mysql> SELECT database();
To get all available databases, run the following command: mysql> SHOW databases; Other command-line clients o mysql import for importing data files o mysqldump for making backups o mysql admin for server administration, and o mysqlcheck for checking the integrity of the database files.
Unit-2 MySQL Client Program 1- What are the two modes of interaction of MySQL Client Program? Write down the differences between the two modes. Answer- mysql is a general-purpose client program for issuing queries and retrieving their results. It can be used interactively or in batch mode to read queries from a file. To invoke mysql interactively from the command line, specify any necessary connection parameters after the command name: shell>mysql -u user_name -p -h host_name You can also provide a database name to select that database as the default database: shell>mysql -u user_name -p -h host_namedb_name Batch mode is useful for running queries that have been prewritten and stored in a file called script file. Its especially valuable for issuing a complex series of queries thats difficult to enter manually, or queries that need to be run automatically by a job scheduler without user intervention. To process a script file is by executing it with a SOURCE command from within mysql: mysql> SOURCE input_file; Notice that there are no quotes around the name of the file. mysql executes the queries in the file and displays any output produced. The file must be located on the client host where youre running mysql. The filename must either be an absolute pathname listing the full name of the file For example, if you started mysql on a Windows machine in the C:\mysql directory and your script file is my_commands.sql in the C:\scripts directory, both of the following SOURCE commands tell mysql to execute the SQL statements in the file: mysql> SOURCE C:\scripts\my_commands.sql; mysql> SOURCE ..\scripts\my_commands.sql; 2- What are statement terminators in MySQL? Explain various options for statement terminators in MySQL with suitable examples. A terminator is necessary after each statement because mysql allows several queries to be entered on a single input line. There are several terminators to end a statement. Two terminators are the semicolon character (;) and the \g sequence. Theyre equivalent and may be used interchangeably: mysql> SELECT VERSION(), DATABASE(); +----------------+-------------+ | VERSION() | DATABASE() | +----------------+-------------+ | 5.0.10-beta-log | world | +----------------+-------------+ mysql> SELECT VERSION(), DATABASE()\g +----------------+-------------+ | VERSION() | DATABASE() | +----------------+-------------+ | 5.0.10-beta-log | world | +----------------+-------------+ The \G sequence also terminates queries, but causes mysql to display query results in a vertical style that shows each output row with each column value on a separate line: mysql> SELECT VERSION(), DATABASE()\G *************************** 1. row *************************** VERSION(): 5.0.10-beta-log DATABASE(): world The \G terminator is especially useful if a query produces very wide output lines because vertical format can make the result much easier to read. If you are using mysql to define a stored routine or a trigger that uses compound statement syntax and consists of multiple statements, the definition will contain semicolons internally. In this case, it is necessary to redefine the ; terminator to cause mysql to pass semicolons in the definition to the server rather than interpreting them itself. 3- What are MySQL(Client) Commands? What are long form and short form for them?. Answer- When you issue an SQL statement while running mysql, the program sends the statement to the MySQL server to be executed. SELECT, INSERT, UPDATE, and DELETE are examples of this type of input. mysql also understands a number of its own commands that arent SQL statements. The QUIT and SOURCE commands that have already been discussed are examples of mysql commands. Another example is STATUS, which displays information about the current connection to the server, as well as status information about the server itself. Here is what a status display might look like: mysql> STATUS; A full list of mysql commands can be obtained using the HELP command.mysql commands have both a long form and a short form. The long form is a full word (such as SOURCE, STATUS, or HELP). The short form consists of a backslash followed by a single character (such as \., \s, or \h). The long forms may be given in any lettercase. The short forms are case sensitive. 4- What are the different types of Output Formats in MySQL? How will you change your output from default format to another format? Answer- By default, mysql produces output in one of two formats, depending on whether you use it in interactive or batch mode: 1. When invoked interactively, mysql displays query output in a tabular format that uses bars and dashes to display values lined up in boxed columns. 2. When you invoke mysql with a file as its input source on the command line, mysql runs in batch mode with query output displayed using tab characters between data values. To override the default output format, use these options: a. --batch or -B Produce batch mode (tab-delimited) output, even when running interactively. b. --table or -t Produce tabular output format, even when running in batch mode. In batch mode, you can use the --raw or -r option to suppress conversion of characters such as newline and carriage return to escape-sequences such as \n or \r. In raw mode, the characters are printed literally. To select an output format different from either of the default formats, use these options: c. --html or -H Produce output in HTML format. d. --xml or -X Produce output in XML format. 5- What is -safe--update option MySQL? What are its effects on MySQL statements? Answer- Its possible to inadvertently issue statements that modify many rows in a table or that return extremely large result sets. The --safe-updates option helps prevent these problems. The option is particularly useful for people who are just learning to use MySQL. -- safe-updates has the following effects: UPDATE and DELETE statements are allowed only if they include a WHERE clause that specifically identifies which records to update or delete by means of a key value, or if they include a LIMIT clause. Output from single-table SELECT statements is restricted to no more than 1,000 rows unless the statement includes a LIMIT clause. Multiple-table SELECT statements are allowed only if MySQL will examine no more than 1,000,000 rows to process the query. The --i-am-a-dummy option is a synonym for --safe-updates.
Unit-3 MySQL Architecture 1- What is difference among mysqld, mysqladmin, mysqldump, mysqlimport and mysqlcheck? Answer- mysqld "mysqld" is MySQL server daemon program which runs quietly in background on your computer system. Invoking "mysqld" will start the MySQL server on your system. Terminating "mysqld" will shutdown the MySQL server. Here is an example of invoking "mysqld" with the "--console" option: >cd \mysql\bin >mysqld --console mysqladmin mysqladmin is a client tool program for database server administrators to manage a MySQL server remotely. The syntax to run mysqladmin is: \mysql\bin\mysqladmin [options] command [command-arg] It support a number of commonly used commands like: "mysqladmin shutdown" - Shuts down the server. "mysqladmin ping" - Checks if the server is alive or not. "mysqladmin status" - Displays several important server status values. "mysqladmin version" - Displays version information of the server. "mysqladmin create databaseName" - Creates a new database. "mysqladmin drop databaseName" - Drops an existing database.
Mysqldump Mysqldump is part of the mysql relational database package which allows you to "dump" a database, or a collection of databases, for backup or transferral to another SQL server. The server that imports the databases does not have to be mysql. The typical way to use mysqldump is one of the following commands: mysqldump [options] db_name [tables] mysqldump [options] --databases DB1 [DB2 DB3...] mysqldump [options] --all-databases
mysqlimport "mysqlimport" - A command-line interface for administrators or end users to load data files into tables program tool to load data into tables. Here is a sample commands supported by "mysqlimport": "mysqlimport databaseName fileName" - Imports the data from the specified file to the specified database. The data will be loaded into the table who's name matches the specified file name.
mysqlcheck "mysqlcheck" is a command-line interface for administrators to check and repair tables. Here are some sample commands supported by "mysqlcheck": "mysqlcheck databaseName tableName" - Checks the specified table in the specified database. "mysqlcheck databaseName" - Checks all tables in the specified database. "mysqlcheck --all-databases" - Checks all tables in all databases. "mysqlcheck --analyze databaseName tableName" - Analyzes the specified table in the specified database. "mysqlcheck --repair databaseName tableName" - Repairs the specified table in the specified database.
2- What are different communication protocols in MySQL? Explain them in brief. Answer- A MySQL client program can connect to a server running on the same machine. This is a local connection. A client can also connect to a server running on another machine, which is a remote connection. MySQL supports connections between clients and the server using several networking protocols, as shown in the following table. Protocol Types of Connections Supported Operating Systems TCP/IP Local, remote All Unix socket file Local only Unix only Named pipe Local only Windows only Shared memory Local only Windows only Protocols are used for connecting to either local or remote servers. TCP/IP connections are supported by any MySQL server. Unix socket file connections are supported by all Unix servers. Named-pipe connections are supported only on Windows. Shared-memory connections are supported by all Windows servers. MySQL communication protocols are implemented by various libraries and program drivers. Client programs included with MySQL distributions (mysql, mysqladmin, and so forth) establish connections to the server using the native C client library. 3- How is MySQL data stored on disk? Brief various directory (ies) and file(s) created mainly for MyISAM and InnoDB storage engines. Answer- MySQL Server uses disk space in several ways, primarily for directories and files that are found under a single location known as the server's data directory. The server uses its data directory to store all the following: Database directories. Each database corresponds to a single directory under the data directory, regardless of what types of tables you create in the database. For example, a given database is represented by one directory whether it contains MyISAM tables, InnoDB tables, or a mix of the two. Table format files (.frm files) that contains a description of table structure. Every table has its own .frm file, located in the appropriate database directory. This is true no matter which storage engine manages the table. Data and index files are created for each table by some storage engines and placed in the appropriate database directory. For example, the MyISAM storage engine creates a data file and an index file for each table. The InnoDB storage engine has its own tablespace and log files. The tablespace contains data and index information for all InnoDB tables, as well as the undo logs that are needed if a transaction must be rolled back. The log files record information about committed transactions and are used to ensure that no data loss occurs. By default, the tablespace and log files are located in the data directory. The default tablespace file is named ibdata1 and the default log files are named ib_logfile0 and ib_logfile1. (It is also possible to configure InnoDB to use one tablespace file per table. In this case, InnoDB creates the tablespace file for a given table in the table's database directory.) Server log files and status files. These files contain information about the statements that the server has been processing. Logs are used for replication and data recovery, to obtain information for use in optimizing query performance, and to determine whether operational problems are occurring. 4- MySQL Server allocates memory for what kinds of information? Explain the various types of buffers (caches) used by MySQL to hold information in memory. Answer- MySQL Server memory use includes data structures that the server sets up to manage communication with clients and to process the contents of databases. The server allocates memory for many kinds of information as it runs: Thread handlers. The server is multi-threaded, and a thread is like a small process running inside the server. For each client that connects, the server allocates a thread to it to handle the connection. For performance reasons, the server maintains a small cache of thread handlers. The server uses several buffers (caches) to hold information in memory for the purpose of avoiding disk access when possible: a. Grant table buffers. The grant tables store information about MySQL user accounts and the privileges they have. The server loads a copy of the grant tables into memory for fast access-control checking. b. A key buffer holds index blocks for MyISAM tables. By caching index blocks in memory, the server often can avoid reading index contents repeatedly from disk for index-based retrievals and other index-related operations such as sorts. c. The table cache holds descriptors for open tables. For frequently used tables, keeping the descriptors in the cache avoids having to open the tables again and again. d. The server supports a query cache that speeds up processing of queries that are issued repeatedly. e. The host cache holds the results of hostname resolution lookups. These results are cached to minimize the number of calls to the hostname resolver. f. The InnoDB storage engine logs information about current transactions in a memory buffer. When a transaction commits, the log buffer is flushed to the InnoDB log files, providing a record on disk that can be used to recommit the transaction if it is lost due to a crash. If the transaction rolls back instead, the flush to disk need not be done at all
Unit-5 Locking 1- What is locking? Explain its importance in MySQL with suitable examples. Answer-Locking is a mechanism that prevents problems from occurring with simultaneous data access by multiple clients. Locks are managed by the server: It places a lock on data on behalf of one client to restrict access by other clients to the data until the lock has been released. The lock allows access to data by the client that holds the lock, but places limitations on what operations can be done by other clients that are contending for access. The effect of the locking mechanism is to serialize access to data so that when multiple clients want to perform conflicting operations, each must wait its turn. Not all types of concurrent access produce conflicts, so the type of locking that is necessary to allow a client access to data depends on whether the client wants to read or write: If a client wants to read data, other clients that want to read the same data do not produce a conflict, and they all can read at the same time. However, another client that wants to write (modify) data must wait until the read has finished. If a client wants to write data, all other clients must wait until the write has finished, regardless of whether those clients want to read or write. A lock on data can be acquired implicitly or explicitly: For a client that does nothing special to acquire locks, the MySQL server implicitly acquires locks as necessary to process the clients statements safely. For example, the server acquires a read lock when the client issues a SELECT statement and a write lock when the client issues an INSERT statement. Explicit locking may be necessary when a client needs to perform an operation that spans multiple statements and that must not be interrupted by other clients. For example, an application might select a value from one table and then use it to determine which records to update in a set of other tables. 2- What are the levels at which data locking occurs in MySQL? Explain their characteritics. Answer-Data locking in MySQL occurs at different levels. Explicit locks acquired with LOCK TABLES are table locks. For implicit locks, the lock level that MySQL uses depends on the storage engine: MyISAM, MEMORY, and MERGE tables are locked at the table level. BDB tables are locked at the page level. InnoDB tables are locked at the row level. The different levels of locking granularity have different concurrency characteristics: Table locking is not as desirable as page or row locking for concurrency in a mixed read/write environment. A table lock prevents other clients from making any changes to the table, even if the client that holds the lock is not accessing the parts of the table that other clients want to modify. With page and row locks, a client that locks a page or row does not prevent changes by other clients to other pages or rows. Deadlock cannot occur with table locking as it can with page or row locking. For example, with row-level locking, two clients might each acquire a lock on different rows. If each then tries to modify the row that the other has locked, neither client can proceed. This is called deadlock. With table locking, the server can determine what locks are needed and acquire them before executing a statement, so deadlock never occurs. 3- What are Implicit and Explicit locking? Explain them with suitable commands and examples. Answer-
Implicit Locking: For a client that does nothing special to acquire locks, the server implicitly acquires locks as necessary to process the client's statement safely. For example, the server acquires a read lock when the client issues a SELECT statement, and a write lock when the client issues an INSERT statement. Implicit locks are acquired only for the duration of a single statement. Explicit Locking: If implicit locking is not sufficient for a client's purposes, it can manage locks explicitly by acquiring them with LOCK TABLES and releasing them with UNLOCK TABLES. Explicit locking may be necessary when a client needs to perform an operation that spans multiple statements that must not be interrupted by other clients. For example, an application might select a value from one table and then use it to determine which records to update in a set of other tables. With implicit locking Explicit locking can improve performance for multiple statements executed as a group while the lock is in effect. The LOCK TABLES statement names each table to be locked and the type of lock to be acquired. The following statement acquires a read lock on the Country table and a write lock on the City table: LOCK TABLES Country READ, City WRITE; To use LOCK TABLES, you must have the LOCK TABLES privilege, and the SELECT privilege for each table to be locked. To release explicit locks, issue an UNLOCK TABLES statement. This statement names no tables, because it releases all explicit locks held by the issuing client. 4- What is Explicit locking? Explain the various lock types available in Explicit Locking? Answer- Explicit Locking: If implicit locking is not sufficient for a client's purposes, it can manage locks explicitly by acquiring them with LOCK TABLES and releasing them with UNLOCK TABLES. Explicit locking may be necessary when a client needs to perform an operation that spans multiple statements that must not be interrupted by other clients. For example, an application might select a value from one table and then use it to determine which records to update in a set of other tables. With implicit locking The following statement acquires a read lock on the Country table and a write lock on the City table: LOCK TABLES Country READ, City WRITE; The following list describes the available lock types and their effects: READ A READ lock locks a table for read queries such as SELECT that retrieve data from the table. It does not allow write operations even for the client that holds the lock. A client that wants to write to a table that is read-locked must wait until all clients currently reading from it have finished and released their locks. WRITE A WRITE lock is an exclusive lock. It can be acquired only when a table is not being used. Once acquired, only the client holding the write lock can read from or write to the table. Other clients can neither read from nor write to it. READ LOCAL Locks a table for reading, but allows concurrent inserts. A concurrent insert is an exception to the readers block writers principle. It applies only to MyISAM tables. If a MyISAM table has no holes in the middle resulting from deleted or updated records, inserts always take place at the end of the table. In that case, a client that is reading from a table can lock it with a READ LOCAL lock to allow other clients to insert into the table while the client holding the read lock reads from it. If a MyISAM table does have holes, you can remove them by using OPTIMIZE TABLE to defragment the table. LOW_PRIORITY WRITE Locks a table for writing, but acquires the lock with a lower priority. That is, if the client must wait for the lock, other clients that request read locks during the wait are allowed to get their locks first. A normal write lock request is satisfied when no other clients are using the table. 5- What is Advisory locking? How are they implemented, explain with examples? Answer- Advisory Locking An advisory lock is a cooperative lock. That is, an advisory lock has no power to prevent data access by other clients, but instead is based on the concept that all clients will use an agreed-upon convention to cooperate for use of a resource. Advisory locks are implemented using a set of function calls. To acquire a lock, use the GET_LOCK()function: mysql> SELECT GET_LOCK('my lock', 5); +------------------------ + | GET_LOCK('my lock', 5) | +------------------------ + | 1 | +------------------------ + The first argument is a string that specifies the name to be locked, and the second argument is a timeout value in seconds that indicates how long to wait for the lock if it cannot be acquired immediately. GET_LOCK() returns 1 for success, 0 if a timeout occurs and the lock cannot be acquired, or NULL if an error occurs. A client that has acquired an advisory lock can release it by calling RELEASE_LOCK(): mysql> SELECT RELEASE_LOCK('my lock'); +------------------------- + | RELEASE_LOCK('my lock') | +------------------------- + | 1 | +------------------------- + RELEASE_LOCK() returns 1 if the lock was released successfully, 0 if the name was locked but not by the client requesting the release, and NULL if the name was not locked. An advisory lock also is released if the client makes another call to GET_LOCK() or closes its connection to the server. Two other functions are available for checking the status of advisory locks: IS_FREE_LOCK(lock_name) returns 1 if the name is not locked, 0 if it is locked, and NULL if an error occurs. IS_USED_LOCK(lock_name) returns the connection ID of the client that holds the lock on the name, or NULL if the name is not locked. Unit-6 Storage Engines 1- Explain the concept storage engine in MySQL. Which are the most common storage engines in MySQL? Answer- MySQL Storage Engines All tables managed by MySQL Server have certain similarities. For example, every table in a database has a format (.frm) file in the database directory. This file, which stores the definition of the tables structure, is created by the server. Tables have differences as well, which are tied to the storage engines that the server uses to manage table contents. Each storage engine has a particular set of operational characteristics. For example, engines may create additional disk files to accompany the .frm files, but the types of files that they create to manage data and index storage vary per engine. When you create a table, you can choose what storage engine to use. Typically, this choice is made according to which storage engine offers features that best fit the needs of your application. MyISAM table-level locking works best for a query mix that is heavily skewed toward retrievals and includes few updates. Use InnoDB if you must process a query mix containing many updates. InnoDBs use of row-level locking and multi-versioning provides good concurrency for a mix of retrievals and updates. To specify a storage engine explicitly in a CREATE TABLE statement, use an ENGINE option. The following statement creates t as an InnoDB table: CREATE TABLE t (i INT) ENGINE = InnoDB; To determine which storage engine is used for a given table; you can use the SHOW CREATE TABLE or the SHOW TABLE STATUS statement: mysql> SHOW CREATE TABLE City\G The INFORMATION_SCHEMA TABLES table contains storage engine information as well: mysql> SELECT TABLE_NAME, ENGINE FROM INFORMATION_SCHEMA.TABLES -> WHERE TABLE_SCHEMA = 'world'; To reduce memory use, dont configure unneeded storage engines into the server. This requires that you compile MySQL from source rather than using a precompiled binary distribution. To see what storage engines are compiled into your server and whether they are available at runtime, use the SHOW ENGINES statement mysql> SHOW ENGINES\G 2- What are characteristics of MyISAM storage engines? What are the features supported by MyISAM which will be used by MySQL in future? The following characteristics of the MyISAM storage engine are improvements over the older ISAM engine: All data values are stored with the low byte first. This makes the data machine and operating system independent. Large files (up to 63-bit file length) are supported on filesystems and operating systems that support large files. Dynamic-sized rows are much less fragmented when mixing deletes with updates and inserts. This is done by automatically combining adjacent deleted blocks and by extending blocks if the next block is deleted. The maximum number of indexes per table is 64 (32 before MySQL 4.1.2).. The maximum key length is 1000 bytes (500 before MySQL 4.1.2). BLOB and TEXT columns can be indexed. NULL values are allowed in indexed columns. This takes 0-1 bytes per key. All numeric key values are stored with the high byte first to allow better index compression. Index files are usually much smaller with MyISAM than with ISAM. Internal handling of one AUTO_INCREMENT column per table. MyISAM automatically updates this column for INSERT/UPDATE. This makes AUTO_INCREMENT columns faster (at least 10%).. If a table doesn't have free blocks in the middle of the data file, you can INSERT new rows into it at the same time that other threads are reading from the table. (These are known as concurrent inserts.) You can put the data file and index file on different directories to get more speed with the DATA DIRECTORY and INDEX DIRECTORY table options to CREATE TABLE. There is a flag in the MyISAM index file that indicates whether the table was closed correctly. If mysqld is started with the --myisam-recover option, MyISAM tables are automatically checked (and optionally repaired) when opened if the table wasn't closed properly. myisamchk marks tables as checked if you run it with the --update-state option. myisamchk --fast checks only those tables that don't have this mark. myisamchk --analyze stores statistics for key parts, not only for whole keys as in ISAM. myisampack can pack BLOB and VARCHAR columns; pack_isam cannot. MyISAM also supports the following features, which MySQL will be able to use in the near future: Support for a true VARCHAR type; a VARCHAR column starts with a length stored in two bytes. Tables with VARCHAR may have fixed or dynamic record length. VARCHAR and CHAR columns may be up to 64KB. A hashed computed index can be used for UNIQUE. This will allow you to have UNIQUE on any combination of columns in a table. (You can't search on a UNIQUE computed index, however.) 3- Explain advantages of MyISAM over InnoDB? Answer- The following are the advantages of MyISAM over InnoDB: MyISAM tables are stored in separate files in compressed mode, whereas InnoDB tables are stored in table space. More options for further optimization in MyISAM, whereas there is no further optimization possible with InnoDB. Data except for TEXT and BLOB can occupy at most 8000 bytes in InnoDB. Full text indexing available in MyISAM, whereas no full text indexing is available for InnoDB. Count (*) functions execution is slower in InnoDB because of table space complexity. 4- When is InnoDB engine preferred over MyISAM? Explain the transaction model of InnoDB engine. 5- Differentiate MyISAM and InnoDB storage engines? How they differ in their locking mechanism? 6- What is Merge table? When are they used? What are their disadvantages? 7- Write short notes on- i) Heap (Memory) Engine ii) Federated Engine iii) Cluster Engine
Unit-8 Backup and Recovery 1- What are the principles of backup? Answer- Principles of backup Make backups regularly. Enable the binary log so that we have a record of changes made after a given backup. Flush the logs when backing up so that the server will begin a new binary log file. Store the data directory and backups on different physical devices so that a device failure cannot destroy both. Include the backups in the regular filesystem backup procedures.
2- Write difference between Binary and Textual backups. Answer- Binary Backup A copy of the files in which database contents are stored. Preserves the databases in exactly the same format in which MySQL itself stores them on disk. To restore copy the files back to their original locations. Techniques o copy commands (such as cp or tar) o mysqlhotcopy o InnoDB Hot Backup Its faster to make a binary backup. For transferring databases to another machine that uses a different architecture, the files must be binary portable. With binary backup methods, its necessary to make sure that the server does not modify the files while the backup is in progress. Binary backups depends on which storage engine created the tables Text Backup A text backup is a dump of database contents into text files. Restoration involves loading the file contents back into databases by processing them through the server. Techniques o SELECT ... INTO OUTFILE SQL statement o Mysqldump o MySQL Administrator. Its slower to make a text backup because the server must read tables Text backups are portable and independent of machine architecture. With text backup methods, the server must be running because it must read the files that are to be backed up. Text backup procedures are more general and can be used for tables created by any storage engine. 3- What are the steps involved in making Binary MyISAM Backups? Answer- Making Binary MyISAM Backups To make a binary backup of a MyISAM table, copy the .frm, .MYD, and .MYI files. While copying the table must not be in use so: Either stop the server while copying the table Use an appropriate locking protocol to prevent server access to the table. For example: o mysql> USE world; o mysql> LOCK TABLES Country READ; o mysql> FLUSH TABLES Country; After backup release the lock on the table: o mysql> UNLOCK TABLES; To recover a MyISAM table from a binary backup, stop the server, copy the backup table files into the appropriate database directory, and restart the server. 4- What are the conditions for Binary backup? Brief them for MyISAM and InnoDB tables. Answer- Conditions for Binary Portability For MyISAM, binary portability means that one can directly copy the files for a MyISAM table from one MySQL server to another on a different machine and the second server will be able to access the table. For InnoDB, binary portability means that one can directly copy the tablespace files from a MySQL server on one machine to another server on a different machine and the second server will be able to access the tablespace. MyISAM tables and InnoDB tablespaces are binary portable if two conditions are met: o Both M/Cs must use 2s-complement integer arithmetic. o Both M/Cs must use IEEE floating-point format, or else the tables must contain no floating-point columns. o We should use lowercase names for databases and tables. Using lowercase names allows binary portability between Windows and Unix. To force lowercase names, put following in an option file: o [mysqld] o lower_case_table_names=1 5- How MySQL Administrator helps in Making Text Backups? Explain the procedure. Answer- Making Text Backups with MySQL Administrator GUI program to provide backup and restore capabilities. It generates backup files containing SQL statements that can be reloaded into your MySQL server to re-create databases and tables. These files are similar to the SQL-format backup files generated bymysqldump. MySQL Administrator stores backup configuration options as projects. One can select and execute these projects later to perform a backup operation based on a given set of specifications. The project approach enables us to easily select from among multiple types of backups. We can select backup projects on demand or schedule them for periodic execution. 6- What is the big ibdata file that is in all the backups? Answer- You might find your backup data taking more space than expected because of a large file with a name such as ibdata1. This file represents the InnoDB system tablespace, which grows but never shrinks, and is included in every full and incremental backup. To reduce the space taken up by this file in your backup data: After doing a full backup, do a succession of incremental backups, which take up less space. The ibdata1 file in the incremental backups is typically much smaller, containing only the portions of the system tablespace that changed since the full backup. Set the configuration option innodb_file_per_table=1 before creating your biggest or most active InnoDB tables. Those tables are split off from the system tablespaces into separate .ibd files, which are more flexible in terms of freeing disk space when dropped or truncated, and can be individually included or excluded from backups. If your system tablespace is very large because you created a high volume of InnoDB data before turning on the innodb_file_per_table setting, you might use mysqldump to dump the entire instance, then turn on innodb_file_per_table before re-creating it, so that all the table data is kept outside the system tablespace. 7- Write short notes on- i) Mysqlhtocopy Answer- mysqlhotcopy A Perl script, requires the DBI module to be installed. It runs on Unix and NetWare. Works for MyISAM tables but not InnoDB tables. Operation of mysqlhotcopy is fast Steps involved: o mysqlhotcopy connects to the local MySQL server o locks the tables so that the server will not change them, flushes the tables to make sure that any pending changes are written to disk o Copies the table files. When it has finished the copy operation, it unlocks the tables. Many options, invoke it with the --help option. Some examples: Back up the world database to a directory named world in the /var/archive directory: o shell> mysqlhotcopy world /var/archive Back up only the tables in the world database whose name contains Country: o shell> mysqlhotcopy world./Country/ /var/archive ii) InnoDB Hot Backup Answer- InnoDB Hot Backup The InnoDB Hot Backup program (ibbackup) is a commercial product available from Innobase Oy. It can back up InnoDB tables while the server is running without disturbing normal database activity. Its available for Unix and Windows. iii) Mysqldump Answer- Mysqldump It can dump all databases, specific databases, or specific tables. mysqldump can back up local or remote servers, although the destination for the dump files depends on how you invoke it. It works for tables created by any storage engine. Output files are written in text format and are portable. Three general modes of operation: Default: o shell> mysqldump world > world.sql o shell> mysqldump world City Country > city_country.sql --databases option o shell> mysqldump --databases world test> world_and_test.sql --all-databases option o shell> mysqldump --all-databases > alldb.sql iv) Backing Up Log and Status Files Answer- Backing Up Log and Status Files In addition to backing up the databases, we should also back up the following files: Binary log files. Necessary because the binary logs store updates that have been made after the backup was made. Option files used by the server (my.cnf and my.ini files). These files contain configuration information that must be restored after a crash. Replication slave servers create a master.info file that contains information needed for connecting to the master server, and a relay- log.info file that indicates the current progress in processing the relay logs. Replication slaves create data files for processing LOAD DATA INFILE statements. These files are located in the directory named by the slave_load_tmpdir system variable, which can be set by starting the server with the --slave-load-tmpdir option. If slave_load_tmpdir is not set, the value of the tmpdir system variable applies. The data files to back up have names beginning withSQL_LOAD-. To back up the preceding files, you can use normal file system operation. Static files such as option files can be backed up with no special precautions. Dynamic files such as logs that the server changes as it runs are best backed up with the server stopped.