1. Mysql Installation Method
Mysql is a platform-independent open-source relational database. you can install and deploy the Mysql database in rpm mode, which is also the simplest. However, for the rpm version, the functions of the Mysql database are fixed and the version is not the latest. If you want to install the latest Mysql database, you can compile it in binary or source code, the following three methods are briefly compared:
Installation Method
|
Advantages
|
Disadvantages
|
Rpm
|
Easy to install, query, uninstall, and configure
|
The version is not the latest and the function is fixed.
|
Binary
|
Fast Installation and version update
|
Configuration is troublesome and can only be performed on specific platforms
|
Source code
|
The most flexible installation and configuration, custom functions
|
Compilation takes a long time and configuration is difficult
|
2. install and configure a binary Mysql database
Before installing the binary Mysql database, check whether the rpm version of the software package is installed in the system. You can use rpm-q mysql-server mysql for query. If you have already installed the software, delete it first, you can use rpm-e mysql-server mysql or yum-y remove mysql-server mysql to delete the data.
1. To obtain the binary Mysql database package, go to the official website:
[Root @ station254 ~] # Switch cd/usr/local/src/to the directory where the source code package is stored
|
[Root @ station254 src] # download wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz
|
2. decompress, configure, and copy data to the mysql working directory.
Decompress and enter the Directory: x indicates decompression, v indicates detailed process, z calls gzip to decompress, f indicates a common file
[Root @ station254 src] # tar-xvzf mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz unpackage and decompress
|
[Root @ station254 src] # Enter the directory where the cd mysql-5.6.14-linux-glibc2.5-x86_64 is extracted
|
The binary source code package has been configured, compiled, and installed. You can directly use it to install it in the/usr/local/mysql directory.
[Root @ station254 mysql-5.6.14-linux-glibc2.5-x86_64] # mkdir/usr/local/mysql CREATE directory [Root @ station254 mysql-5.6.14-linux-glibc2.5-x86_64] # cp-R */usr/local/mysql/copy
[Root @ station254 mysql] # chown-R root: root/usr/local/mysql/modify permissions to ensure that the root user has control permissions |
3. initialize the database. The binary mysql database is initialized to the/usr/local/mysql/data directory by default.
[Root @ station254 ~] # Cd/usr/local/mysql/ [Root @ station254 mysql] #./scripts/mysql_install_db -- user = mysql -- datadir =/usr/local/mysql/data/perform database Initialization
|
Note: The mysql_install_db command must be executed in the mysql main directory. Otherwise, the command cannot be executed successfully. After the command is executed successfully, two default databases will be created in the/usr/local/mysql/data directory: mysql and test databases. The former is used to store mysql-related database information, and the latter is an empty database without any tables!
4. Create a mysql configuration file, which can be stored in/etc/my. cnf or/usr/local/mysql/my. cnf. The latter is used in this article.
[Root @ station254 ~] # Vim/usr/local/mysql/my. cnf: [Mysqld] server-side container configuration Datadir =/usr/local/mysql/data Directory Port = 3306 listening port number. The default port number is TCP port 3306. User = mysql is started as a mysql user Error-log =/usr/local/mysql/data/mysqld. log error log storage path Pid-file =/usr/local/mysql/mysqld. pid file storage path Socket =/usr/local/mysql/data/mysqld. sock socket file path
[Client] configure containers on the client Port = 3306 Socket =/usr/local/mysql/data/mysqld. sock
|
5. Copy the startup script to the/etc/init. d/directory.
[Root @ station254 ~] # Cp/usr/local/mysql/support-files/mysql. server/etc/init. d/mysqld copy the startup script and rename it
[Root @ station254 ~] # Vim/etc/init. d/mysqld modify the startup script and modify the following content: Basedir =/usr/local/mysql basic Directory, which stores configuration files and other related library files. It can be modified by default. Datadir =/usr/local/mysql/data mysql database Directory, which can be modified by default but not by default.
Conf =/usrlocal/mysql/my. cnf is in row 224. The default value is/etc/my. cnf and is changed to/usr/local/mysql/my. cnf.
|
6. Start the mysql database. If the database fails, check the log and check whether the configuration file is correct.
[Root @ station254 ~] # Service mysqld start mysql database
[Root @ station254 ~] # Netstat-antupl | grep mysqld: view the listening port. The default port is port 3306 of TCP. Tcp 0 0: 3306: * LISTEN 8328/mysqld
View Process status: [Root @ station254 ~] # Ps aux | grep mysqld Root 8147 0.0 0.0 11392 1424 pts/0 S/bin/sh/usr/local/mysql/bin/mysqld_safe -- datadir =/usr/local/mysql/data -- pid- file =/usr/local/mysql/data/mysqld. pid Mysql 8328 0.4 5.6 1011204 449268 pts/0 Sl/usr/local/mysql/bin/mysqld -- basedir =/usr/local/mysql -- datadir =/usr/local/mysql /data -- plugin-dir =/usr/local/mysql/lib/plugin -- user = mysql -- log-error =/usr/local/mysql/data/mysqld. log -- pid-file =/usr/local/mysql/data/mysqld. pid -- socket =/usr/local/mysq/dat/mysqld. sock -- Ports = 3306
|
3. Client Test
The binary mysql package provides tools related to the server and client, which are stored in the/usr/local/mysql/bin directory. Common tools include mysql, mysqld, and mysqldump, the PATH of the default search command is recorded in the special variable $ PATH. To call the client, you can enter an absolute PATH such as/usr/local/mysql/bin/mysql, or add the/usr/local/mysql/bin PATH to the $ PATH variable.
[Root @ station254 ~] #/Usr/local/mysql/bin/mysql if you cannot log on, use-S to specify the path of the socket File Welcome to the MySQL monitor. Commands end with; or \ g Type 'help; 'or' \ H' for help. type' \ C' to clear the current input statement.
Mysql>
This logon method is inconvenient. You can modify the value of the $ PATH variable: [Root @ station254 ~] # PATH = $ PATH:/usr/local/mysql/bin/ [Root @ station254 ~] # Echo $ PATH /Usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/bin: /root/bin:/usr/local/mysql/bin/
The above modification takes effect at. To make the modification take effect permanently, you need to write it into the configuration file: [Root @ station254 ~] # Vim ~ /. Bash_profile PATH = $ PATH: $ HOME/bin:/usr/local/mysql/bin
To make it permanently effective, you can use the source command: [Root @ station254 ~] # Source ~ /. Bash_profile [Root @ station254 ~] # Echo $ PATH /Usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/bin: /root/bin:/usr/local/mysql/bin/
|
Note: If the socket field in the [client] configuration is not configured in the configuration file, you may need to specify the path of the socket file during logon. In addition, you may also find the socket file under/tmp when logging on. The solution is to create a soft link for the socket file and link it to/tmpmysql. sock.
4. Use common tools for testing:
1. log on to the mysql server
[Root @ station254 ~] # Mysql has configured the PATH, so you can directly enter the mysql client to log on Welcome to the MySQL monitor. Commands end with; or \ g. Type 'help; 'or' \ H' for help. type' \ C' to clear the current input statement. Mysql>
|
2. Set the root Login Password
[Root @ station254 ~] # Mysqladmin-u root password 'redhat' set the root user password to redhat |
3. Log On with the new password
[Root @ station254 ~] # Mysql-u root-predhat |
4. view the database
Mysql> show databases; + -------------------- + | Database | four databases are created by default. + -------------------- + | Information_schema | | Mysql | | Performance_schema | | Test | + -------------------- + 4 rows in set (0.01 sec) |
5. Create a database. A database is the basis for storing tables.
Mysql> create database example; Query OK, 1 row affected (0.00 sec)
Mysql> show databases; + -------------------- + | Database | + -------------------- + | Information_schema | | Example | a new database named example is created. At this time, the database does not store any tables. | Mysql | | Performance_schema | | Test | + -------------------- + 5 rows in set (0.00 sec)
|
6. To use a database to manipulate tables in the database, you must first select a database to manipulate the tables in the database.
Mysql> use example; Database changed
|
7. view the tables in the database. The database consists of multiple tables, and the table is the entity of an object.
Mysql> show tables; the newly created database does not contain any tables. Empty set (0.00 sec) |
8. Creating a data table is actually the process of declaring columns, that is, the columns in the table, the attributes of these classes, and the constraints on these columns.
Mysql> create table test ( -> Id int, -> Name char (20 ), -> Sex enum ('M', 'w '), -> Age tinyint unsigned, -> Primary key (id ));
A test table is created with four fields: id, name, sex, and age, indicating the number, name, gender, and age. An integer, primary data type, and primary key constraint are used.
|
9. view the table structure
Mysql> desc test; + ------- + --------------------- + ------ + ----- + --------- + ------- + | Field | Type | Null | Key | Default | Extra | + ------- + --------------------- + ------ + ----- + --------- + ------- + | Id | int (11) | NO | PRI | 0 | | Name | char (20) | YES | NULL | | Sex | enum ('M', 'w') | YES | NULL | | Age | tinyint (3) unsigned | YES | NULL | + ------- + --------------------- + ------ + ----- + --------- + ------- + 4 rows in set (0.00 sec)
In the same row or above mode, the table structure can be clearly displayed, including the data type, constraints, default values, and whether the table is a primary key.
Another way to view the table structure is as follows: Mysql> show create table test; + ------- + Begin certificate + | Table | Create Table | + ------- + Begin certificate + | Test | create table 'test '( 'Id' int (11) not null default '0 ', 'Name' char (20) default null, 'Sex' enum ('M', 'w') default null, 'Age' tinyint (3) unsigned default null, Primary key ('id ') ) ENGINE = InnoDB default charset = latin1 | + ------- + Begin certificate + 1 row in set (0.00 sec)
Through the preceding viewing method, we can know that many additional information needs to be added during table creation, such as engine, character encoding, and default value.
|
10. delete a table:
Mysql> drop table test; Query OK, 0 rows affected (0.13 sec)
|
11. delete a database
Mysql> drop database example; Query OK, 0 rows affected (0.00 sec)
|
12. grant an ordinary user access permission
Command 10 grant granted permissions. Its syntax structure is as follows: Grant permission on database_name.table_name to 'username' @ 'IP' identified by 'Password ';
Where: Permission indicates permissions. For example, add, delete, modify, and query operations, ALL indicates ALL permissions.
Database_name indicates the database name, * indicates all databases Table_name indicates the table name, * indicates all tables in the database Username indicates the user name to be granted The IP address indicates the IP address used to log on to the client. Password indicates the password set for the user.
Create a redhat user and the password is redhat. you can log on from the local machine as follows: Mysql> grant all on *. * to 'redhat' @ 'localhost' identified by 'redhat '; Query OK, 0 rows affected (0.01 sec)
After creating a user, save it in the mysql. user table: Mysql> select User, Host, Password from mysql. user; + -------- + ------------------------ + ------------------------------------------- + | User | Host | Password | + -------- + ------------------------ + ------------------------------------------- + | Root | localhost | * 84BB5DF4823DA319BBF86C99624479A198E6EEE9 | | Root | station254.example.com | | Root | 127.0.0.1 | | Root |: 1 | | Localhost | | Station254.example.com | | Redhat | localhost | * 84BB5DF4823DA319BBF86C99624479A198E6EEE9 | user created successfully + -------- + ------------------------ + ------------------------------------------- + 7 rows in set (0.00 sec)
|
13. Refresh the user permission table
The user created above cannot log on at this time. You need to refresh the permission table so that mysql can read the corresponding information: Mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
Client logon test: [Root @ station254 ~] #/Usr/local/mysql/bin/mysql-u root-predhat-h localhost
|
5. installation script for mysql binary database:
#! /Bin/bash # This script is used to install and configure the mysql Binary Package.
# Check the connectivity of the network. If the network is not general, exit the installation steps and prompt /Bin/ping-c 1 www.mysql.com &>/dev/null If [$? -Ne 0]; then Echo "your network environment is faulty. Please verify it! " Exit 0 Fi
# Download the corresponding binary mysql package if the network environment is okay Cd/usr/local/src; ls mysql-5.6.14-linux * &>/dev/null |/usr/bin/wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz &>/dev/null If [$? -Ne 0]; then Echo "failed to download mysql Binary Package. Please check! " Exit 0 Fi
# Decompress and copy the required files to the mysql home directory /Bin/tar-xf mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz & cd mysql-5.6.14-linux-glibc2.5-x86_64 Test-z/usr/local/mysql | mkdir/usr/local/mysql & cp-R */usr/local/mysql /Bin/chown-R root: root/usr/local/mysql
# Initializing a database Cd/usr/local/mysql &./scripts/mysql_install_db -- user = mysql &>/dev/null If [$? -Ne 0]; then Echo "failed to initialize mysql database. Please check! " Exit 0 Fi
# Setting the configuration file /Bin/cat>/usr/local/mysql/my. cnf <EOF [Mysqld] Datadir =/usr/local/mysql/data User = mysql Port = 3306 Log-error =/usr/local/mysql/data/mysqld. log Pid-file =/usr/local/mysql/data/mysqld. pid Socket =/usr/local/mysql/data/mysqld. sock
[Client] Port = 3306 Socket =/usr/local/mysql/data/mysqld. sock EOF
# Copy the Startup Script Cp-f/usr/local/mysql/support-files/mysql. server/etc/init. d/mysqld>/dev/null 2> & 1 /Sbin/service mysqld start>/dev/null 2> & 1 If [
# Copy the Startup Script Cp-f/usr/local/mysql/support-files/mysql. server/etc/init. d/mysqld>/dev/null 2> & 1 /Sbin/service mysqld start>/dev/null 2> & 1 If [$? -Eq 0]; then Echo "Congratulations, you have successfully configured the binary mysql database. Please log on with the mysql client! " Else Echo "sorry, your binary mysql database configuration is incorrect. Please check it! " Fi
|
Summary: This article describes how to install, configure, and use mysql database binary. Compared with the rpm package installation method, the binary version is faster, and the configuration file needs to be modified manually, to meet the requirements, and use a script to install and configure mysql binary.
|
This article is from the linux Server blog, please be sure to keep this source http://573302346.blog.51cto.com/1730296/1301466