One, the MySQL compilation installs
Preparatory work
1. Check if MySQL has been installed and uninstall it if it is recommended to use (-e).
[Email protected] ~]# rpm-q mysql-server MySQL # #查看是否有安装过MySQL
[Email protected] ~]# mount/dev/cdrom/mnt/# #挂载光盘文件
[[email protected] ~]# rpm-ivh/mnt/packages/ncurses-devel-5.9-13.20130511.el7.x86_64.rpm # #安装ncurses-devel Support Package
2, MySQL 5.X version needs CMake compile installation, so need to install CMake package.
[Email protected] ~]# tar zxf cmake-2.8.6.tar.gz
[Email protected] ~]# CD cmake-2.8.6/
[Email protected] cmake-2.8.6]#./configure
[Email protected] cmake-2.8.6]# gmake && gmake Install
Source code compilation and installation
1, create a running user (MySQL), this user does not need to log on to the system, you can not create the host directory.
[[email protected] cmake-2.8.6]# Groupadd MySQL
[Email protected] cmake-2.8.6]# useradd-m-s/sbin/nologin mysql-g MySQL
2, unzip the download good source package, unzip the directory/USR/SRC
[Email protected] ~]# tar zxf mysql-5.6.36.tar.gz-c/usr/src/
[Email protected] ~]# cd/usr/src/mysql-5.6.36/
3. Configure MySQL to support more features
[Email protected] mysql-5.6.36]# cmake-dcmake_install_prefix=/usr/local/mysql-dsysconfdir=/etc-ddefault_charset= Utf8-ddefault_collation=utf8_general_ci-dwith_extra_charsets=all
DCMAKE_INSTALL_PREFIX ##指定MySQL安装到那个路径下 DSYSCONFDIR ##指定初始化文件目录 DDEFAULT_CHARSET ##指定默认使用的字符集编码(如:utf8) DDEFALUT_COLLATION ##指定默认使用的字符集校对规则,utf8_general_ci是适用于utf-8字符集的通用规则 DWITH_HXTRA_CHARSETS ##指定额外支持的其他字符集编码4、编译安装 [[email protected] mysql-5.6.36]# make [[email protected] mysql-5.6.36]# make install
Additional adjustments after installation
1, the database to set permissions, belong to the main: belong to the group
[Email protected] ~]# chown-r mysql:mysql/usr/local/mysql
2, establish the configuration file, because CENTOS7 default support MARIADB database, so the system default/ETC/MY.COF configuration file is mariadb configuration file,
In the MySQL source directory under the Support-files folder, provided the MySQL data can be a sample configuration file My-default.cof file, so
We need to copy the My-default.cof file to/etc path and delete the MY.COF
[[email protected] mysql-5.6.36]# rm-rf/etc/my.cof [[email protected] mysql-5.6.36]# CP support-files/m Y-DEFAULT.CNF/ETC/MY.CNF [[email protected] ~]# vi/etc/my.cnf # #手动指 Fixed PID file pid-file=/usr/local/mysql/data/mysql.pid:wq3, initialize the database, so that the user MySQL can log in normally, so need to run user MySQL identity to execute the initialization script Mysql_install_ DB, specify the database storage path, and so on. [[email protected] mysql-5.6.36]#/usr/local/mysql/scripts/mysql_install_db--user=mysql--basedir=/usr/local /mysql--DATADIR=/USR/LOCAL/MYSQL/DATA/4, set environment variables, in order to facilitate the use of MySQL command in the approved directory, you need to set the environment variable in/etc/profile [[email protected ] mysql-5.6.36]# echo "path= $PATH:/usr/local/mysql/bin" >>/etc/profile [[email protected] ~]# source/etc/p ROFILE5, adding system services [[email protected] mysql-5.6.36]# CP support-files/mysql.server/usr/local/mysql/bin/mysqld.sh [[email protected] mysql-5.6.36]# chmod +x/usr/local/mysql/bin/mysqld.sh [[email protected] ~]# Vi/usr/lib /systemd/system/mysqld.serVice [Unit] description=the Apache HTTP Server after=network.target [Service] User=mysql Group=mysql Type=forking pidfile= # #PID文件为空 Execstart=/usr/local/mysql/bin/mysqld . sh Start execstop=/usr/local/mysql/bin/mysqld.sh stop [Install] wantedby=multi-user.target:wq [[Email&nbs P;protected] ~]# systemctl start mysqld # #启动MySQL [[email protected] ~]# systemctl enable mysqld # #设置为开机启动 [[email protected] ~]# systemctl status mysqld # #查看启动状态 [[email protected] ~ ]# NETSTAT-ANPT | grep mysqld # #过滤mysql端口状态
Second, access MySQL database
1. Log in to MySQL server: The default user is the root password is empty
1) No password login
[[email protected] ~]# mysql-u root # "-u" option for specifying authenticated Users
2) Login with password
[[email protected] ~]# mysql-u root-p # # "-P" option for password checking
3) Set the root user password to log in to MySQL
[Email protected] ~]# mysqladmin-u root password 123123
2. Execute MySQL operation statement
Mysql> STATUS; # # "STATUS;" Statement to view the basic state of the current database
3. Exit the MySQL operating environment
Mysql> Exit # #退出可以使用 "Exit" or "quit" command to exit the MySQL editor
Third, the use of MySQL database
View the database structure
1. See what databases are in the database system
mysql> show databases;
2. Enter a database
mysql> use MySQL;
3. See what tables are in the current database
Mysql> Show tables;
4. View table structure
mysql> describe user;
Create and delete libraries and tables
1. Create a database named Benet
mysql> CREATE DATABASE Benet;
Query OK, 1 row Affected (0.00 sec)
2, a new table in the database, the table two columns user_name is not allowed to be empty, user_passwd set as the primary key,
Mysql> Use Benet
Database changed
Mysql> CREATE TABLE Users (user_name char (+) not null,user_passwd char (+) DEFAULT ', PRIMARY KEY (user_name));
Query OK, 0 rows affected (0.03 sec)
3. Delete a data table
mysql> DROP TABLE benet.users;
Query OK, 1 row Affected (0.00 sec)
4. Delete a database
mysql> DROP DATABASE Benet;
Query OK, 1 row Affected (0.00 sec)
Manage data records in a table
1. Inserting data into the table
mysql> use Benet;
Database changed
mysql> INSERT into users (USER_NAME,USER_PASSWD) VALUES (' Zhangsan ', PASSWORD (' 123123 ')); # #方法一
Query OK, 1 row Affected (0.00 sec)
Mysql> INSERT into users values (' Lisi ', PASSWORD (' 123123 ')); # #方法二 Query OK, 1 row Affected (0.00 sec) 2, query data record mysql> select * from Benet.users; # #查询表中所有列 +-----------+-------------------------------------------+ | User_name | user_passwd | +-----------+-------------------------------------------+ | Lisi | *e56a114692fe0de073f9a1dd68a00eeb9703f3f1 | | Zhangsan | *e56a114692fe0de073f9a1dd68a00eeb9703f3f1 | +-----------+-------------------------------------------+ 2 rows in Set (0.00 sec) mysql> Select User_name,user_p ASSWD from Benet.users where user_name= ' Zhangsan '; # #按条件查询 +-----------+-------------------------------------------+ | User_name | user_passwd | +-----------+-------------------------------------------+ | Zhangsan | *e56a114692fe0de073f9a1dd68a00eeb9703f3f1 | +-----------+-------------------------------------------+ 1 row in Set (0.00 sec) 3, modify data logging mysql> Update Benet. Users set User_passwd=password (") WHERE user_name= ' Lisi '; # #修改users表中zhangsan的密码为空 Query OK, 1 row Affected (0.00 sec) Rows matched:1 changed:1 warnings:0 mysql> s elect * from Benet.users; +-----------+-------------------------------------------+ | User_name | user_passwd | +-----------+-------------------------------------------+ | Lisi | | | Zhangsan | *e56a114692fe0de073f9a1dd68a00eeb9703f3f1 | +-----------+-------------------------------------------+ 2 rows in Set (0.00 sec) modify root user password mysql> update my Sql.user set Password=password (' 123456 ') WHERE user= ' root '; Query OK, 4 rows Affected (0.00 sec) rows Matched:4 changed:4 warnings:0 mysql> FLUSH privileges; # #刷新用户授权 so that it takes effect immediately. Query OK, 0 rows Affected (0.00 sec) mysql> EXIT Bye [[email protected] ~]# mysql-u root-p Enter pas Sword: # #输入新密码 "123456" 4, delete data record mysql> Delete From Benet.users where user_name= ' Lisi '; # #删除表中user_name列为 ' zhangsan ' user Query OK, 1 row Affected (0.00 sec) mysql> select * from Benet.users; +-----------+-------------------------------------------+ | User_name | user_passwd | +-----------+-------------------------------------------+ | Zhangsan | *e56a114692fe0de073f9a1dd68a00eeb9703f3f1 | +-----------+-------------------------------------------+ 1 row in Set (0.00 sec) database with guest user and local user added by default (user, Password are empty), out of security considerations, should produce these guest users, mysql> select User,host,password from Mysql.user where user= '; # #显示user列为空的用户数据 +------+-----------------------+----------+ | user | Host | password | +------+-----------------------+----------+ | | localhost | | | | Localhost.localdomain | | +------+-----------------------+----------+ 2 rows in Set (0.00 sec) mysql> Delete from mysql.user where user= '; # #删除mysql the user column in the User table empty field Query OK, 2 rows Affected (0.00 sec)
Four, database user authorization
1, grant permission, use the "grant" clause, when the brake user does not exist, the GRANT statement will create a new user, otherwise, the GRANT statement will modify the user information.
Format: GRANT permission list on library name. Table name to User name @ source address [identified by ' Password ']
Permissions list: Used to list the various database operations used by authorization, separated by commas, such as "Select,insert,update". Use "All" to represent all authorizations and perform any action. Library name. Table Name: The name of the library and table that specifies the authorization action, which you can use with the wildcard character "*". For example, use "benet.*". The object that represents the authorization action is all tables in the "Benet" database. User name @ Source address: Used to specify the user name and the client address to allow access, that is, who can connect and where to connect from. The source address can be a domain name, an IP address, or a "%" wildcard that represents all addresses within a region or network segment, such as "%." Benet.com "" 192.168.1.% "and so on. Identified by: Used to set the password string to use when connecting to the database, and if you omit the "identified by" section when creating a new user, the user's password will be empty. Mysql> GRANT Select on benet.* to ' xiaoming ' @ ' localhost ' identified by ' 123123 '; # #使用grant语句创建xiaoming用户 Query OK, 0 rows affected (0.01 sec) and Grant Query permissions 2, view permissions mysql> show grants for ' xiaoming ' @ ' localhost '; +-------------------------------------------------------------------------------------------------------------- ---+ | Grants for [email protected] | +-----------------------------------------------------------------------------------------------------------------+ | GRANT USAGE on *. xiaoming ' @ ' localhost ' identified by PASSWORD ' *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 ' | | GRANT SELECT on ' benet '. * to ' xiaoming ' @ ' localhost ' | +-------------------------------------------------------------------------------------------------------------- ---+ 2 rows in Set (0.00 sec) 3, REVOKE permission, REVOKE statement: Used to revoke a specified user's rights to the database, the user can still connect to the database after the car is repaired, the operation of the corresponding database will be disabled. Format: REVOKE permissions list on database name. Table name from user name @ Source Address mysql> REVOKE all on benet.* from ' xiaoming ' @ ' localhost '; # #撤销 "Xiaoming" to "benet.*" for all permissions query OK, 0 rows Affected (0.00 sec) mysql> Show grants for ' xiaoming ' @ ' localhost '; # #确认已撤销对benet数据库的权限 +------------------------------------------------------------------------------- ----------------------------------+| Grants for [email protected] |+------------------------------------------------------------------------------------------------------------- ----+| GRANT USAGE on *. xiaoming ' @ ' localhost ' identified by PASSWORD ' *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 ' |+------ -----------------------------------------------------------------------------------------------------------+1 Row in Set (0.00 sec) "
(1) centos7--mysql-5.6.36 Install Manual