/  6
 
ySQL DBAhttp://www.schwer.us/nblug/dba/mysql.html1 of 6 6/12/2008 1:41 AM
nblug:augie:dba: mysql
DatabaseAdministration
TheBasics1.
Connecting to and Disconnecting from the Servera.Creating and Using a Databaseb.Creating a Tablec.
ConfiguringMySQL2.
my.cnf a.
Security3.
Set the root passworda.Access Control Listsb.GRANT and REVOKEc.When privileges take effectd.Advanced Topicse.Checking your work f.
User Management4.
Adding Usersa.More on GRANTb.Limiting User Resourcesc.
BackupandRestore5.
mysqldumpa.
Caveats6.
Foreign Keysa.Passwordsb.The BasicsThese are some of the basics of using the MySQL DBMS (Database Management System), the lessons learned here should help you function throughout the rest of this tutorial.
ConnectingtoandDisconnectingfromtheServer
is easy to do. To connect to the server, you'll usually need to provide a MySQL user name when you invoke mysql and, mostlikely, a password. If the server runs on a machine other than the one where you log in, you'll also need to specify a hostname.
shell> mysql -h host -u user -pEnter password: ********
If that works, you should see some introductory information followed by amysql>prompt:
Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 584 to server version: 3.23.47Type 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql>
The prompt tells you that MySQL is ready for you to enter commands. After you have connected successfully, you can disconnect any time by typingQUITat themysql>prompt.
CreatingandUsingaDatabase
. Once you are connected to the server you may create a database like so:
mysql> CREATE DATABASE tutorial;
Creating a database does not select it for use; you must do that explicitly. To make tutorial the current database, use this command:
mysql> USE tutorialDatabase changed
 
ySQL DBAhttp://www.schwer.us/nblug/dba/mysql.html2 of 6 6/12/2008 1:41 AM
Your database needs to be created only once, but you must select it for use each time you begin a mysql session. You can do this by issuing aUSEstatement as shown above.Alternatively, you can select the database on the command-line when you invokemysql. Just specify its name after any connection parameters that you might need to provide. Forexample:
shell> mysql -h host -u user -p tutorialEnter password: ********
CreatingaTable
. After connecting to the server and your chosen database you may create a table like so:
mysql> CREATE TABLE cheese (name VARCHAR(15) NOT NULL,weight INT NOT NULL,PRIMARY KEY(name));
Configuring MySQLThe
my.cnf
file is the configuration file for the MySQL server, and its client applications. The scope and what affect the my.cnf file has depends on where it is placed.
/etc/my.cnf - global optionsDATADIR/my.cnf - server specific options~/.my.cnf - user specified options
DATADIRis specified at compile time. It may be/usr/local/mysql/data, or on my Mandrake 8.2 system it is/var/lib/mysql. MySQL will try to find my.cnf in the order above. If you have multiple my.cnf files then the options from the most recently read my.cnf will take precedence over previousoptions. Options on the command line take precedence over any my.cnf file.The following programs support my.cnf for their configuration:mysql, mysqladmin, mysqld, mysqld_safe, mysql.server, mysqldump, mysqlimport, mysqlshow, mysqlcheck,myisamchk, and myisampack.The basic setup of a my.cnf file is such:# comment- comments begin with a '#' or ';'. [group]- group is the name of the program or group for which you want to set options.optionoption=valueset-variable = variable=valueA good use for theclientgroup is to set your password so you are not prompted everytime you log in (see below). Just make sure the my.cnf in your home directory is onlyreadable by you.[client]password=my_passwordBelow are some examples from the MySQL docs.Here is a typical global option file:[client]port=3306socket=/tmp/mysql.sock [mysqld]port=3306socket=/tmp/mysql.sock set-variable = key_buffer_size=16Mset-variable = max_allowed_packet=1M[mysqldump]quick Here is a typical user option file:[client]# The following password will be sent to all standard MySQL clientspassword=my_password[mysql]no-auto-rehashset-variable = connect_timeout=2[mysqlhotcopy]interactive-timeoutYour distribution of MySQL (source or binary) will come with some sample configuration files. For source you will find them in thesupport-filedirectory. Binary users willtypically find them under/usr/local/mysqlor/usr/share/mysql. They will usually take the form of my-xxxx.cnf wherexxxxis an adjective describing what type of configuration file it is. Currently there are sample configuration files for small, medium, large, and very large systems.
 
ySQL DBAhttp://www.schwer.us/nblug/dba/mysql.html3 of 6 6/12/2008 1:41 AM
Security
Set theroot password
. The default install of MySQL leaves the root password blank. So the first step you take after you install MySQL should be this one:shell> mysql -u root mysqlmysql> UPDATE user SET Password=PASSWORD('new_password') WHERE user='root';mysql> FLUSH PRIVILEGES;
AccessControlLists
. MySQL uses Access Control Lists (ACLs) for all connections, queries, and other operations that a user may attempt to perform. The ACLs are composed of tables which are used to determine privilege.MySQL access control involves two stages:
Stage 1 Connection Verification 
: The server checks whether you are even allowed to connect.
Stage 2 Request Verification 
: Assuming you can connect, the server checks each request you issue to see whether you have sufficient privileges to perform it.The server uses theuser,db, andhosttables in the mysql database at both stages of access control. For the second stage of access control, the server may, if the request involves tables, additionally consult thetables_privandcolumns_privtables. A description of these tables can be seen below:
mysql> DESCRIBEuser;+-----------------+-----------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-----------------+-----------------+------+-----+---------+-------+| Host | char(60) binary | | PRI | | || User | char(16) binary | | PRI | | || Password | char(16) binary | | | | || Select_priv | enum('N','Y') | | | N | || Insert_priv | enum('N','Y') | | | N | || Update_priv | enum('N','Y') | | | N | || Delete_priv | enum('N','Y') | | | N | || Create_priv | enum('N','Y') | | | N | || Drop_priv | enum('N','Y') | | | N | || Reload_priv | enum('N','Y') | | | N | || Shutdown_priv | enum('N','Y') | | | N | || Process_priv | enum('N','Y') | | | N | || File_priv | enum('N','Y') | | | N | || Grant_priv | enum('N','Y') | | | N | || References_priv | enum('N','Y') | | | N | || Index_priv | enum('N','Y') | | | N | || Alter_priv | enum('N','Y') | | | N | |+-----------------+-----------------+------+-----+---------+-------+mysql> DESCRIBEdb;+-----------------+-----------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-----------------+-----------------+------+-----+---------+-------+| Host | char(60) binary | | PRI | | || Db | char(64) binary | | PRI | | || User | char(16) binary | | PRI | | || Select_priv | enum('N','Y') | | | N | || Insert_priv | enum('N','Y') | | | N | || Update_priv | enum('N','Y') | | | N | || Delete_priv | enum('N','Y') | | | N | || Create_priv | enum('N','Y') | | | N | || Drop_priv | enum('N','Y') | | | N | || Grant_priv | enum('N','Y') | | | N | || References_priv | enum('N','Y') | | | N | || Index_priv | enum('N','Y') | | | N | || Alter_priv | enum('N','Y') | | | N | |+-----------------+-----------------+------+-----+---------+-------+mysql> DESCRIBEhost;+-----------------+-----------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-----------------+-----------------+------+-----+---------+-------+| Host | char(60) binary | | PRI | | || Db | char(64) binary | | PRI | | || Select_priv | enum('N','Y') | | | N | || Insert_priv | enum('N','Y') | | | N | || Update_priv | enum('N','Y') | | | N | || Delete_priv | enum('N','Y') | | | N | || Create_priv | enum('N','Y') | | | N | || Drop_priv | enum('N','Y') | | | N | || Grant_priv | enum('N','Y') | | | N | || References_priv | enum('N','Y') | | | N | || Index_priv | enum('N','Y') | | | N | || Alter_priv | enum('N','Y') | | | N | |+-----------------+-----------------+------+-----+---------+-------+mysql> DESCRIBEcolumns_priv;+-------------+----------------------------------------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------------+----------------------------------------------+------+-----+---------+-------+| Host | char(60) binary | | PRI | | || Db | char(64) binary | | PRI | | || User | char(16) binary | | PRI | | || Table_name | char(64) binary | | PRI | | || Column_name | char(64) binary | | PRI | | || Timestamp | timestamp(14) | YES | | NULL | || Column_priv | set('Select','Insert','Update','References') | | | | |+-------------+----------------------------------------------+------+-----+---------+-------+mysql> DESCRIBEtables_priv;+-------------+-----------------------------------------------------------------------------------------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |

Share & Embed

More from this user

Recent Readcasters

Add a Comment

Characters: ...