In case you’re new to MySQL and don’t already have a working installation of the software, this appendix guides you through the process of obtaining, installing, configuring, and testing
Trang 1Tip Don’t assume everything is fine because you issued the START SLAVE command successfully—monitor the slave’s activities by using the SHOW SLAVE STATUS command
You can also read the slave’s error log to make sure everything is okay.
Checking Replication Status
The SHOW SLAVE STATUS command provides information about the slave server’s status It should be run on the slave database server Here’s what it looks like:
Relay_Log_File: ACHILLES-relay-bin.000006 Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes
Slave_SQL_Running: Yes Replicate_Do_DB:
when replication begins
reading when replication begins
when replication begins
replication begins
T able 13-1 Common Options for the CHANGE MASTER TO Command
Trang 21 row in set (0.00 sec)
In addition to displaying information on the current server and user credentials, the SHOW SLAVE STATUS command provides information on how many times the slave server will attempt to connect to the master server, the status of slave I/O and SQL threads, the name and position in the master’s binary log, the name and position
in the slave’s relay log, the size of relay log files, the databases and tables excluded from replication, and whether SSL connections are in use
The SHOW PROCESSLIST command displays information about the threads on the server, and was discussed in Chapter 10 In a replication context, it can be used to obtain status information on both the master and the slave For each thread, the output
is shown in various fields, as illustrated:
Info: show processlist
2 rows in set (0.00 sec)
Trang 3Command: Sleep Time: 1937 State:
Info: NULL
*************************** 2 row ***************************
Id: 13 User: system user Host:
db: NULL Command: Connect Time: 1941 State: Waiting for master to send event Info: NULL
*************************** 3 row ***************************
Id: 14 User: system user Host:
db: NULL Command: Connect Time: 1941 State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
*************************** 4 row ***************************
Id: 16 User: root Host: CERBERUS:1294 db: NULL
Command: Query Time: 0 State: NULL Info: SHOW PROCESSLIST
4 rows in set (0.03 sec)
Of these various fields, the one you’ll usually be most interested in is the State
field, which contains information about what the server is doing For example, on the master server, you could see something like ‘Sending binlog event to slave.’ On the slave’s I/O thread, you might see ‘Connecting to master’ or ‘Requesting binlog dump.’ On the slave’s SQL thread, a common state is ‘Reading event from the relay log.’ You’ll also find information (where appropriate) about which database the thread
is accessing, the statement it’s currently executing, and how long (in seconds) the thread has been executing.
Trang 4Working with Master Server Binary Logs
As discussed earlier, when replicating, everything is based on the binary log on the master server To display events in this log, the SHOW BINLOG EVENTS command can
be used Here’s an example:
Info: use `db1`; delete from log where RecordID = 37
2 rows in set (0.00 sec)
By itself, this command displays all events in the binary log This can be a consuming process when dealing with large binary logs Therefore, the MySQL manual suggests limiting the output of this command by only showing events starting from a specific position in the log (the FROM clause) and displaying a specified number of events (the LIMIT clause), as in the previous example
time-The PURGE MASTER command deletes all binary logs on the master server prior to a specified date or log number As an example, suppose you want to purge all the master
binary update logs prior to the one named bin_log.999 You would execute the following:
(Master server)
mysql> PURGE MASTER LOGS TO mysql-bin.000999;
Query OK, 0 rows affected (0.00 sec)
Note that this statement requires the SUPER privilege
For additional information about the master server’s binary logs, use the SHOW MASTER STATUS command, which displays the current binary log name and position being written to Here’s an example:
Trang 5To learn more about the topics discussed in this chapter, consider visiting the following links:
A comparison of replication methods, at http://dev.mysql.com/doc/refman/5.1/
• en/replication-sbr-rbr.html Replication variables and options, at http://dev.mysql.com/doc/refman/5.1/
• en/replication-options.html Replication thread states, at http://dev.mysql.com/doc/refman/5.1/en/
• master-thread-states.html Replication tips, at http://dev.mysql.com/doc/refman/5.1/en/replication-notes
• html
Trang 7installing MySQL
and the Sample database
Trang 8T his book discusses the MySQL RDBMS and the tools and commands it provides
to store, manipulate, and retrieve data in databases In case you’re new to MySQL and don’t already have a working installation of the software, this appendix guides you through the process of obtaining, installing, configuring, and testing the MySQL server It discusses the different versions of MySQL, covers installation of binary versions on both UNIX and Microsoft Windows, and helps create a server environment suitable for running the code examples in this book.
Caution This appendix is intended to provide an overview and general guide to the process
of installing and configuring MySQL on UNIX and Windows It is not intended as a
replacement for the installation instructions that ship with MySQL If you encounter difficulties installing or configuring MySQL, visit the online MySQL manual or search the Web for detailed troubleshooting information and advice (some links are provided at the end
There are two primary decisions to be made when selecting which MySQL distribution
to download and use.
Choosing which version to install
•
Choosing between binary and source distributions
•
Choosing Which Version to Install
Sun Microsystems currently makes two versions of the MySQL database server available
on their website.
MySQL Community Server
• This is the General Public License (GPL) version
of the MySQL database server, which includes support for both regular, nontransactional storage engines and transaction-safe tables It is suited for production environments requiring a stable, flexible, and robust database engine, and can be downloaded free of charge.
MySQL Enterprise Server
• This version is only available as part of the MySQL Enterprise platform, a commercial offering aimed at enterprise customers with business-critical applications It includes all the features of the Community Server, along with automated updates and hot fixes, consulting support, and monitoring services
So long as you’re willing to put in the time and effort needed to manage the MySQL database server and don’t mind resolving technical issues yourself, the
Trang 9MySQL Community Server is the most appropriate choice It’s the version used in all the examples in this book, and it’s stable, feature-rich, and suited for most common applications However, business customers who need automated updates, continuous system monitoring, and access to 24/7 technical and consulting support would probably be better served by a MySQL Enterprise subscription
Choosing Between Binary and Source Distributions
Sun Microsystems makes both source and binary distributions of the MySQL database server available for download on their website As of this writing, binary distributions are available for Linux (Red Hat, SuSE, and generic distributions), Solaris, FreeBSD, Mac OS X, 32-bit and 64-bit Windows, HP-UX, and IBM AIX and IBM i5, and source distributions are available for both Windows and UNIX platforms
Windows users must further choose between three different binary distributions: the
“Essentials” distribution, which includes the minimum set of files and an automated installer; the “Complete” distribution, which includes everything in the “Essentials”
distribution plus additional tools such as the MySQL Benchmark Suite; and the
“Noinstall” distribution, which includes everything in the “Complete” distribution
except the automated installer.
In most cases, it’s preferable to use a precompiled binary distribution rather than a source distribution, for two reasons: It is easier to install, and it has been optimized for maximum performance on different platforms by the MySQL development team That said, there are a number of possible situations where a source distribution might be preferable to a binary distribution.
You need to recompile MySQL with different compile-time options from the
• defaults provided by the MySQL team (for example, to set a different value for the default installation path).
You need to compile a smaller, lighter version of MySQL that doesn’t include all
• the features (and overhead) of the standard binary distribution.
You need newer, experimental features that are disabled by default in the
• standard binaries.
You need to make modifications to the server’s source code.
• Source distributions are typically used only by experienced developers who either need to tweak MySQL’s default values for their own purposes or who are interested in studying the source code to see how it works Such users usually also have the time, inclination, and expertise to diagnose and troubleshoot compilation and configuration issues that may arise during the installation process
MySQL versions that don’t come with an automated installer are usually packaged
in either TGZ or ZIP format Therefore, users on both UNIX and Windows platforms will need a decompression tool capable of dealing with Tape Archive (TAR) and GNU
Zip (GZ) files On UNIX, the tar and gzip utilities are appropriate, and are usually
included with the operating system On Windows, a good decompression tool is WinZip, available from www.winzip.com
Trang 10The instructions in the following sections assume that you will be using a binary distribution of MySQL Community Server This distribution can be downloaded from the MySQL website The MySQL software is also mirrored on a number of other sites around the world, and you can make your download more efficient by selecting the site that is geographically closest to you Once downloaded, move to the section titled
“Installing and Configuring MySQL.”
Installing and Configuring MySQL
The next step is to install and configure MySQL for your specific platform The following sections outline the steps for both Windows and UNIX platforms.
Installing on UNIX
MySQL is available in binary form for almost all versions of UNIX, and can be compiled from source for those UNIX variants for which no binary distribution exists This section will discuss installing and configuring MySQL on Linux using a binary distribution; the process for other UNIX variants is similar, though you should refer to the documentation included with the MySQL distribution for platform-specific notes.
To install MySQL from a binary distribution, use the following steps:
1 Ensure that you are logged in as the system’s “root” user.
[user@host]# su – root
2 Extract the contents of the MySQL binary archive to an appropriate directory on
your system—for example, /usr/local/:
[root@host]# cd /usr/local [root@host]# tar -xzvf /tmp/mysql-5.1.30-linux-i686-glibc23.tar.gz
The MySQL files should get extracted into a directory named according to the
format mysql-version-os-architecture—for example, mysql-5.1.30-linux-i686-glibc23
3 For ease of use, set a shorter name for the directory created in the previous step
by creating a soft link named mysql pointing to this directory in the same location:
[root@host]# ln -s mysql-5.1.30-linux-i686-glibc23 mysql
4 For security reasons, the MySQL database server process should never run as the system superuser Therefore, it is necessary to create a special “mysql” user
and group for this purpose Do this with the groupadd and useradd commands,
and then change the ownership of the MySQL installation directory to this user and group:
[root@host]# groupadd mysql [root@host]# useradd –g mysql mysql [root@host]# chown -R mysql /usr/local/mysql [root@host]# chgrp -R mysql /usr/local/mysql
Trang 115 Initialize the MySQL tables with the mysql_install_db initialization script,
included in the distribution:
[root@host]# /usr/local/mysql/scripts/mysql_install_db user=mysql
Figure A-1 demonstrates what you should see when you do this.
As this output suggests, this initialization script prepares and installs the various MySQL base tables and sets up default access permissions for MySQL.
6 Alter the ownership of the MySQL binaries so that they are owned by “root”:
[root@host]# chown -R root /usr/local/mysql
and ensure that the “mysql” user created in step 4 has read/write privileges to the MySQL data directory:
[root@host]# chown -R mysql /usr/local/mysql/data
7 Start the MySQL server by manually running the mysqld_safe script:
[root@host]# /usr/local/mysql/bin/mysqld_safe user=mysql &
MySQL should now start up normally
Once installation has been successfully completed and the server has started up, move down to the section entitled “Testing MySQL” to verify that it is functioning as
it should.
F igure A-1 The output of the mysql_install_db script
Trang 12Installing on Windows
MySQL is available in both source and binary forms for both 32-bit and 64-bit versions
of Microsoft Windows Most often, you will want to use either the “Essentials” or
“Complete” binary distribution, which includes an automated installer to get MySQL
up and running in just a few minutes
To install MySQL from a binary distribution, use the following steps:
1 Log in as an administrator (if you’re using Windows NT/2000/XP/Vista)
2 Double-click the mysql-*.msi file to begin the installation process You should
see a welcome screen (Figure A-2).
3 Select the type of installation required (Figure A-3)
Most often, a Typical Installation will do; however, if you’re the kind who likes tweaking default settings, or if you’re just short of disk space, select the Custom Installation option, and decide which components of the package should be installed.
4 MySQL should now begin installing to your system (Figure A-4).
F igure A-2 Beginning MySQL installation on Windows
Trang 13F igure A-3 Selecting the MySQL installation type
F igure A-4 MySQL installation in progress
Trang 145 Once installation is complete, you should see a success notification At this point, you will have the option to launch the MySQL Server Instance Config Wizard to complete configuration of the software Select this option, and you should see the corresponding welcome screen (Figure A-5).
6 Select the type of configuration (Figure A-6) In most cases, the Standard Configuration will suffice.
7 Install MySQL as a Windows service, such that it starts and stops automatically with Windows (Figure A-7).
8 Enter a password for the MySQL administrator (“root”) account (Figure A-8).
9 The server will now be configured with your specified settings and automatically started You will be presented with a success notification once all required tasks are complete (Figure A-9).
You can now proceed to test the server, as described in the section “Testing MySQL,”
to ensure that everything is working as it should.
F igure A-5 Beginning MySQL configuration on Windows
Trang 15F igure A-6 Selecting the configuration type
F igure A-7 Setting up the MySQL service
Trang 16F igure A-8 Setting the administrator password
F igure A-9 MySQL configuration successfully completed
Trang 17Testing MySQL
First, start up the MySQL command-line client by changing to the bin/ subdirectory of
your MySQL installation directory and typing the following command:
prompt# mysql -u root
You should be rewarded with a prompt, as shown:
Welcome to the MySQL monitor Commands end with ; or \g
Your MySQL connection id is 1 Server version: 5.1.30-community MySQL Community Server (GPL) Type 'help;' or '\h' for help Type '\c' to clear the buffer
mysql>
At this point, you are connected to the MySQL server and can begin executing SQL commands or queries to test whether the server is working as it should Here are a few examples, with their output:
mysql> SHOW DATABASES;
+ -+
| Database | + -+
| mysql |
| test |
+ -+
2 rows in set (0.13 sec) mysql> USE mysql; Database changed mysql> SHOW TABLES; + -+
| Tables_in_mysql |
+ -+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
Trang 18| plugin |
| proc |
| procs_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type | | user |
+ -+
23 rows in set (0.23 sec) mysql> SELECT VERSION(); + -+
| VERSION() |
+ -+
| 5.1.30-community | + -+
1 row in set (0.00 sec)
If you see output similar to that, your MySQL installation is working as it should Exit the command-line client by typing the following command, and you’ll be returned
to your command prompt:
mysql> exit
If you don’t see output like that shown here, or if MySQL throws warnings and errors at you, review the installation procedure in the previous section, as well as the documents that shipped with your version of MySQL, to see what went wrong.
Performing Post-Installation Steps
Once testing is complete, you may wish to perform the following two tasks.
Setting the MySQL Superuser Password
On UNIX, when MySQL is first installed, access to the database server is restricted to the MySQL administrator, aka “root.” By default, this user is initialized with a blank password, which is generally considered a Bad Thing You should, therefore, rectify
this as soon as possible by setting a password for this user via the included mysqladmin
utility, using the following syntax in UNIX:
[root@host]# /usr/local/mysql/bin/mysqladmin -u root password 'new-password'