Installing MySQL on Linux and basic operation

In order to provide a richer, more powerful web applications, often also need to have a back-end database support, web programming and other roles.
Small medium-sized enterprises in general, the most commonly used database is MySQL, MySQL is a true multi-threaded, multi-user SQL database service, with its high performance, high reliability and ease of use features, has become the most popular open source server field database system.
MySQL source code compiler package (MySQL-5.6.tar.gz):
https://pan.baidu.com/s/1pgYHB8kyJ7U1zl2cm4XUcw
extraction code: y8py
MySQL 5.X series version of the most widely used, this version stability, compatibility is Yes
its official site is: http://www.mysql.com
preparations:
to avoid port conflicts, conflict phenomena program, it is recommended to view the MySQL installation, make sure not to use the MySQL RPM package is installed
[root@mysql /]# rpm -qa | grep mysql
, if any, , suggested to unload: RPM -q MySQL MySQL-Server
then needs to be installed is ncurses package already installed three detected, a still missing, it is necessary to mount the CD-ROM system, to install

[root@mysql /]# rpm -qa | grep ncurses
ncurses-5.9-13.20130511.el7.x86_64
ncurses-libs-5.9-13.20130511.el7.x86_64
ncurses-base-5.9-13.20130511.el7.noarch
[root@mysql /] # mount /dev/sr0 /media/
[root@mysql Packages]# rpm -ivh ncurses-devel-5.9-13.20130511.el7.x86_64.rpm 
warning: ncurses-devel-5.9-13.20130511.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:ncurses-devel-5.9-13.20130511.el7################################# [100%]

MySQL 5.X series version requires cmake to compile and install, so you need to install the package cmake:

[root@mysql media]# tar zxf cmake-2.8.6.tar.gz -C /usr/src/
[root@mysql media]# cd /usr/src/cmake-2.8.6/
[root@mysql cmake-2.8.6]# ./configure 
[root@mysql cmake-2.8.6]# gmake && gmake install           # 过程会很长

Source code to compile and install:
Create a user run:

[root@mysql /]# groupadd mysql
[root@mysql /]# useradd -M -s /sbin/nologin mysql -g mysql    # -M 不创建宿主目录   -s 指定shell环境   -g 指定加入组 

Unpack:
download mysql extract the source package:

[root@mysql media]# tar zxf mysql-5.6.36.tar.gz -C /usr/src/
[root@mysql media]# cd /usr/src/mysql-5.6.36/

Configuration:
the rich, the huge structure of corporate website, the website might use a variety of character sets, with a corresponding database system should also support different character set encoding. In the configuration process, you can use the default character set is utf-8, and adds support for character sets.
Note Note Note: The next command input is case-sensitive, a punctuation mark can not be wrong, wrong, you have to start over, after the finish command must be patient and check

[root@mysql 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:指定默认使用的字符集编码
--DDEFAULT_COLLATION:指定默认使用的字符集校对规则,utf8_general_ci 是适用于 utf-8 字符集的通用规则 
--DWITH_EXTRA_CHARSETS:指定额外支持的其他字符集编码

[root@mysql mysql-5.6.36]# make && make install #编译并安装  过程会很漫长
Other adjustments after installation:
the directory database permissions settings:
[root@mysql ~]# chown -R mysql:mysql /usr/local/mysql
Profiling:
In Centos 7 MariaDB database systems supported by default, so the system default configuration file is MariaDB /etc/my.cnf profile. The support-files in the source package file directory folder, the default MySQL database provided a sample configuration file my-default.cnf file, so it is necessary to replace the configuration file provided by MySQL my.cnf before starting the original file content.

[root@mysql ~]# rm -rf /etc/my.cnf                       # 将原来 etc 文件夹下的 my.cnf 文件删除
[root@mysql ~]# cp /usr/src/mysql-5.6.36/support-files/my-default.cnf /etc/my.cnf

Initialize the database:
execute init script mysql_install_db to run the mysql user, specifying that data storage directory

[root@mysql ~]# /usr/local/mysql/scripts/mysql_install_db 
--user=mysql 
--basedir=/usr/local/mysql 
--datadir=/usr/local/mysql/data

Setting environment variables (for ease of use mysql command in any directory, you need to / etc / profile to set the environment variable):

[root@mysql ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@mysql ~]# . /etc/profile                  // 立即生效

Add as a system service:

[root@mysql ~]# cp /usr/src/mysql-5.6.36/support-files/mysql.server
/usr/local/mysql/bin/mysqld.sh       # 将服务脚本复制到 MySQL安装目录中
[root@mysql ~]# chmod +x /usr/local/mysql/bin/mysqld.sh     # 添加执行权限

[root@mysql /]# vim /usr/lib/systemd/system/mysqld.service 

[Unit]
Description=MySQL Server
After=network.target

[Service]
User=mysql                                     # 指定程序运行的用户账号
Group=mysql                                  # 指定程序运行的组账号

Type=forking
PIDFile=/usr/local/mysql/data/mysql.com.pid          # 注意pid前要输主机名,输入之前先查看一下本机主机名
ExecStart=/usr/local/mysql/bin/mysqld.sh  start
ExecStop=/usr/local/mysql/bin/mysqld.sh  stop

[Install]
WantedBy=multi-user.target
[root@mysql /]# systemctl enable mysqld              # 设置开机自启
[root@mysql /]# systemctl status mysqld               # 检查服务启动状态
[root@mysql /]# systemctl start mysqld                  # 启动服务

If too troublesome to write the configuration file, you can change another way:

When the execution of /usr/local/mysql/bin/mysqld.sh given permission, proceed as follows:

[[email protected]]# cp /usr/local/mysql/bin/mysqld.sh /etc/init.d/mysqld
[[email protected]]# vim /etc/init.d/mysqld 

Installing MySQL on Linux and basic operation
After the changes, save and exit. Continue to execute the following command:

[root@localhost mysql-5.6.36]# chkconfig --add mysqld #添加为系统服务

After starting the service landing database:
root is the default MySQL Administrator

[root@mysql /]# mysql -u root              # 无密码的时候登陆
[root@mysql /]# mysqladmin -u root password 123456       # 第一次设置密码
[root@mysql /]# mysqladmin -u root -p password 654321      # 修改密码,先输入新密码
Enter password:                                        # 根据提示输入旧密码
[root@mysql /]# mysql -u root -p               # 使用密码登陆
Enter password:                                         # 根据提示输入密码
 [root@mysql /]# mysql -u root -p 
 ……       // 省略部分内容
mysql> status                    # 查看当前数据库的基本信息
mysql> exit                        # 退出 MySQL操作环境  ouit 也行
Bye

Check out the MySQL database in which:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.03 sec)

View database tables in which:

mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql         |
+---------------------------+
| columns_priv              |
| db                               |
| time_zone_name        |
……      // 省略部分内容
| time_zone_transition   |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

View the table structure:

mysql> describe user;
+----------+----------+------+-----+---------+-------+
| Field    | Type     | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| name     | char(16) | YES  |     | NULL    |       |
| xingbie  | char(10) | YES  |     | NULL    |       |
| nianling | int(11)  | YES  |     | NULL    |       |
+----------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)

Create a new database:
mysql> create database users;
Create a new table:

mysql> use users;
mysql> create table user (name char(16),xingbie char(10),nianling int);

To delete a data table:

mysql> drop table user;
Query OK, 0 rows affected (0.01 sec)

To delete a database:

mysql> drop database users;
Query OK, 0 rows affected (0.00 sec)

Data recording management table:
inserting the data record:

insert   into  表名(字段一,字段二,...)   values(字段一的值,字段二的值,....)
mysql> insert into user (name,xingbie,nianling) values('zhangsan','nan','25');

Query data record:

mysql> select * from user;
+----------+---------+----------+
| name     | xingbie | nianling |
+----------+---------+----------+
| zhangsan | nan     |       25 |
+----------+---------+----------+
1 row in set (0.00 sec)

Edit the record:

update    表名   set    字段名=‘修改的字段值’ where  条件表达式
mysql> update user set nianling='20' where name='zhangsan';

Deleting data records:

delete    from   表名    where   条件表达式;
mysql> delete from user where name='zhangsan';

Database User Authorization:
grant permissions

grant    权限列表    on    库名.表名    to  用户@来源地址   identified    by   ‘密码’;
mysql> grant select on users.user to zhangsan@localhost identified by  '123456';

Use the GRANT statement to note the following:
Installing MySQL on Linux and basic operation
Installing MySQL on Linux and basic operation
View permissions:

show    grants    for  用户名@来源地址;
show    grants    for  'zhangsan'@'localhost';

Revoke privileges:

revoke    权限列表    on   数据库名.表名    from   用户名@来源地址
revoke    all    on   test.user    from   'zhangsan'@'localhost';

Guess you like

Origin blog.51cto.com/14227204/2425596