Table of Contents
This section discusses tasks that you should perform after installing MySQL:
If necessary, initialize the data directory and create the MySQL grant tables. For some MySQL installation methods, data directory initialization may be done for you automatically:
Installation on Windows
Installation on Linux using a server RPM distribution.
Installation using the native packaging system on many platforms, including Debian Linux, Ubuntu Linux, Gentoo Linux, and others.
Installation on OS X using a DMG distribution.
For other platforms and installation types, including installation from generic binary and source distributions, you must initialize the data directory yourself. For instructions, see Section 3.1, “Initializing the Data Directory”.
For instructions, see Section 3.2, “Starting the Server”, and Section 3.3, “Testing the Server”.
Assign passwords to any initial accounts in the grant tables, if that was not already done during data directory initialization. Passwords prevent unauthorized access to the MySQL server. You may also wish to restrict access to test databases. For instructions, see Section 3.4, “Securing the Initial MySQL Accounts”.
Optionally, arrange for the server to start and stop automatically when your system starts and stops. For instructions, see Section 3.5, “Starting and Stopping MySQL Automatically”.
Optionally, populate time zone tables to enable recognition of named time zones. For instructions, see MySQL Server Time Zone Support.
When you are ready to create additional user accounts, you can find information on the MySQL access control system and account management in Chapter 4, The MySQL Access Privilege System, and Chapter 5, MySQL User Account Management.
After installing MySQL, you must initialize the data directory,
including the tables in the mysql system
database. For some MySQL installation methods, data directory
initialization may be done automatically, as described in
Chapter 3, Postinstallation Setup and Testing. For other installation
methods, including installation from generic binary and source
distributions, you must initialize the data directory yourself.
This section describes how to initialize the data directory on Unix and Unix-like systems. (For Windows, see Windows Postinstallation Procedures.) For some suggested commands that you can use to test whether the server is accessible and working properly, see Section 3.3, “Testing the Server”.
In the examples shown here, the server runs under the user ID of
the mysql login account. This assumes that such
an account exists. Either create the account if it does not exist,
or substitute the name of a different existing login account that
you plan to use for running the server. For information about
creating the account, see
Creating a
mysql System User and Group, in
Installing MySQL on Unix/Linux Using Generic Binaries.
Change location into the top-level directory of your MySQL
installation, represented here by
BASEDIR:
shell> cd BASEDIR
BASEDIR is likely to be something
like /usr/local/mysql or
/usr/local. The following steps assume
that you have changed location to this directory.
You will find several files and subdirectories in the
BASEDIR directory. The most
important for installation purposes are the
bin and scripts
subdirectories, which contain the server as well as client and
utility programs.
If necessary, ensure that the distribution contents are
accessible to mysql. If you installed the
distribution as mysql, no further action is
required. If you installed the distribution as
root, its contents will be owned by
root. Change its ownership to
mysql by executing the following commands
as root in the installation directory. The
first command changes the owner attribute of the files to the
mysql user. The second changes the group
attribute to the mysql group.
shell>chown -R mysql .shell>chgrp -R mysql .
If necessary, initialize the data directory, including the
mysql database containing the initial MySQL
grant tables that determine how users are permitted to connect
to the server.
Typically, data directory initialization need be done only the first time you install MySQL. If you are upgrading an existing installation, you should run mysql_upgrade instead (see mysql_upgrade — Check and Upgrade MySQL Tables). However, the command that initializes the data directory does not overwrite any existing privilege tables, so it should be safe to run in any circumstances.
shell> scripts/mysql_install_db --user=mysql
It is important to make sure that the database directories and
files are owned by the mysql login account
so that the server has read and write access to them when you
run it later. To ensure this if you run
mysql_install_db as
root, include the
--user option as
shown. Otherwise, you should execute the program while logged
in as mysql, in which case you can omit the
--user option from
the command.
The mysql_install_db command creates the
server's data directory. Under the data directory, it creates
directories for the mysql database that
holds the grant tables and the test
database that you can use to test MySQL. The program also
creates privilege table entries for the initial account or
accounts. test_. For a complete listing and
description of the grant tables, see
Chapter 4, The MySQL Access Privilege System.
It might be necessary to specify other options such as
--basedir or
--datadir if
mysql_install_db does not identify the
correct locations for the installation directory or data
directory. For example:
shell>scripts/mysql_install_db --user=mysql \--basedir=/opt/mysql/mysql \--datadir=/opt/mysql/mysql/data
If you do not want to have the test
database, you can remove it after starting the server, using
the instructions in Section 3.4, “Securing the Initial MySQL Accounts”.
If you have trouble with mysql_install_db at this point, see Section 3.1.1, “Problems Running mysql_install_db”.
After initializing the data directory, you can establish the
final installation ownership settings. To leave the
installation owned by mysql, no action is
required here. Otherwise, most of the MySQL installation can
be owned by root if you like. The exception
is that the data directory must be owned by
mysql. To accomplish this, run the
following commands as root in the
installation directory. For some distribution types, the data
directory might be named var rather than
data; adjust the second command
accordingly.
shell>chown -R root .shell>chown -R mysql data
If the plugin directory (the directory named by the
plugin_dir system variable)
is writable by the server, it may be possible for a user to
write executable code to a file in the directory using
SELECT ... INTO
DUMPFILE. This can be prevented by making the plugin
directory read only to the server or by setting the
secure_file_priv system variable at server
startup to a directory where
SELECT writes can be performed
safely.
If you installed MySQL using a source distribution, you may
want to optionally copy one of the provided configuration
files from the support-files directory
into your /etc directory. There are
different sample configuration files for different use cases,
server types, and CPU and RAM configurations. To use one of
these standard files, copy it to
/etc/my.cnf, or
/etc/mysql/my.cnf and edit and check the
configuration before starting your MySQL server for the first
time.
You can also create my.cnf yourself and
place into it the options the server should use at startup.
See Server Configuration Defaults.
If you do not copy one of the standard configuration files or create your own, the MySQL server starts with its default settings.
If you want MySQL to start automatically when you boot your machine, see Section 3.5, “Starting and Stopping MySQL Automatically”.
Data directory initialization creates time zone tables in the
mysql database but does not populate them. To
do so, use the instructions in
MySQL Server Time Zone Support.
The purpose of the mysql_install_db program
is to initialize the data directory, including the tables in the
mysql system database. It does not overwrite
existing MySQL privilege tables, and it does not affect any
other data.
To re-create your privilege tables, first stop the
mysqld server if it is running. Then rename
the mysql directory under the data
directory to save it, and run
mysql_install_db. Suppose that your current
directory is the MySQL installation directory and that
mysql_install_db is located in the
bin directory and the data directory is
named data. To rename the
mysql database and re-run
mysql_install_db, use these commands.
shell>mv data/mysql data/mysql.oldshell>scripts/mysql_install_db --user=mysql
When you run mysql_install_db, you might encounter the following problems:
mysql_install_db fails to install the grant tables
You may find that mysql_install_db fails to install the grant tables and terminates after displaying the following messages:
Starting mysqld daemon with databases from XXXXXX mysqld ended
In this case, you should examine the error log file very
carefully. The log should be located in the directory
XXXXXX named by the error message and
should indicate why mysqld did not start.
If you do not understand what happened, include the log when
you post a bug report. See How to Report Bugs or Problems.
There is a mysqld process running
This indicates that the server is running, in which case the grant tables have probably been created already. If so, there is no need to run mysql_install_db at all because it needs to be run only once, when you first install MySQL.
Installing a second mysqld server does not work when one server is running
This can happen when you have an existing MySQL installation, but want to put a new installation in a different location. For example, you might have a production installation, but you want to create a second installation for testing purposes. Generally the problem that occurs when you try to run a second server is that it tries to use a network interface that is in use by the first server. In this case, you should see one of the following error messages:
Can't start server: Bind on TCP/IP port: Address already in use Can't start server: Bind on unix socket...
For instructions on setting up multiple servers, see Running Multiple MySQL Instances on One Machine.
You do not have write access to the
/tmp directory
If you do not have write access to create temporary files or
a Unix socket file in the default location (the
/tmp directory) or the
TMPDIR environment variable, if it has
been set, an error occurs when you run
mysql_install_db or the
mysqld server.
You can specify different locations for the temporary
directory and Unix socket file by executing these commands
prior to starting mysql_install_db or
mysqld, where
some_tmp_dir is the full path
name to some directory for which you have write permission:
shell>TMPDIR=/shell>some_tmp_dir/MYSQL_UNIX_PORT=/shell>some_tmp_dir/mysql.sockexport TMPDIR MYSQL_UNIX_PORT
Then you should be able to run mysql_install_db and start the server with these commands:
shell>scripts/mysql_install_db --user=mysqlshell>bin/mysqld_safe --user=mysql &
If mysql_install_db is located in the
scripts directory, modify the first
command to scripts/mysql_install_db.
See How to Protect or Change the MySQL Unix Socket File, and MySQL Program Environment Variables.
There are some alternatives to running the mysql_install_db program provided in the MySQL distribution:
If you want the initial privileges to be different from the
standard defaults, use account-management statements such as
CREATE USER,
GRANT, and
REVOKE to change the
privileges after the grant tables have
been set up. In other words, run
mysql_install_db, and then use
mysql -u root mysql to connect to the
server as the MySQL root user so that you
can issue the necessary statements. (See
Account Management Statements.)
To install MySQL on several machines with the same
privileges, put the CREATE
USER, GRANT, and
REVOKE statements in a file
and execute the file as a script using
mysql after running
mysql_install_db. For example:
shell>scripts/mysql_install_db --user=mysqlshell>bin/mysql -u root < your_script_file
This enables you to avoid issuing the statements manually on each machine.
It is possible to re-create the grant tables completely
after they have previously been created. You might want to
do this if you are just learning how to use
CREATE USER,
GRANT, and
REVOKE and have made so many
modifications after running
mysql_install_db that you want to wipe
out the tables and start over.
To re-create the grant tables, stop the server if it is
running and remove the mysql database
directory. Then run mysql_install_db
again.
This section describes how start the server on Unix and Unix-like systems. (For Windows, see Starting the Server for the First Time.) For some suggested commands that you can use to test whether the server is accessible and working properly, see Section 3.3, “Testing the Server”.
Start the MySQL server like this:
shell> bin/mysqld_safe --user=mysql &
It is important that the MySQL server be run using an unprivileged
(non-root) login account. To ensure this if you
run mysqld_safe as root,
include the --user option as
shown. Otherwise, execute the program while logged in as
mysql, in which case you can omit the
--user option from the
command.
For further instructions for running MySQL as an unprivileged user, see Section 2.5, “How to Run MySQL as a Normal User”.
If the command fails immediately and prints mysqld
ended, look for information in the error log (which by
default is the
file
in the data directory).
host_name.err
If the server is unable to access the data directory it starts or
read the grant tables in the mysql database, it
writes a message to its error log. Such problems can occur if you
neglected to create the grant tables by initializing the data
directory before proceeding to this step, or if you ran the
command that initializes the data directory without the
--user option. Remove the
data directory and run the command with the
--user option.
If you have other problems starting the server, see Section 3.2.1, “Troubleshooting Problems Starting the MySQL Server”. For more information about mysqld_safe, see mysqld_safe — MySQL Server Startup Script.
You can set up new accounts using the
bin/mysql_setpermission script if you install
the DBI and DBD::mysql Perl
modules. See mysql_setpermission — Interactively Set Permissions in Grant Tables. For Perl
module installation instructions, see
Perl Installation Notes.
If you would like to use mysqlaccess and have
the MySQL distribution in some nonstandard location, you must
change the location where mysqlaccess expects
to find the mysql client. Edit the
bin/mysqlaccess script at approximately line
18. Search for a line that looks like this:
$MYSQL = '/usr/local/bin/mysql'; # path to mysql executable
Change the path to reflect the location where
mysql actually is stored on your system. If you
do not do this, a Broken pipe error will occur
when you run mysqlaccess.
This section provides troubleshooting suggestions for problems starting the server. For additional suggestions for Windows systems, see Troubleshooting a Microsoft Windows MySQL Server Installation.
If you have problems starting the server, here are some things to try:
Check the error log to see why the server does not start.
Specify any special options needed by the storage engines you are using.
Make sure that the server knows where to find the data directory.
Make sure that the server can access the data directory. The ownership and permissions of the data directory and its contents must be set such that the server can read and modify them.
Verify that the network interfaces the server wants to use are available.
Some storage engines have options that control their behavior.
You can create a my.cnf file and specify
startup options for the engines that you plan to use. If you are
going to use storage engines that support transactional tables
(InnoDB, NDB),
be sure that you have them configured the way you want before
starting the server:
If you are using InnoDB tables, see
InnoDB Configuration.
Storage engines will use default option values if you specify none, but it is recommended that you review the available options and specify explicit values for those for which the defaults are not appropriate for your installation.
When the mysqld server starts, it changes location to the data directory. This is where it expects to find databases and where it expects to write log files. The server also writes the pid (process ID) file in the data directory.
The data directory location is hardwired in when the server is
compiled. This is where the server looks for the data directory
by default. If the data directory is located somewhere else on
your system, the server will not work properly. You can
determine what the default path settings are by invoking
mysqld with the
--verbose and
--help options.
If the default locations do not match the MySQL installation layout on your system, you can override them by specifying options to mysqld or mysqld_safe on the command line or in an option file.
To specify the location of the data directory explicitly, use
the --datadir option. However,
normally you can tell mysqld the location of
the base directory under which MySQL is installed and it looks
for the data directory there. You can do this with the
--basedir option.
To check the effect of specifying path options, invoke
mysqld with those options followed by the
--verbose and
--help options. For example, if
you change location into the directory where
mysqld is installed and then run the
following command, it shows the effect of starting the server
with a base directory of /usr/local:
shell> ./mysqld --basedir=/usr/local --verbose --help
You can specify other options such as
--datadir as well, but
--verbose and
--help must be the last options.
Once you determine the path settings you want, start the server
without --verbose and
--help.
If mysqld is currently running, you can find out what path settings it is using by executing this command:
shell> mysqladmin variables
Or:
shell> mysqladmin -h host_name variables
host_name is the name of the MySQL
server host.
If you get Errcode 13 (which means
Permission denied) when starting
mysqld, this means that the privileges of the
data directory or its contents do not permit server access. In
this case, you change the permissions for the involved files and
directories so that the server has the right to use them. You
can also start the server as root, but this
raises security issues and should be avoided.
Change location into the data directory and check the ownership
of the data directory and its contents to make sure the server
has access. For example, if the data directory is
/usr/local/mysql/var, use this command:
shell> ls -la /usr/local/mysql/var
If the data directory or its files or subdirectories are not
owned by the login account that you use for running the server,
change their ownership to that account. If the account is named
mysql, use these commands:
shell>chown -R mysql /usr/local/mysql/varshell>chgrp -R mysql /usr/local/mysql/var
Even with correct ownership, MySQL might fail to start up if there is other security software running on your system that manages application access to various parts of the file system. In this case, reconfigure that software to enable mysqld to access the directories it uses during normal operation.
If the server fails to start up correctly, check the error log.
Log files are located in the data directory (typically
C:\Program Files\MySQL\MySQL Server
5.5\data on Windows,
/usr/local/mysql/data for a Unix/Linux
binary distribution, and /usr/local/var for
a Unix/Linux source distribution). Look in the data directory
for files with names of the form
and
host_name.err,
where host_name.loghost_name is the name of your
server host. Then examine the last few lines of these files. You
can use tail to display them:
shell>tailshell>host_name.errtailhost_name.log
The error log should contain information that indicates why the server could not start.
If either of the following errors occur, it means that some other program (perhaps another mysqld server) is using the TCP/IP port or Unix socket file that mysqld is trying to use:
Can't start server: Bind on TCP/IP port: Address already in use Can't start server: Bind on unix socket...
Use ps to determine whether you have another mysqld server running. If so, shut down the server before starting mysqld again. (If another server is running, and you really want to run multiple servers, you can find information about how to do so in Running Multiple MySQL Instances on One Machine.)
If no other server is running, try to execute the command
telnet . (The
default MySQL port number is 3306.) Then press Enter a couple of
times. If you do not get an error message like your_host_name
tcp_ip_port_numbertelnet:
Unable to connect to remote host: Connection refused,
some other program is using the TCP/IP port that
mysqld is trying to use. You will need to
track down what program this is and disable it, or else tell
mysqld to listen to a different port with the
--port option. In this case, you
will also need to specify the port number for client programs
when connecting to the server using TCP/IP.
Another reason the port might be inaccessible is that you have a firewall running that blocks connections to it. If so, modify the firewall settings to permit access to the port.
If the server starts but you cannot connect to it, you should
make sure that you have an entry in
/etc/hosts that looks like this:
127.0.0.1 localhost
If you cannot get mysqld to start, you can
try to make a trace file to find the problem by using the
--debug option. See
The DBUG Package.
After the data directory is initialized and you have started the
server, perform some simple tests to make sure that it works
satisfactorily. This section assumes that your current location is
the MySQL installation directory and that it has a
bin subdirectory containing the MySQL
programs used here. If that is not true, adjust the command path
names accordingly.
Alternatively, add the bin directory to your
PATH environment variable setting. That enables
your shell (command interpreter) to find MySQL programs properly,
so that you can run a program by typing only its name, not its
path name. See Setting Environment Variables.
Use mysqladmin to verify that the server is running. The following commands provide simple tests to check whether the server is up and responding to connections:
shell>bin/mysqladmin versionshell>bin/mysqladmin variables
If you cannot connect to the server, specify a -u
root option to connect as root. If you
have assigned a password for the root account
already, you'll also need to specify -p on the
command line and enter the password when prompted. For example:
shell>bin/mysqladmin -u root -p versionEnter password:(enter root password here)
The output from mysqladmin version varies slightly depending on your platform and version of MySQL, but should be similar to that shown here:
shell> bin/mysqladmin version
mysqladmin Ver 14.12 Distrib 5.5.55, for pc-linux-gnu on i686
...
Server version 5.5.55
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 14 days 5 hours 5 min 21 sec
Threads: 1 Questions: 366 Slow queries: 0
Opens: 0 Flush tables: 1 Open tables: 19
Queries per second avg: 0.000
To see what else you can do with mysqladmin,
invoke it with the --help
option.
Verify that you can shut down the server (include a
-p option if the root account
has a password already):
shell> bin/mysqladmin -u root shutdown
Verify that you can start the server again. Do this by using mysqld_safe or by invoking mysqld directly. For example:
shell> bin/mysqld_safe --user=mysql &
If mysqld_safe fails, see Section 3.2.1, “Troubleshooting Problems Starting the MySQL Server”.
Run some simple tests to verify that you can retrieve information from the server. The output should be similar to that shown here.
Use mysqlshow to see what databases exist:
shell> bin/mysqlshow
+--------------------+
| Databases |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
The list of installed databases may vary, but will always include
the minimum of mysql and
information_schema.
If you specify a database name, mysqlshow displays a list of the tables within the database:
shell> bin/mysqlshow mysql
Database: mysql
+---------------------------+
| Tables |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
Use the mysql program to select information
from a table in the mysql database:
shell> bin/mysql -e "SELECT User, Host, plugin FROM mysql.user" mysql
+------+-----------+-----------------------+
| User | Host | plugin |
+------+-----------+-----------------------+
| root | localhost | mysql_native_password |
+------+-----------+-----------------------+
At this point, your server is running and you can access it. To tighten security if you have not yet assigned passwords to the initial account or accounts, follow the instructions in Section 3.4, “Securing the Initial MySQL Accounts”.
For more information about mysql, mysqladmin, and mysqlshow, see mysql — The MySQL Command-Line Tool, mysqladmin — Client for Administering a MySQL Server, and mysqlshow — Display Database, Table, and Column Information.
The MySQL installation process involves initializing the data
directory, including the mysql database
containing the grant tables that define MySQL accounts. For
details, see Chapter 3, Postinstallation Setup and Testing.
This section describes how to assign passwords to the initial accounts created during the MySQL installation procedure, if you have not already done so.
The mysql.user grant table defines the initial
MySQL user accounts and their access privileges:
Some accounts have the user name root.
These are superuser accounts that have all privileges and can
do anything. If these root accounts have
empty passwords, anyone can connect to the MySQL server as
root without a
password and be granted all privileges.
On Windows, root accounts are created
that permit connections from the local host only.
Connections can be made by specifying the host name
localhost, the IP address
127.0.0.1, or the IPv6 address
::1. If the user selects the
Enable root access from remote
machines option during installation, the
Windows installer creates another root
account that permits connections from any host.
On Unix, each root account permits
connections from the local host. Connections can be made
by specifying the host name localhost,
the IP address 127.0.0.1, the IPv6
address ::1, or the actual host name or
IP address.
An attempt to connect to the host 127.0.0.1
normally resolves to the localhost account.
However, this fails if the server is run with the
--skip-name-resolve option, so
the 127.0.0.1 account is useful in that
case. The ::1 account is used for IPv6
connections.
If accounts for anonymous users were created, these have an empty user name. The anonymous accounts have no password, so anyone can use them to connect to the MySQL server.
On Windows, there is one anonymous account that permits
connections from the local host. Connections can be made
by specifying a host name of localhost.
On Unix, each anonymous account permits connections from
the local host. Connections can be made by specifying a
host name of localhost for one of the
accounts, or the actual host name or IP address for the
other.
The 'root'@'localhost' account also has
a row in the mysql.proxies_priv table
that enables granting the
PROXY privilege for
''@'', that is, for all users and all
hosts. This enables root to set up
proxy users, as well as to delegate to other accounts the
authority to set up proxy users. See
Section 5.7, “Proxy Users”.
To display which accounts exist in the
mysql.user table and check whether their
passwords are empty, use the following statement:
mysql> SELECT User, Host, Password FROM mysql.user;
+------+--------------------+----------+
| User | Host | Password |
+------+--------------------+----------+
| root | localhost | |
| root | myhost.example.com | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | myhost.example.com | |
+------+--------------------+----------+
This output indicates that there are several
root and anonymous-user accounts, none of which
have passwords. The output might differ on your system, but the
presence of accounts with empty passwords means that your MySQL
installation is unprotected until you do something about it:
Assign a password to each MySQL root
account that does not have one.
To prevent clients from connecting as anonymous users without a password, either assign a password to each anonymous account or remove the accounts.
In addition, the mysql.db table contains rows
that permit all accounts to access the test
database and other databases with names that start with
test_. This is true even for accounts that
otherwise have no special privileges such as the default anonymous
accounts. This is convenient for testing but inadvisable on
production servers. Administrators who want database access
restricted only to accounts that have permissions granted
explicitly for that purpose should remove these
mysql.db table rows.
The following instructions describe how to set up passwords for
the initial MySQL accounts, first for the root
accounts, then for the anonymous accounts. The instructions also
cover how to remove anonymous accounts, should you prefer not to
permit anonymous access at all, and describe how to remove
permissive access to test databases. Replace
new_password in the examples with the
password that you want to use. Replace
host_name with the name of the server
host. You can determine this name from the output of the preceding
SELECT statement. For the output
shown, host_name is
myhost.example.com.
For additional information about setting passwords, see
Section 5.5, “Assigning Account Passwords”. If you forget your
root password after setting it, see
How to Reset the Root Password.
To set up additional accounts, see Section 5.2, “Adding User Accounts”.
You might want to defer setting the passwords until later, to avoid the need to specify them while you perform additional setup or testing. However, be sure to set them before using your installation for production purposes.
On Windows, you can also perform the process described in this section using the Configuration Wizard (see The Security Options Dialog). On all platforms, the MySQL distribution includes mysql_secure_installation, a command-line utility that automates much of the process of securing a MySQL installation. MySQL Workbench is available on all platforms, and also offers the ability to manage user accounts (see MySQL Workbench ).
A root account password can be set several
ways. The following discussion demonstrates three methods:
Use the SET PASSWORD statement
Use the UPDATE statement
Use the mysqladmin command-line client program
To assign passwords using SET
PASSWORD, connect to the server as
root and issue a SET
PASSWORD statement for each root
account listed in the mysql.user table.
For Windows, do this:
shell>mysql -u rootmysql>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mysql>new_password');SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('mysql>new_password');SET PASSWORD FOR 'root'@'::1' = PASSWORD('mysql>new_password');SET PASSWORD FOR 'root'@'%' = PASSWORD('new_password');
The last statement is unnecessary if the
mysql.user table has no root
account with a host value of %.
For Unix, do this:
shell>mysql -u rootmysql>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mysql>new_password');SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('mysql>new_password');SET PASSWORD FOR 'root'@'::1' = PASSWORD('mysql>new_password');SET PASSWORD FOR 'root'@'host_name' = PASSWORD('new_password');
You can also use a single statement that assigns a password to all
root accounts by using
UPDATE to modify the
mysql.user table directly. This method works on
any platform:
shell>mysql -u rootmysql>UPDATE mysql.user SET Password = PASSWORD('->new_password')WHERE User = 'root';mysql>FLUSH PRIVILEGES;
The FLUSH statement causes the
server to reread the grant tables. Without it, the password change
remains unnoticed by the server until you restart it.
To assign passwords to the root accounts using
mysqladmin, execute the following commands:
shell>mysqladmin -u root password "shell>new_password"mysqladmin -u root -hhost_namepassword "new_password"
Those commands apply both to Windows and to Unix. The double quotation marks around the password are not always necessary, but you should use them if the password contains spaces or other characters that are special to your command interpreter.
The mysqladmin method of setting the
root account passwords does not work for the
'root'@'127.0.0.1' or
'root'@'::1' account. Use the
SET PASSWORD method shown earlier.
After the root passwords have been set, you
must supply the appropriate password whenever you connect as
root to the server. For example, to shut down
the server with mysqladmin, use this command:
shell>mysqladmin -u root -p shutdownEnter password:(enter root password here)
The mysql commands in the following
instructions include a -p option based on the
assumption that you have assigned the root
account passwords using the preceding instructions and must
specify that password when connecting to the server.
To assign passwords to the anonymous accounts, connect to the
server as root, then use either
SET PASSWORD or
UPDATE.
To use SET PASSWORD on Windows, do
this:
shell>mysql -u root -pEnter password:(enter root password here)mysql>SET PASSWORD FOR ''@'localhost' = PASSWORD('new_password');
To use SET PASSWORD on Unix, do
this:
shell>mysql -u root -pEnter password:(enter root password here)mysql>SET PASSWORD FOR ''@'localhost' = PASSWORD('mysql>new_password');SET PASSWORD FOR ''@'host_name' = PASSWORD('new_password');
To set the anonymous-user account passwords with a single
UPDATE statement, do this (on any
platform):
shell>mysql -u root -pEnter password:(enter root password here)mysql>UPDATE mysql.user SET Password = PASSWORD('->new_password')WHERE User = '';mysql>FLUSH PRIVILEGES;
The FLUSH statement causes the
server to reread the grant tables. Without it, the password change
remains unnoticed by the server until you restart it.
If you prefer to remove any anonymous accounts rather than assigning them passwords, do so as follows on Windows:
shell>mysql -u root -pEnter password:(enter root password here)mysql>DROP USER ''@'localhost';
On Unix, remove the anonymous accounts like this:
shell>mysql -u root -pEnter password:(enter root password here)mysql>DROP USER ''@'localhost';mysql>DROP USER ''@'host_name';
By default, the mysql.db table contains rows
that permit access by any user to the test
database and other databases with names that start with
test_. (These rows have an empty
User column value, which for access-checking
purposes matches any user name.) This means that such databases
can be used even by accounts that otherwise possess no privileges.
If you want to remove any-user access to test databases, do so as
follows:
shell>mysql -u root -pEnter password:(enter root password here)mysql>DELETE FROM mysql.db WHERE Db LIKE 'test%';mysql>FLUSH PRIVILEGES;
The FLUSH statement causes the
server to reread the grant tables. Without it, the privilege
change remains unnoticed by the server until you restart it.
With the preceding change, only users who have global database
privileges or privileges granted explicitly for the
test database can use it. However, if you
prefer that the database not exist at all, drop it:
mysql> DROP DATABASE test;
This section discusses methods for starting and stopping the MySQL server.
Generally, you start the mysqld server in one of these ways:
Invoke mysqld directly. This works on any platform.
On Windows, you can set up a MySQL service that runs automatically when Windows starts. See Starting MySQL as a Windows Service.
On Unix and Unix-like systems, you can invoke mysqld_safe, which tries to determine the proper options for mysqld and then runs it with those options. See mysqld_safe — MySQL Server Startup Script.
On systems that use System V-style run directories (that is,
/etc/init.d and run-level specific
directories), invoke mysql.server. This
script is used primarily at system startup and shutdown. It
usually is installed under the name mysql.
The mysql.server script starts the server
by invoking mysqld_safe. See
mysql.server — MySQL Server Startup Script.
On OS X, install a launchd daemon to enable automatic MySQL startup at system startup. The daemon starts the server by invoking mysqld_safe. For details, see Installing a MySQL Launch Daemon. A MySQL Preference Pane also provides control for starting and stopping MySQL through the System Preferences. See Installing and Using the MySQL Preference Pane.
On Solaris/OpenSolaris, use the service management framework (SMF) system to initiate and control MySQL startup. For more information, see Installing MySQL on OpenSolaris Using IPS.
The mysqld_safe and mysql.server scripts, Solaris/OpenSolaris SMF, and the OS X Startup Item (or MySQL Preference Pane) can be used to start the server manually, or automatically at system startup time. mysql.server and the Startup Item also can be used to stop the server.
The following table shows which option groups the server and startup scripts read from option files.
Table 3.1 MySQL Startup Scripts and Supported Server Option Groups
| Script | Option Groups |
|---|---|
| mysqld | [mysqld], [server],
[mysqld- |
| mysqld_safe | [mysqld], [server],
[mysqld_safe] |
| mysql.server | [mysqld], [mysql.server],
[server] |
[mysqld-
means that groups with names like
major_version][mysqld-5.1] and
[mysqld-5.5] are read by servers
having versions 5.1.x, 5.5.x, and so
forth. This feature can be used to specify options that can be
read only by servers within a given release series.
For backward compatibility, mysql.server also
reads the [mysql_server] group and
mysqld_safe also reads the
[safe_mysqld] group. However, you should update
your option files to use the [mysql.server] and
[mysqld_safe] groups instead.
For more information on MySQL configuration files and their structure and contents, see Using Option Files.