第二十三章 MySQL数据库 手册1 安装

145 阅读10分钟

@[TOC](第二十三章 MySQL数据库 手册1 安装)


实验⼀:CentOS6系统下安装 MariaDB

⽬的

CentOS6系统下安装 mysql。

前提

linux系统,连接网络。

命令介绍

1 CentOS6.10下yum安装MySQL服务 1.1 CentOS6.10下使默认yum的Base源安装MySQL服务

6 ~]# yum install mysql-server -y

···
Installed:
 mysql-server.x86_64 0:5.1.73-8.el6_8 
Dependency Installed:
 mysql.x86_64 0:5.1.73-8.el6_8 perl-DBD-MySQL.x86_64 0:4.013-3.el6 
 perl-DBI.x86_64 0:1.609-4.el6 
 
Complete!

1.2、CentOS6.10,启动mysql。

#启动服务
6 ~]# service mysqld start

···
Please report any problems with the /usr/bin/mysqlbug script!

                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
#查看端口,mysql的3306,已启动
6 ~]# ss -ntl
State       Recv-Q Send-Q           Local Address:Port             Peer Address:Port 
LISTEN      0      128                  127.0.0.1:6010                        *:*     
LISTEN      0      128                        ::1:6010                       :::*     
LISTEN      0      128                         :::56794                      :::*     
LISTEN      0      50                           *:3306                        *:*     
LISTEN      0      128                          *:60011                       *:*     
LISTEN      0      128                         :::111                        :::*     
LISTEN      0      128                          *:111                         *:*     
LISTEN      0      128                         :::22                         :::*     
LISTEN      0      128                          *:22                          *:*     
LISTEN      0      128                  127.0.0.1:631                         *:*     
LISTEN      0      128                        ::1:631                        :::*     
LISTEN      0      100                        ::1:25                         :::*     
LISTEN      0      100                  127.0.0.1:25                          *:*  

1.3、CentOS6.10,进⼊和退出mysql。

#进入mysql
6 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit		退出mysql
Bye

#停止服务,3306端口关闭
6 ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]

1.4、CentOS6.10下使yum卸载MySQL服务

#查询所有yum安装历史
6 ~]# yum history
Loaded plugins: fastestmirror, refresh-packagekit, security
ID     | Login user               | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
     4 | root <root>              | 2022-05-12 23:54 | Install        |    4   
     3 | root <root>              | 2022-04-05 01:40 | Install        |   46   
     2 | root <root>              | 2022-04-05 01:40 | Install        |    4   
     1 | System <unset>           | 2022-04-05 01:05 | Install        | 1126   
history list

#删除yum安装记录、如第四条也就是我们刚刚安装的mysql以及依赖包
6 ~]# yum history undo 4 -y

实验⼆:CentOS7系统下安装 MariaDB

⽬的

CentOS7系统下安装 MariaDB。

前提

linux系统,连接网络。

命令介绍

1、CentOS7.6下yum安装MySQL服务

1.1、CentOS7.6下使默认yum的Base源安装MySQL服务**

7 ~]# yum install mariadb-server -y
...

Installed:
  mariadb-server.x86_64 1:5.5.60-1.el7_5                                               

Dependency Installed:
  mariadb.x86_64 1:5.5.60-1.el7_5                                                      
  perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7                                         
  perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7                                          
  perl-DBD-MySQL.x86_64 0:4.023-6.el7                                                  
  perl-DBI.x86_64 0:1.627-4.el7                                                        
  perl-Data-Dumper.x86_64 0:2.145-3.el7                                                
  perl-IO-Compress.noarch 0:2.061-2.el7                                                
  perl-Net-Daemon.noarch 0:0.48-5.el7                                                  
  perl-PlRPC.noarch 0:0.2020-14.el7                                                    

Complete!

1.2、CentOS7.6,启动mysql。

#启动服务
7 ~]# systemctl start mariadb
#查看端口,3306,是否启动
7 ~]# ss -ntl
State       Recv-Q Send-Q                                     Local Address:Port       
LISTEN      0      50                                                     *:3306       
LISTEN      0      128                                                    *:111        
LISTEN      0      128                                                    *:6000       
LISTEN      0      128                                                    *:39475      
LISTEN      0      128                                                    *:22         
LISTEN      0      128                                            127.0.0.1:631        
LISTEN      0      100                                            127.0.0.1:25         
LISTEN      0      128                                            127.0.0.1:6010       
LISTEN      0      128                                                   :::52527      
LISTEN      0      128                                                   :::111        
LISTEN      0      128                                                   :::6000       
LISTEN      0      128                                                   :::22         
LISTEN      0      128                                                  ::1:631        
LISTEN      0      100                                                  ::1:25         
LISTEN      0      128                                                  ::1:6010 

1.3、CentOS7.6,进⼊和退出mysql。

#进入mysql
7 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit		#退出mysql
Bye

#停止服务,3306端口关闭
7 ~]# systemctl stop mariadb

1.4、CentOS7.6下使yum卸载MySQL服务

#查看yum安装的历史记录
7 ~]# yum history 
Loaded plugins: fastestmirror, langpacks
ID     | Login user               | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
     2 | root <root>              | 2022-05-13 00:20 | Install        |   10   
     1 | System <unset>           | 2022-01-12 11:01 | Install        | 1382   
history list

#现在刚刚安装的mysql服务
7 ~]# yum history undo 2 -y

2、CentOS7.6下使⽤⼆进制格式安装

前提

还原快照或使用干净系统、可连接网络

下载地址

项目官方: downloads.mariadb.org/mariadb/rep… 国内镜像: mirrors.tuna.tsinghua.edu.cn/mariadb/ mirrors.tuna.tsinghua.edu.cn/mysql/ 个人网盘:【推荐】 share.weiyun.com/xrwzuYEF

#下载 ‘--no-check-certificate’不检查证书
7 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-10.2.25/bintar-linux-x86_64/mariadb-10.2.25-linux-x86_64.tar.gz --no-check-certificate
--2018-05-13 00:35:09--  https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-10.2.25/bintar-linux-x86_64/mariadb-10.2.25-linux-x86_64.tar.gz
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.15.130, 2402:f000:1:400::2
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.15.130|:443... connected.
WARNING: cannot verify mirrors.tuna.tsinghua.edu.cn's certificate, issued by ‘/C=US/O=Let's Encrypt/CN=R3’:
  Issued certificate has expired.
HTTP request sent, awaiting response... 200 OK
Length: 980933367 (935M) [application/octet-stream]
Saving to: ‘mariadb-10.2.25-linux-x86_64.tar.gz’

100%[=============================================>] 980,933,367 14.2MB/s   in 71s    

2018-05-13 00:36:20 (13.1 MB/s) - ‘mariadb-10.2.25-linux-x86_64.tar.gz’ saved [980933367/980933367]

2.2、准备⽤户

7 ~]# groupadd -r -g 306 mysql
7 ~]# useradd -r -g 306 -u 306 -d /data/mysql mysql
7 ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)

2.3、准备数据⽬录,建议使⽤逻辑卷

7 ~]# mkdir -pv /data/mysql
7 ~]# ll -d /data/mysql/
drwxr-xr-x 2 root root 6 May 13 01:22 /data/mysql/
7 ~]# chown mysql.mysql /data/mysql/		#修改权限
7 ~]# ll -d /data/mysql/
drwxr-xr-x 2 mysql mysql 6 May 13 01:22 /data/mysql/

2.4、准备⼆进制程序

#查看解压文件
7 ~]# ls
anaconda-ks.cfg  Downloads                            Music     Templates
Desktop          initial-setup-ks.cfg                 Pictures  Videos
Documents        mariadb-10.2.25-linux-x86_64.tar.gz  Public

#解压缩,把文件解压缩到/usr/local/文件夹
7 ~]# tar xfv mariadb-10.2.25-linux-x86_64.tar.gz -C /usr/local/

#设置软连接
7 ~]# cd /usr/local/
7 local]# ln -sv mariadb-10.2.25-linux-x86_64 mysql
‘mysql’ -> ‘mariadb-10.2.25-linux-x86_64’

7 local]# ll
total 0
drwxr-xr-x.  2 root root   6 Apr 11  2018 bin
drwxr-xr-x.  2 root root   6 Apr 11  2018 etc
drwxr-xr-x.  2 root root   6 Apr 11  2018 games
drwxr-xr-x.  2 root root   6 Apr 11  2018 include
drwxr-xr-x.  2 root root   6 Apr 11  2018 lib
drwxr-xr-x.  2 root root   6 Apr 11  2018 lib64
drwxr-xr-x.  2 root root   6 Apr 11  2018 libexec
drwxrwxr-x  13 wang wang 269 Feb 12 04:03 mariadb-10.2.25-linux-x86_64
lrwxrwxrwx   1 root root  28 May 13 01:30 mysql -> mariadb-10.2.25-linux-x86_64		#刚刚设置的软连接信息
drwxr-xr-x.  2 root root   6 Apr 11  2018 sbin
drwxr-xr-x.  5 root root  49 Jan 12 11:01 share
drwxr-xr-x.  2 root root   6 Apr 11  2018 src

#权限设置 将目录'/usr/local/mysql/'这个目录的所有者和组改为'root和mysql'
7 local]# chown -R root:mysql /usr/local/mysql/

2.5、准备配置⽂件

7 local]# mkdir /etc/mysql/
7 mysql]# cp /usr/local/mariadb-10.2.25-linux-x86_64/support-files/my-large.cnf /etc/mysql/my.cnf

7 mysql]# vim /etc/mysql/my.cnf 
[mysqld]中添加三个选项:
datadir = /data/mysql
innodb_file_per_table = on
skip_name_resolve = on		#禁止主机名解析,建议使用

2.6、创建数据库⽂件

7 local]# cd /usr/local/mysql/
7 mysql]# ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql

2.7、准备服务脚本,并启动服务

7 mysql]# pwd
/usr/local/mysql

7 mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
7 mysql]# chkconfig --add mysqld
7 mysql]# chkconfig --list

Note: This output shows SysV services only and does not include native
 	  systemd services. SysV configuration data might be overridden by native
	  systemd configuration.

 	  If you want to list systemd services use 'systemctl list-unit-files'.
 	  To see services enabled on particular target use
 	  'systemctl list-dependencies [target]'.
 
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
7 mysql]# service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]

7 mysql]# ss -tnl
State       Recv-Q Send-Q                                     Local Address:Port                                                    Peer Address:Port              
LISTEN      0      128                                                    *:111                                                                *:*                  
LISTEN      0      128                                                    *:6000                                                               *:*                  
LISTEN      0      5                                          192.168.122.1:53                                                                 *:*                  
LISTEN      0      128                                                    *:22                                                                 *:*                  
LISTEN      0      128                                            127.0.0.1:631                                                                *:*                  
LISTEN      0      100                                            127.0.0.1:25                                                                 *:*                  
LISTEN      0      128                                            127.0.0.1:6010                                                               *:*                  
LISTEN      0      128                                                    *:39326                                                              *:*                  
LISTEN      0      80                                                    :::3306                                                              :::*                  
LISTEN      0      128                                                   :::111                                                               :::*                  
LISTEN      0      128                                                   :::6000                                                              :::*                  
LISTEN      0      128                                                   :::22                                                                :::*                  
LISTEN      0      128                                                  ::1:631                                                               :::*                  
LISTEN      0      100                                                  ::1:25                                                                :::*                  
LISTEN      0      128                                                  ::1:6010                                                              :::*                  
LISTEN      0      128                                                   :::58910                                                             :::*         

2.8、PATH路径

7 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
7 ~]# vim /etc/profile.d/mariadb10.2.25.sh
#!/bin/bash

export PATH=/usr/local/mysql/bin:$PATH
7 ~]# source /etc/profile.d/mariadb10.2.25.sh 
7 ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

2.9、登录

7 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.25-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit		#退出
Bye

2.10、安全初始化

7 ~]# mysql_secure_installation 

设置数据库管理员root口令
禁止root远程登录
删除anonymous用户帐号
删除test数据库

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 							#密码'123456'
Re-enter new password: 					#确认密码'123456'
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

#用户root登录,密码123456
7 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 10.2.25-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit
Bye

2.11、删除服务

7 ~]# rm -f mariadb-10.2.25-linux-x86_64.tar.gz 

7 ~]# service mysqld stop
Stopping mysqld (via systemctl):                           [  OK  ]

7 ~]# userdel mysql

7 ~]# rm -rf /data/mysql/

7 ~]# rm -rf /usr/local/mariadb-10.2.25-linux-x86_64/

7 ~]# rm -f /usr/local/mysql 

7 ~]# rm -rf /etc/mysql/

7 ~]# chkconfig --del mysqld

7 ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
 	  systemd services. SysV configuration data might be overridden by native
	  systemd configuration.

 	  If you want to list systemd services use 'systemctl list-unit-files'.
 	  To see services enabled on particular target use
 	  'systemctl list-dependencies [target]'.
 
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off

7 ~]# rm -f /etc/profile.d/mariadb10.2.25.sh

3、CentOS7.6下使MariaDB官⽅源码包编译安装

3.1、下载源码包:

官方下载地址: archive.mariadb.org/mariadb-10.… 清华大学镜像: mirrors.tuna.tsinghua.edu.cn/mariadb/mar… 网盘下载地址: share.weiyun.com/2tmPdZJO

7 ~]# 7 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-10.2.43/source/mariadb-10.2.43.tar.gz --no-check-certificate
--2022-05-15 11:13:16--  https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-10.2.43/source/mariadb-10.2.43.tar.gz
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.15.130, 2402:f000:1:400::2
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.15.130|:443... connected.
WARNING: cannot verify mirrors.tuna.tsinghua.edu.cn's certificate, issued by ‘/C=US/O=Let's Encrypt/CN=R3’:
  Issued certificate has expired.
HTTP request sent, awaiting response... 200 OK
Length: 74415871 (71M) [application/octet-stream]
Saving to: ‘mariadb-10.2.43.tar.gz’

100%[=============================================>] 74,415,871  17.4MB/s   in 4.2s   

2022-05-15 11:13:21 (17.0 MB/s) - ‘mariadb-10.2.43.tar.gz’ saved [74415871/74415871]

7 ~]# ll mariadb-10.2.43.tar.gz 
-rw-r--r-- 1 root root 74415871 Feb 11 02:06 mariadb-10.2.43.tar.gz

3.2、安装依赖的包:

7 ~]# yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel -y

3.3、解压源码包:

7 ~]# tar zxf mariadb-10.2.43.tar.gz 
7 ~]# ll -d mariadb-10.2.43/
drwxrwxr-x 33 1001 1001 4096 Feb 11 02:06 mariadb-10.2.43/
7 ~]# ll mariadb-10.2.43/
total 320
-rw-rw-r--  1 1001 1001  1485 Feb 11 02:06 appveyor.yml
drwxrwxr-x  2 1001 1001  4096 Feb 11 02:06 BUILD
-rw-rw-r--  1 1001 1001  8575 Feb 11 02:06 BUILD-CMAKE
drwxrwxr-x  2 1001 1001  4096 Feb 11 02:06 client
drwxrwxr-x  5 1001 1001  4096 Feb 11 02:06 cmake
-rw-rw-r--  1 1001 1001 18155 Feb 11 02:06 CMakeLists.txt
-rw-rw-r--  1 1001 1001 17323 Feb 11 02:06 config.h.cmake
-rw-rw-r--  1 1001 1001 32054 Feb 11 02:06 configure.cmake
-rw-rw-r--  1 1001 1001 17987 Feb 11 02:06 COPYING
-rw-rw-r--  1 1001 1001  2093 Feb 11 02:06 CREDITS
drwxrwxr-x  2 1001 1001   290 Feb 11 02:06 dbug
drwxrwxr-x  6 1001 1001  4096 Feb 11 02:06 debian
drwxrwxr-x  2 1001 1001   172 Feb 11 02:06 Docs
drwxrwxr-x  6 1001 1001   317 Feb 11 02:06 extra
drwxrwxr-x  4 1001 1001  4096 Feb 11 02:06 include
-rw-rw-r--  1 1001 1001   108 Feb 11 02:06 INSTALL-SOURCE
-rw-rw-r--  1 1001 1001   130 Feb 11 02:06 INSTALL-WIN-SOURCE
-rw-rw-r--  1 1001 1001  1562 Feb 11 02:06 KNOWN_BUGS.txt
drwxrwxr-x 12 1001 1001  4096 Feb 11 02:06 libmariadb
drwxrwxr-x  3 1001 1001   247 Feb 11 02:06 libmysqld
drwxrwxr-x  2 1001 1001  4096 Feb 11 02:06 libservices
drwxrwxr-x  2 1001 1001  4096 Feb 11 02:06 man
drwxrwxr-x 10 1001 1001  4096 Feb 11 02:06 mysql-test
drwxrwxr-x  2 1001 1001  4096 Feb 11 02:06 mysys
drwxrwxr-x  2 1001 1001   213 Feb 11 02:06 mysys_ssl
drwxrwxr-x  5 1001 1001  4096 Feb 11 02:06 pcre
drwxrwxr-x 32 1001 1001  4096 Feb 11 02:06 plugin
drwxrwxr-x  3 1001 1001    18 Feb 11 02:06 randgen
-rw-rw-r--  1 1001 1001  2440 Feb 11 02:06 README.md
drwxrwxr-x  2 1001 1001  4096 Feb 11 02:06 scripts
drwxrwxr-x  3 1001 1001 12288 Feb 11 02:06 sql
drwxrwxr-x  5 1001 1001  4096 Feb 11 02:06 sql-bench
drwxrwxr-x  2 1001 1001   151 Feb 11 02:06 sql-common
drwxrwxr-x 25 1001 1001  4096 Feb 11 02:06 storage
drwxrwxr-x  2 1001 1001  4096 Feb 11 02:06 strings
drwxrwxr-x  6 1001 1001  4096 Feb 11 02:06 support-files
drwxrwxr-x  2 1001 1001  4096 Feb 11 02:06 tests
-rw-rw-r--  1 1001 1001 86263 Feb 11 02:06 THIRDPARTY
drwxrwxr-x  9 1001 1001   141 Feb 11 02:06 unittest
-rw-rw-r--  1 1001 1001    68 Feb 11 02:06 VERSION
drwxrwxr-x  3 1001 1001   298 Feb 11 02:06 vio
drwxrwxr-x  4 1001 1001    71 Feb 11 02:06 win
drwxrwxr-x  2 1001 1001   130 Feb 11 02:06 wsrep
drwxrwxr-x 11 1001 1001  4096 Feb 11 02:06 zlib

3.4、⽤户和数据⽬录

7 ~]# mkdir -pv /data/mysql
7 ~]# useradd -r -s /sbin/nologin -d /data/mysql mysql

7 ~]# cat  /etc/passwd | grep mysql
mysql:x:987:981::/data/mysql:/sbin/nologin
7 ~]# chown mysql.mysql /data/mysql/
7 ~]# ll -d /data/mysql/
drwxr-xr-x 2 mysql mysql 6 May 13 18:17 /data/mysql/

3.5、配置、编译和安装

7 ~]# cd mariadb-10.2.43/

7 mariadb-10.2.43]#  cmake . \
-DCMAKE_INSTALL_PREFIX=/app/mysql \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc/mysql \
-DMYSQL_USER=mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

以下是执行过程:
-- Running cmake version 2.8.12.2
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
···
-- Looking for strndup
-- Looking for strndup - found
-- Looking for krb5_free_unparsed_name
-- Looking for krb5_free_unparsed_name - found
-- Looking for event.h
-- Looking for event.h - not found
-- Configuring done
-- Generating done
-- Build files have been written to: /data/mariadb-10.2.43

提示:如果出错,执行rm -f CMakeCache.txt

编译安装

7 mariadb-10.2.43]#  make && make install
以下是执行过程:
Scanning dependencies of target INFO_BIN
[ 0%] Built target INFO_BIN
Scanning dependencies of target INFO_SRC
[ 0%] Built target INFO_SRC
Scanning dependencies of target abi_check
[ 0%] Built target abi_check
Scanning dependencies of target readline
[ 0%] Building C object extra/readline/CMakeFiles/readline.dir/readline.c.o
[ 0%] Building C object extra/readline/CMakeFiles/readline.dir/funmap.c.o
··· 	时间很长
-- Installing: /app/mysql/share/pkgconfig/mariadb.pc
-- Installing: /app/mysql/share/aclocal/mysql.m4
-- Installing: /app/mysql/support-files/mysql.server

3.6、设置环境变量

7 mariadb-10.2.43]# vim /etc/profile.d/mariadb-10.2.43.sh
#!/bin/bash

export PATH=/app/mysql/bin:$PATH
7 mariadb-10.2.43]# source /etc/profile.d/mariadb-10.2.43.sh 
7 mariadb-10.2.43]# echo  $PATH
/app/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

3.7、初始化数据库

7 mariadb-10.2.43]# cd /app/mysql/
7 mysql]# scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql

3.8、准备配置⽂件

7 mysql]# cp /app/mysql/support-files/my-huge.cnf /etc/my.cnf

3.9、添加服务为开启启动

7 mysql]# cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld

3.10、执⾏MariaDB安全脚本,启动mysql

7 mysql]# chkconfig --add mysqld
7 mysql]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
7 mysql]# service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]
7 mysql]# ss -ntl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128             *:111                         *:*                  
LISTEN      0      128             *:6000                        *:*                  
LISTEN      0      128             *:22                          *:*                  
LISTEN      0      128     127.0.0.1:631                         *:*                  
LISTEN      0      100     127.0.0.1:25                          *:*                  
LISTEN      0      128     127.0.0.1:6010                        *:*                  
LISTEN      0      128             *:36132                       *:*                  
LISTEN      0      80             :::3306                       :::*                  
LISTEN      0      128            :::111                        :::*                  
LISTEN      0      128            :::6000                       :::*                  
LISTEN      0      128            :::22                         :::*                  
LISTEN      0      128           ::1:631                        :::*                  
LISTEN      0      100           ::1:25                         :::*                  
LISTEN      0      128           ::1:6010                       :::*                  
LISTEN      0      128            :::49178                      :::*              
7 mysql]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.43-MariaDB-log Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit
Bye

3.11、安全初始化

7 mysql]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y

New password: 				#密码'123456'
Re-enter new password: 		#确认密码'123456'
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
#用户root登录,密码123456
7 mysql]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 10.2.43-MariaDB-log Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit
Bye

4、CentOS7.6下使MariaDB官⽅yum源安装MariaDB多实例

官方YUM源编辑器

官方提供:mariadb.org/download/?t…

在这里插入图片描述

4.1、自制yum源(10.2版本)

7 ~]# cd /etc/yum.repos.d/

7 yum.repos.d]# vim mariadb10.2.repo

# MariaDB 10.2 CentOS repository list - created 2022-05-14 05:44 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
baseurl = https://mirrors.aliyun.com/mariadb/yum/10.2/centos7-amd64
gpgkey=https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

4.2、安装MariaDB

7 ~]# yum install MariaDB-server -y

4.3、创建文件夹,将多实例分开。

7 ~]# mkdir -pv /usr/local/mysql/{3306,3307,3308}/{data,etc,socket,bin,log,pid}
7 ~]# tree /usr/local/mysql
/usr/local/mysql
├── 3306
│   ├── bin
│   ├── data
│   ├── etc
│   ├── log
│   ├── pid
│   └── socket
├── 3307
│   ├── bin
│   ├── data
│   ├── etc
│   ├── log
│   ├── pid
│   └── socket
└── 3308
    ├── bin
    ├── data
    ├── etc
    ├── log
    ├── pid
    └── socket

21 directories, 0 files
7 ~]# id mysql
uid=987(mysql) gid=981(mysql) groups=981(mysql)

7 ~]# chown -R mysql.mysql /usr/local/mysql/

7 ~]# ll -d /usr/local/mysql/
drwxr-xr-x 5 mysql mysql 42 May 14 13:51 /usr/local/mysql/

4.4、分别为3个MariaDB实例创建初始化数据

7 ~]# mysql_install_db --user=mysql --datadir=/usr/local/mysql/3306/data
7 ~]# mysql_install_db --user=mysql --datadir=/usr/local/mysql/3307/data
7 ~]# mysql_install_db --user=mysql --datadir=/usr/local/mysql/3308/data

如果是10.2以上版本cp /etc/my.cnf /usr/local/mysql/3306/etc/my.cnf、并复制注意1全部内容到/usr/local/mysql/3306/etc/my.cnf


7 ~]# cp /usr/share/mysql/my-huge.cnf /usr/local/mysql/3306/etc/my.cnf
#注意1
7 ~]# cat /usr/local/mysql/3306/etc/my.cnf

[client]

port = 3306
socket = /usr/local/mysql/3306/socket/mysql.sock	#修改

[mysqld]
datadir = /usr/local/mysql/3306/data/	#修改
port = 3306
socket = /usr/local/mysql/3306/socket/mysql.sock	#修改
log-error = /usr/local/mysql/3306/log/mariadb.log	#修改
pid-file = /usr/local/mysql/3306/pid/mariadb.pid	#修改
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M

thread_concurrency = 8

log-bin=mysql-bin

server-id = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
7 ~]# cp /usr/local/mysql/3306/etc/my.cnf /usr/local/mysql/3307/etc/my.cnf
7 ~]# sed -i 's/3306/3307/' /usr/local/mysql/3307/etc/my.cnf 

7 ~]# cp /usr/local/mysql/3306/etc/my.cnf /usr/local/mysql/3308/etc/my.cnf
7 ~]# sed -i 's/3306/3308/' /usr/local/mysql/3308/etc/my.cnf 

4.5、编写服务启动脚本

7 ~]# vim /usr/local/mysql/3306/bin/mysqld
#!/bin/bash
#chkconfig: 345 80 2
#服务器端口
port=3306
#MySQL用户名
mysql_user="root"
#MySQL用户密码
#mysql_pwd=""
#MySQL路径
mysql_basedir="/usr/local/mysql"
#指定连接socket文件路径
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
	if [ ! -e "$mysql_sock" ];then
		printf "Starting MySQL...\n"
		mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf &> /dev/null &
	else
		printf "MySQL is running...\n"
		exit
	fi
}

function_stop_mysql()
{
	if [ ! -e "$mysql_sock" ];then
		printf "MySQL is stopped...\n"
		exit
	else
		printf "Stoping MySQL...\n"
		mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
	fi
}

function_restart_mysql()
{
	printf "Restarting MySQL...\n"
	function_stop_mysql
	sleep 2
	function_start_mysql
}

case $1 in
start)
	function_start_mysql
;;
stop)
	function_stop_mysql
;;
restart)
	function_restart_mysql
;;
*)
	printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac
7 ~]# chmod 755 /usr/local/mysql/3306/bin/mysqld 

7 ~]# cp /usr/local/mysql/3306/bin/mysqld /usr/local/mysql/3307/bin/mysqld
7 ~]# sed -i 's/3306/3307/' /usr/local/mysql/3307/bin/mysqld

7 ~]# cp /usr/local/mysql/3306/bin/mysqld /usr/local/mysql/3308/bin/mysqld
7 ~]# sed -i 's/3306/3308/' /usr/local/mysql/3308/bin/mysqld

4.6、启动服务

[root@centos7 ~]# /usr/local/mysql/3306/bin/mysqld start
Starting MySQL...
[root@centos7 ~]# /usr/local/mysql/3307/bin/mysqld start
Starting MySQL...
[root@centos7 ~]# /usr/local/mysql/3308/bin/mysqld start
Starting MySQL...
#查看是否开启端口330633073308
7 ~]# ss -tnl|grep 330
LISTEN      0      80             :::3306                       :::*                  
LISTEN      0      80             :::3307                       :::*                  
LISTEN      0      80             :::3308                       :::*                  
7 ~]# mysql -uroot -S /usr/local/mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.43-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit
Bye

7 ~]# mysql -uroot -S /usr/local/mysql/3307/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.43-MariaDB-log Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit
Bye

7 ~]# mysql -uroot -S /usr/local/mysql/3308/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.43-MariaDB-log Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit
Bye
7 ~]# /usr/local/mysql/3306/bin/mysqld restart
Restarting MySQL...
Stoping MySQL...
Enter password: 	#密码为空、直接回车
Starting MySQL...

7 ~]# ss -ntl	#查看一下3306端口号、是否重启成功
State       Recv-Q Send-Q                                     Local Address:Port                                                    Peer Address:Port              
LISTEN      0      128                                                    *:111                                                                *:*                  
LISTEN      0      128                                                    *:6000                                                               *:*                  
LISTEN      0      128                                                    *:40785                                                              *:*                  
LISTEN      0      5                                          192.168.122.1:53                                                                 *:*                  
LISTEN      0      128                                                    *:22                                                                 *:*                  
LISTEN      0      128                                            127.0.0.1:631                                                                *:*                  
LISTEN      0      100                                            127.0.0.1:25                                                                 *:*                  
LISTEN      0      128                                            127.0.0.1:6010                                                               *:*                  
LISTEN      0      5                                              127.0.0.1:25151                                                              *:*                  
LISTEN      0      128                                                   :::38663                                                             :::*                  
LISTEN      0      80                                                    :::3306                                                              :::*                  
LISTEN      0      128                                                   :::111                                                               :::*                  
LISTEN      0      128                                                   :::6000                                                              :::*                  
LISTEN      0      128                                                   :::80                                                                :::*                  
LISTEN      0      128                                                   :::22                                                                :::*                  
LISTEN      0      128                                                  ::1:631                                                               :::*                  
LISTEN      0      100                                                  ::1:25                                                                :::*                  
LISTEN      0      128                                                  ::1:6010                                                              :::*    
7 ~]# /usr/local/mysql/3306/bin/mysqld stop
Stoping MySQL...
Enter password: 	#密码为空、直接回车

7 ~]# ss -ntl		##查看一下3306端口号、是否关闭
State       Recv-Q Send-Q                                     Local Address:Port                                                    Peer Address:Port              
LISTEN      0      128                                                    *:111                                                                *:*                  
LISTEN      0      128                                                    *:6000                                                               *:*                  
LISTEN      0      128                                                    *:40785                                                              *:*                  
LISTEN      0      5                                          192.168.122.1:53                                                                 *:*                  
LISTEN      0      128                                                    *:22                                                                 *:*                  
LISTEN      0      128                                            127.0.0.1:631                                                                *:*                  
LISTEN      0      100                                            127.0.0.1:25                                                                 *:*                  
LISTEN      0      128                                            127.0.0.1:6010                                                               *:*                  
LISTEN      0      5                                              127.0.0.1:25151                                                              *:*                  
LISTEN      0      128                                                   :::38663                                                             :::*                  
LISTEN      0      128                                                   :::111                                                               :::*                  
LISTEN      0      128                                                   :::6000                                                              :::*                  
LISTEN      0      128                                                   :::80                                                                :::*                  
LISTEN      0      128                                                   :::22                                                                :::*                  
LISTEN      0      128                                                  ::1:631                                                               :::*                  
LISTEN      0      100                                                  ::1:25                                                                :::*                  
LISTEN      0      128                                                  ::1:6010                                                              :::*        

实验三:登录数据库

交互式模式:
	客户端命令:
		\h, help
		\u,use
		\s,status
		\!,system
 	服务器端命令:
 		SQL语句, 需要语句结束符;
脚本模式:
 	mysql –uUSERNAME -pPASSWORD < /path/somefile.sql
 	mysql> source /path/from/somefile.sql

mysql客户端可用选项:
 -A, --no-auto-rehash 禁止补全
 -u, --user= 用户名,默认为root
 -h, --host= 服务器主机,默认为localhost
 -p, --passowrd= 用户密码,建议使用-p,默认为空密码
 -P, --port= 服务器端口
 -S, --socket= 指定连接socket文件路径
 -D, --database= 指定默认数据库
 -C, --compress 启用压缩
 -e “SQL“ 执行SQL命令
 -V, --version 显示版本
 -v --verbose 显示详细信息
 --print-defaults 获取程序默认使用的配置

服务器监听的两种socket地址:
 ip socket: 监听在tcp的3306端口,支持远程通信
 unix sock: 监听在sock文件上,仅支持本机通信
 如:/var/lib/mysql/mysql.sock

登录系统:mysql –uroot –p