630 likes | 750 Views
Chapter 12. MySql Administration. Securing a new MySQL Installation. The MySQL installation procedure sets up the server's data directory and populates it with two databases: A mysql database containing the grant tables that control access by clients to the server
E N D
Chapter 12 MySql Administration Mysql Developer's Library, Pual Dubios. 4th Edition
Securing a new MySQL Installation • The MySQL installation procedure sets up the server's data directory and populates it with two databases: • A mysql database containing the grant tables that control access by clients to the server • A test database that can be used for testing purposes Mysql Developer's Library, Pual Dubios. 4th Edition
Securing a new MySQL Installation • The initial installation is not secure, so you should assign passwords to these accounts. Mysql Developer's Library, Pual Dubios. 4th Edition
The book uses examples of running MySQL server on a machine with a hostname of cobra.snake.net and that you will be connecting to the server from that same machine Mysql Developer's Library, Pual Dubios. 4th Edition
Passwords for Initial MySQL Accounts • The grant tables in the mysql database are set up during the MySQL installation procedure with two kinds of accounts. Mysql Developer's Library, Pual Dubios. 4th Edition
Passwords for Initial MySQL Accounts • Accounts that have a username of root. • These are superuser accounts intended for administrative purposes. • The root accounts have all privileges and can do anything, including deleting all your databases and shutting down the server. Mysql Developer's Library, Pual Dubios. 4th Edition
Passwords for Initial MySQL Accounts • Accounts that have a blank username. • These are "anonymous" accounts; • they enable Users to connect to the server without having accounts explicitly set up for them in advance. • Anonymous users usually are given very few privileges, to limit the scope of what they can do. Mysql Developer's Library, Pual Dubios. 4th Edition
User table • Each entry (row) in the user table contains a Host value that indicates • the host from which a user can connect • User • Password values that indicate the name and password the user must give when connecting from that host. Mysql Developer's Library, Pual Dubios. 4th Edition
User table SELECT Host, User, Password FROM mysql.user; Mysql Developer's Library, Pual Dubios. 4th Edition
The Windows user table entries look like those in the following table. Mysql Developer's Library, Pual Dubios. 4th Edition
User table – root access • These user table account entries enable connections by client programs as follows: • You can connect as root from the local host. As root, you have all privileges and can perform any operation. Mysql Developer's Library, Pual Dubios. 4th Edition
User table – anonymous access • You can connect anonymously with no username from the local host. • For current versions of MySQL, this account has no superuser privileges. • Before MySQL 5.0.36/5.1.16, the account has the same superuser privileges as root and can do anything. • Revoke privileges to anonymous account in addition to assigning a password to the account, or perhaps just delete the account entirely. Mysql Developer's Library, Pual Dubios. 4th Edition
Db Table • Another grant table contains privilege information that enables anonymous users to use a particular database on the mysql server. Mysql Developer's Library, Pual Dubios. 4th Edition
password-assignment • Connect to the server as root, determine which accounts have no password • use a SET PASSWORD statement for each one. • SELECT Host, User FROM mysql.user WHERE Password = ''; Mysql Developer's Library, Pual Dubios. 4th Edition
SET Password Statement • mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('rootpass'); • mysql> SET PASSWORD FOR 'root'@'cobra.snake.net' = PASSWORD('rootpass'); • mysql> SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('rootpass'); • mysql> SET PASSWORD FOR ''@'localhost' = PASSWORD('anonpass'); • mysql> SET PASSWORD FOR ''@'cobra.snake.net' = PASSWORD('anonpass'); Mysql Developer's Library, Pual Dubios. 4th Edition
SET PASSWORD – with UPdate • An alternative to SET PASSWORD is to modify the user table directly with UPDATE. • This method can be used to specify a password for all accounts with a given User value, regardless of their Host value, and thus modify multiple accounts simultaneously. • To set the password for all root accounts and all anonymous-user accounts, use these statements. Mysql Developer's Library, Pual Dubios. 4th Edition
mysql> UPDATE mysql.user SET Password=PASSWORD('rootpass') WHERE User='root'; • mysql> UPDATE mysql.user SET Password=PASSWORD('anonpass') WHERE User=''; mysql> FLUSH PRIVILEGES; Mysql Developer's Library, Pual Dubios. 4th Edition
SET PASSWORD • SET PASSWORD • the server notices that you've made a change to the grant tables and automatically re-reads them to refresh its in-memory copy of the tables. • UPDATE • it's necessary to explicitly tell the server to reload the tables. • Use FLUSH PRIVILEGES statement following the UPDATE statements. Mysql Developer's Library, Pual Dubios. 4th Edition
Check privileges • mysql> SHOW GRANTS for ''@'localhost'; Mysql Developer's Library, Pual Dubios. 4th Edition
no superuser privileges, the output will look like this Mysql Developer's Library, Pual Dubios. 4th Edition
account has superuser privileges, you'll see this instead Mysql Developer's Library, Pual Dubios. 4th Edition
revoke the account's privileges • mysql> REVOKE ALL ON *.* FROM ''@'localhost'; mysql> REVOKE GRANT OPTION ON *.* FROM ''@'localhost'; Mysql Developer's Library, Pual Dubios. 4th Edition
Delete a user • mysql> DROP USER ''@'localhost'; • mysql> DROP USER ''@'cobra.snake.net'; Mysql Developer's Library, Pual Dubios. 4th Edition
For REVOKE and DROP USER, the server automatically re-reads the grant tables and no FLUSH PRIVILEGES statement is needed. Mysql Developer's Library, Pual Dubios. 4th Edition
Running the MySQL Server On Windows • Windows servers provide two types of connections Mysql Developer's Library, Pual Dubios. 4th Edition
Running the Server as a Windows Service • On Windows, any MySQL server can be installed as a Windows service using this command: • C:\> C:\mysql\bin\mysqld --install Mysql Developer's Library, Pual Dubios. 4th Edition
Start or Stop Mysql • To start or stop the service from the command line, use the following commands: • C:\> net start MySQL • C:\> net stop MySQL Mysql Developer's Library, Pual Dubios. 4th Edition
Stop Service from Localhost • Use a host value of 127.0.0.1 rather than localhost: • % mysqladmin -p -u root --protocol=tcp shutdown • % mysqladmin -p -u root -h 127.0.0.1 shutdown Mysql Developer's Library, Pual Dubios. 4th Edition
Stop Service from Localhost • 127.0.0.1 is an IP number (it refers to the local host's loopback interface) • it explicitly forces a TCP/IP connection to be used rather than a socket connection. Mysql Developer's Library, Pual Dubios. 4th Edition
Controlling How the Server Listens for Connections • The default port number is 3306 Mysql Developer's Library, Pual Dubios. 4th Edition
Named Pipes • By default the MySQL server will allow connections via TCP/IP from any host (but may reject a connection based on the user's remote hostname/IP address). • In many cases TCP/IP connectivity is not required and can be disabled to prevent remote access to the MySQL server. • If you are using MySQL locally for development or for use with a web server, you should disable TCP/IP networking. Mysql Developer's Library, Pual Dubios. 4th Edition
Named Pipes – Windows Server • Named-pipe connections are disabled by default. • To enable this capability, start the server with the --enable-named-pipe option. • This enables local clients to connect through the named pipe by specifying the --protocol=pipe option or by connecting to the special hostname "." (period). • By default, the pipe name is MySQL (not case sensitive). • To specify a different name, use the --socket option. Mysql Developer's Library, Pual Dubios. 4th Edition
Shared memory • MySQL supports shared-memory connections on Windows, but this capability is disabled by default Mysql Developer's Library, Pual Dubios. 4th Edition
Shared Memory • The following table shows the allowable --protocol option values and indicates the platforms on which each value may be used. The values are not case sensitive. • On Windows, the shared-memory name to use, for connections made via shared memory to a local server. The default value is MYSQL. The shared-memory name is case sensitive. • The server must be started with the --shared-memory option to enable shared-memory connections. Mysql Developer's Library, Pual Dubios. 4th Edition
Bind the TCP/IP Address • In some situations it is not possible to disable TCP/IP networking even when the server will only be needed for requests from localhost. • add the following to the [mysqld] section of your server configuration file: • bind-address=127.0.0.1 • This will cause the MySQL server to respond only to requests from localhost, and ignore all requests from the machine's network interfaces. • http://dev.mysql.com/tech-resources/articles/securing_mysql_windows.html Mysql Developer's Library, Pual Dubios. 4th Edition
Managing MySQL User Accounts • Grant tables in the mysql database • CREATE USER, DROP USER, and RENAME USER create, remove, and rename MySQL accounts. • GRANT specifies account privileges (and creates accounts if they do not exist). • REVOKE removes privileges from existing MySQL accounts. • SET PASSWORD assigns passwords to existing accounts. • SHOW GRANTS displays the privileges held by existing accounts. Mysql Developer's Library, Pual Dubios. 4th Edition
Grant Table Mysql Developer's Library, Pual Dubios. 4th Edition
Grant Table • Manipulate the contents of the grant tables directly by issuing SQL statements like INSERT and UPDATE. • GRANT and REVOKE make it easier to manage user accounts by acting as a front end to the grant tables. Mysql Developer's Library, Pual Dubios. 4th Edition
High-Level MySQL Account Management • CREATE USER creates a new account and optionally assigns a password: CREATE USER account [IDENTIFIED BY 'password']; CREATE USER grants no privileges • DROP USER removes an existing account DROP USER account; • RENAME USER changes the name of an existing account: • RENAME USER from_account TO to_account; Mysql Developer's Library, Pual Dubios. 4th Edition
CREATE USER • asking these questions: • What is the user's name? • From which host or hosts should the user be able to connect? • What is the user's password? Mysql Developer's Library, Pual Dubios. 4th Edition
Specifying Account Names • CREATE USER consists of a username and hostname in 'user_name'@'host_name' format • You specify not only who can connect but from where. • This enables you to set up separate accounts for two users who have the same name but that connect from different locations. • MySQL lets you distinguish between them and assign privileges to each one independent of the other. • The server stores the user_name and host_name values in the User and Host columns of the user table row for the account, and in any other grant table rows associated with the account. Mysql Developer's Library, Pual Dubios. 4th Edition
Root access • There also is nothing special about the name root that is used for the MySQLsuperuser that can do anything. Mysql Developer's Library, Pual Dubios. 4th Edition
User access • At one extreme, you can limit access to a single host if you know users will be connecting only from that host: • CREATE USER 'boris'@'localhost' IDENTIFIED BY 'frost'; • CREATE USER 'fred'@'ares.mars.net' IDENTIFIED BY 'steam'; Mysql Developer's Library, Pual Dubios. 4th Edition
User Access • Enabling a user to connect only from a single host is the strictest form of access you can allow. • At the other extreme, you might have a user who travels a lot and needs to be able to connect from hosts all over the world. • If the user's name is max, you can enable him to connect from anywhere like this: • CREATE USER 'max'@'%' IDENTIFIED BY 'mist'; Mysql Developer's Library, Pual Dubios. 4th Edition
User access • The '%' character functions as a wildcard with the same meaning as in a LIKE pattern match. • a hostname specifier, % means "any host." • This is the easiest way to set up a user, but it's also the least secure. Mysql Developer's Library, Pual Dubios. 4th Edition
User access • To take a middle ground, you can enable a user to connect from a limited set of hosts. • For example, to enable mary to connect from any host in the snake.net domain, • use a host specifier of %.snake.net: • CREATE USER 'mary'@'%.snake.net' IDENTIFIED BY 'fog'; Mysql Developer's Library, Pual Dubios. 4th Edition
User access • The host part of the account value can be given using an IP number rather than a hostname Mysql Developer's Library, Pual Dubios. 4th Edition
User access • CREATE USER 'joe'@'192.168.128.3' IDENTIFIED BY 'water'; • CREATE USER 'ardis'@'192.168.128.%' IDENTIFIED BY 'snow'; • CREATE USER 'rex'@'192.168.128.0/255.255.255.0' IDENTIFIED BY 'ice'; Mysql Developer's Library, Pual Dubios. 4th Edition
User access • The first of the statements indicates a specific single address, 192.168.128.3, from which the user can connect. • The second specifies an IP pattern for the 192.168.128 Class C subnet. • In the third statement, 192.168.128.0/255.255.255.0 specifies a netmask that has the first 24 bits turned on. Mysql Developer's Library, Pual Dubios. 4th Edition
User access • On Windows, the user can connect by specifying a host value of localhost or 127.0.0.1. • Both of these connections are made using TCP/IP, except that if the server supports shared-memory connections, a connection to localhost is made using shared memory by default. Mysql Developer's Library, Pual Dubios. 4th Edition