A tutorial on installing and configuring MySQL in Linux

Source: Internet
Author: User

The Linux environment in this article is Red Hat 9.0,mysql is 4.0.16.
Second, the installation of MySQL
1, download the MySQL installation files
The following two files are required to install MySQL:
mysql-server-4.0.16-0.i386.rpm
mysql-client-4.0.16-0.i386.rpm
Download Address: http://www.mysql.com/downloads/mysql-4.0.html, open this page, Drop-down page to find "Linux x86 RPM

Downloads ", locate the" Server "and" Client programs "items
, download the required two RPM files.
2, install MySQL
The rpm file is a software installation package developed by Red Hat, which allows Linux to remove many complex procedures when installing software packages. This command is installed at

The commonly used parameter is –IVH, where I indicates that the specified RMP package will be installed, V
Indicates the details of the installation, h indicates that the "#" symbol appears during installation to display the current installation process. This symbol will continue until the installation is complete.

Check.

1) Install server side
Run the following command in a directory with two RMP files:
[Root@test1 local]# RPM-IVH mysql-server-4.0.16-0.i386.rpm
Displays the following information.
Warning:mysql-server-4.0.16-0.i386.rpm:v3 DSA Signature:nokey, key ID 5072e1f5
Preparing ... ########################################### [100%]
1:mysql-server ########################################### [100%]
。。。。。。 (Omitted display)
/usr/bin/mysqladmin-u root password ' new-password '
/usr/bin/mysqladmin-u root-h test1 password ' new-password '
。。。。。。 (Omitted display)
Starting mysqld daemon with databases From/var/lib/mysql
If appear as above information, the service side installs completes. Test whether the success can be run netstat to see if the MySQL port is open, for example, open indicates that the service has been restarted

Move, install successfully. The default port for MySQL is 3306.

[Root@test1 local]# Netstat-nat
Active Internet connections (servers and established)
Proto recv-q send-q Local address Foreign
TCP 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
The above shows that the MySQL service has been started.
2) Install the client
Run the following command:
[Root@test1 local]# RPM-IVH mysql-client-4.0.16-0.i386.rpm
Warning:mysql-client-4.0.16-0.i386.rpm:v3 DSA Signature:nokey, key ID 5072e1f5
Preparing ... ########################################### [100%]
1:mysql-client ########################################### [100%]
Show installation complete.
Connect MySQL with the following command to test for success.

Third, login MySQL
The command to log in to MySQL is MySQL, and the usage syntax for MySQL is as follows:
MySQL [-u username] [H-host] [-p[password]] [dbname]
Username and password are MySQL username and password respectively, MySQL's initial management account is root, no password, note: this

The root user is not a Linux system user. The MySQL default user is root, because
No password Initially, just type MySQL the first time you enter.
[Root@test1 local]# MySQL
Welcome to the MySQL Monitor. Commands End With; Or\g.
Your MySQL Connection ID is 1 to server Version:4.0.16-standard
Type ' help, ' or ' \h ' for help. Type ' \c ' to clear the buffer.
Mysql>
There is a "mysql>" prompt, congratulations, installation success!
The login format with the password added is as follows:
Mysql-u root-p
Enter Password: (enter password)
Where-U is followed by the username,-p requires a password, enter the password at the input password.
Note: This MySQL file is in the/usr/bin directory and is not a file/etc/init.d/mysql the startup file that follows.
Four, several important directories of MySQL
After MySQL installation is complete unlike SQL Server default installed in a directory, its database files, configuration files and command files are in different eyes

, it's very important to understand these directories, especially for Linux beginners, because
Linux's own directory structure is more complex, if not clear MySQL installation directory that can not talk about in-depth study.
Here are some of these directories.
1. Database Directory
/var/lib/mysql/
2. Configuration file
/usr/share/mysql (mysql.server command and configuration file)
3. Related Orders
/usr/bin (mysqladmin mysqldump order)
4. Startup script
/etc/rc.d/init.d/(Directory of startup script files MySQL)
V. Modify the login password
MySQL defaults to no password, the importance of installing the password added is self-evident.
1. Order
usr/bin/mysqladmin-u root password ' new-password '
Format: Mysqladmin-u username-P Old password password new password
2. Examples
Example 1: Add a password of 123456 to root.
Type the following command:
[Root@test1 local]#/usr/bin/mysqladmin-u root password 123456
Note: Since Root does not have a password at the beginning, the-p old password can be omitted.
3. Test whether the modification is successful
1 Login without password
[Root@test1 local]# MySQL
ERROR 1045:access denied for user: ' Root@localhost ' (Using password:no)
An error is displayed, indicating that the password has been modified.
2 Login with modified password
[Root@test1 local]# mysql-u root-p
Enter Password: (enter modified password 123456)
Welcome to the MySQL Monitor. Commands End With; Or\g.
Your MySQL Connection ID is 4 to server Version:4.0.16-standard
Type ' help, ' or ' \h ' for help. Type ' \c ' to clear the buffer.
Mysql>
Success!
This is through the mysqladmin command to modify the password, or you can change the password by modifying the library.

Vi. Initiation and cessation
1, start
MySQL installation is completed after the boot file MySQL in the/ETC/INIT.D directory, when you need to start running the following command.
[Root@test1 init.d]#/etc/init.d/mysql start
2. Stop
/usr/bin/mysqladmin-u root-p shutdown
3, automatic start
1 See if MySQL is in the auto start list
[Root@test1 local]#/sbin/chkconfig–list
2 Add MySQL to your system's startup service group
[Root@test1 local]#/sbin/chkconfig–add MySQL
3 Remove MySQL from the Startup service group.
[Root@test1 local]#/sbin/chkconfig–del MySQL

Vii. change the MySQL directory

MySQL defaults to the data file storage directory for/var/lib/mysql. The following steps are required if you want to move the directory to/home/data:
1, the home directory to establish the data directory
Cd/home
mkdir data
2, the MySQL service process to stop:
Mysqladmin-u root-p shutdown
3. Move/var/lib/mysql Entire directory to/home/data
mv/var/lib/mysql/home/data/
This will be the MySQL data file moved to the/home/data/mysql
4, find my.cnf configuration file
If there is no MY.CNF configuration file in the/etc/directory, please find *.cnf file under/usr/share/mysql/, copy one to/etc/and rename it

As MY.CNF). The order is as follows:
[Root@test1 mysql]# cp/usr/share/mysql/my-medium.cnf/etc/my.cnf
5, edit the MySQL configuration file/etc/my.cnf
To ensure that MySQL works properly, you need to indicate where the Mysql.sock file will be generated. Modify Socket=/var/lib/mysql/mysql.sock One

The value on the right side of the equal sign in the line is:/home/mysql/mysql.sock. Operation is as follows

VI my.cnf (with VI tool to edit MY.CNF file, find the following data modification)
# The MySQL server
[Mysqld]
Port = 3306
#socket =/var/lib/mysql/mysql.sock (original content, in order to be more secure with "#" comment on this row)
Socket =/home/data/mysql/mysql.sock (plus this row)
6, modify the MySQL startup script/etc/rc.d/init.d/mysql
Finally, you need to modify the MySQL startup script/etc/rc.d/init.d/mysql, put it in the Datadir=/var/lib/mysql line, the equal sign to the right

The path is changed to your current actual storage path: Home/data/mysql.
[Root@test1 etc]# Vi/etc/rc.d/init.d/mysql
#datadir =/var/lib/mysql (note this row)
Datadir=/home/data/mysql (plus this row)
7, restart the MySQL service
/etc/rc.d/init.d/mysql start
or restart Linux with the reboot command
Increase MySQL Users
Format: Grant Select on database. * To User name @ login host identified by "password"
Example 1, add a user user_1 password is 123, so that he can log on any host, and all databases have query, insert, modify, delete the right

Limit. First connect the root user to MySQL, and then type the following command:
Mysql> Grant Select,insert,update,delete on *.* to user_1@ "% identified by" 123″;
Example 1 The added user is very dangerous, and if you know the user_1 password, then he can log on to your MySQL data on any computer on the Internet.

Library and do whatever you have to do with your data, see Example 2 for solutions.
Example 2, add a user user_2 password is 123, so that this user can only log on localhost, and the database can query, insert AAA,

Modify, delete operation (localhost refers to the local host, that is, the MySQL database
In the host, so that users know the user_2 password, he can not access the database directly from the Internet, only through the MySQL host to operate

AAA Library.

Mysql>grant select,insert,update,delete on aaa.* to User_2@localhost identified by "123″;

With the new user if you can't log in to MySQL, use the following command when you log in:

Mysql-u user_1-p-H 192.168.113.50 (-H followed by the IP address of the host to be logged in)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.