MySQL Strict SQL MODE

严格SQL模式控制MySQL如何处理数据改变语句(insert或update)中的无效或缺失值。一个值可能由于各种原因而无效。例如,它对于列来说有错误的数据类型,或者超过了列的范围。当新记录被插入而对于非NULL且没有显式在定义时指定DEFAULT子句的列没有包含值就会出现缺失值的情况。(对于一个NULL列,如果缺失值就会插入NULL值)严格SQL模式也会影响DDL语句比如create table。

如果严格SQL模式没有生效,MySQL对于无效或者缺失值会插入调整值并生成一个警告。在严格SQL模式中,可以通过使用insert ignore或udpate ignore来产生这种行为。

对于select这样不改变数据的语句,在严格SQL模式中无效值会生成一个警告而不是错误。

严格SQL模式对于试图创建一个键值而超过列的最大键值长度时会产生一个错误。当严格SQL模式没有启用时,会产生一个警告并且截断键值的长度使其满足最大键值长度。

严格SQL模式不影响是否对外键约束执行检查。foreign_key_checks可以被使用。

如果STRICT_ALL_TABLES或STRICT_TRANS_TABLES被启用严格SQL模式就会生效,但这些模式的影响会有不同:
.对于事务表来说,当STRICT_ALL_TABLES或STRICT_TRANS_TABLES被启用后当在数据出现无效或丢失值就会出现错误。语句就会被终止与回滚。

.对于非事务表,如果在插入或更新语句中第一行记录出现坏值这些模式的行为是一样的:语句被终止并且表仍然保持不变。如果语句插入或修改多行记录并且在第二行或之后的行记录中出现坏值,那么结果依赖于严格SQL模式是否被启用。
-对于STRICT_ALL_TABLES,MySQL会返回一个错误并忽略剩余的行记录。然而,因为早些的行记录已经被插入或被更新,会导致部分更新。为了避免这个问题,使用单行语句,就会终止而不会改变表数据。

-对于STRICT_TRANS_TABLES,MySQL会将一个无效的值转换成一个最接近的有效值并插入这个调整值。如果这个值将丢失,MySQL插入这个隐式缺省值。在这种情况下,MySQL生成一个敬告而不是一个错误并继续处理语句。

严格SQL模式对除零,零日期和日期中出现零的处理如下:
.严格SQL模式影响对除零的处理,它包括MOD(N,0):对于数据改变操作(insert,update):
-如果严格SQL模式没有被启用,除零会插入NULL并生成一个警告。
-如果严格SQL模式被启用,除非指定了ignore否则除零操作会生成一个错误。对于insert ignore和update ignore操作,除零操作会插入NULL并生成一个警告。

对于select,除零操作会返回NULL。启用严格SQL模式会导致一个警告。
.严格SQL模式会影响服务器是否允许’0000-00-00’为一个有效日期:
-如果严格SQL模式没有被启用,’0000-00-00’被允许并且插入操作不会产生警告。
-如果严格SQL模式被启用,’0000-00-00’不被允许并且插入操作会产生错误,除非你指定ignore。对于insert ignore或update ignore,’0000-00-00’被允许并且插入操作会产生警告。

.严格SQL模式影响服务器是否允许在日期中的年部分为非零但月和日部分允许为零(比如’2010-00-01’或’2010-01-00′):
-如果严格SQL模式没有被启用,有零的日期被允许并且插入操作不产生警告。
-如果严格SQL模式被启用,有零的日期不被允许并且插入操作产生错误,除非指定ignore。对于insert ignore或update ignore来说,有零的日期将以’0000-00-00’形式被插入并产生一个警告。

IGNORE关键字与严格SQL模式的对比
这里将介绍在语句执行时IGNORE关键字(它降级错误为警告)和严格SQL模式(它升级警告为错误)的对比。描述它们影响那些语句以及应用它们有那些错误。

IGNORE对语句执行的影响
MySQL中的一些语句支持可选的IGNORE关键字。此关键字将导致服务器降级某些类型的错误并生成警告。对于多行语句,IGNORE会导致语句跳到下一行,而不是中止。

例如,如果表t2有一个主键列i,试图在多行记录中插入相同的i值正常来说会产生一个重复键错误:

mysql> insert into t2(i) values(1),(1);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

当使用IGNORE关键字时,包含重复键值的记录仍然不会被插入,但会使用警告来代替错误:

mysql> insert ignore into t2(i) values(1),(1);
Query OK, 1 row affected, 1 warning (0.15 sec)
Records: 2  Duplicates: 1  Warnings: 1

mysql> show warnings;
+---------+------+---------------------------------------+
| Level   | Code | Message                               |
+---------+------+---------------------------------------+
| Warning | 1062 | Duplicate entry '1' for key 'PRIMARY' |
| Warning | 1062 | Duplicate entry '1' for key 'PRIMARY' |
+---------+------+---------------------------------------+
2 rows in set (0.00 sec)

以下语句支持IGNORE关键字:
.create table… select:ignore不能应用到语句的create table或select部分但对于由select语句所提供记录来执行插入语句可以应用。对于唯一键值重复的记录会被丢弃。

.delete:ignore会导致MySQL在处理删除记录时忽略错误。

.insert:使用ignore,对于唯一键值重复的记录会被丢弃。对于重复键值的行记录会导致数据转换为最接近的有效值被插入。

对于分区表当没有匹配指定值的分区被找到时,ignore会导致包含那些不匹配值的记录的插入操作失败。
.load data,load xml:使用ignore,对于唯一键值重复的记录会被丢弃。

.update:使用ignore,对于在唯一键值出现重复键值冲突的记录不会被更新。被更新的记录可能导致数据转换为最接近的有效值被插入。

ignore关键字应用到以下错误:
ER_BAD_NULL_ERROR
ER_DUP_ENTRY
ER_DUP_ENTRY_WITH_KEY_NAME
ER_DUP_KEY
ER_NO_PARTITION_FOR_GIVEN_VALUE
ER_NO_PARTITION_FOR_GIVEN_VALUE_SILENT
ER_NO_REFERENCED_ROW_2
ER_ROW_DOES_NOT_MATCH_GIVEN_PARTITION_SET
ER_ROW_IS_REFERENCED_2
ER_SUBQUERY_NO_1_ROW
ER_VIEW_CHECK_FAILED

严格SQL模式对语句执行的影响
MySQL服务器可以以不同的SQL模式进行操作并且可以应用这些不同模式到不同的客房端,这依赖于sql_mode系统变量。在严格SQL模式中,服务器会将特定的警告升级成错误。

例如,在非严格SQL模式中,向整数类型列插入字符串’abc’的结果是将这个字符串值转换为0并生成一个警告:

mysql> SET sql_mode = '';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> insert into t2(i) values('abc');
Query OK, 1 row affected, 1 warning (0.02 sec)

mysql> show warnings;
+---------+------+--------------------------------------------------------+
| Level   | Code | Message                                                |
+---------+------+--------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: 'abc' for column 'i' at row 1 |
+---------+------+--------------------------------------------------------+
1 row in set (0.00 sec)

在严格SQL模式下,无效值会被拒绝并生成错误:

mysql> SET sql_mode = 'STRICT_ALL_TABLES';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> insert into t2(i) values('abc');
ERROR 1366 (HY000): Incorrect integer value: 'abc' for column 'i' at row 1

在某些条件下,某些值可能超出范围或将无效行插入或从表中删除,严格SQL模式适用于以下语句:
alter table
create table
create table … select
delete(单表和多表)
insert
load data
load xml
select sleep()
update(单表和多表)
在存储程序中,如果程序是在严格模式生效时定义的,则刚才列出的类型的各个语句将以严格SQL模式执行。

严格SQL模式应用于以下错误,代表输入值可能无效或丢失这类错误。如果对于列值使用了错误数据类型或超过了值的
范围那么值就是无效的。如果被插入的新行不包含NOT NULL列值但除了在列定义时显式指定了DEFAULT子句的那么就
是值丢失。
ER_BAD_NULL_ERROR
ER_CUT_VALUE_GROUP_CONCAT
ER_DATA_TOO_LONG
ER_DATETIME_FUNCTION_OVERFLOW
ER_DIVISION_BY_ZERO
ER_INVALID_ARGUMENT_FOR_LOGARITHM
ER_NO_DEFAULT_FOR_FIELD
ER_NO_DEFAULT_FOR_VIEW_FIELD
ER_TOO_LONG_KEY
ER_TRUNCATED_WRONG_VALUE
ER_TRUNCATED_WRONG_VALUE_FOR_FIELD
ER_WARN_DATA_OUT_OF_RANGE
ER_WARN_NULL_TO_NOTNULL
ER_WARN_TOO_FEW_RECORDS
ER_WRONG_ARGUMENTS
ER_WRONG_VALUE_FOR_TYPE
WARN_DATA_TRUNCATED

MySQL SQL模式

MySQL服务器可以以不同的SQL模式来进行操作,并且依赖于sql_mode系统变量的值对不同的客户端可以应用这些不同的SQL模式。DBA可以设置全局SQL模式来匹配服务器操作要求,并且每种应用程序可以设置它的会话SQL模式来满足它的要求。

SQL模式会影响MySQL支持的SQL语法和数据验证检查。这可以在不同环境中让MySQL与其它数据库一起使用变得更容易。

当使用InnoDB表时,可以考虑使用innodb_strict_mode系统变量,它可以对InnoDB表启用额外的错误检查。

设置SQL模式
在MySQL 5.7中缺省的SQL模式包含:ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES, NO_ZERO_IN_DATE,NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,和 NO_ENGINE_SUBSTITUTION。ONLY_FULL_GROUP_BY和STRICT_TRANS_TABLES是在MySQL 5.7.5中加入的。NO_AUTO_CREATE_USER是在MySQL 5.7.7中加入的。ERROR_FOR_DIVISION_BY_ZERO,NO_ZERO_IN_DATE和NO_ZERO_DATE是在MySQL 5.7.8中加入的。

为了在服务器启动时设置SQL模式,可以在命令行中使用–sql-mode=”modes”选项或在选项文件比如Unix中的my.cnf或Windows上的my.ini文件中使用sql-mode=”modes”选项。modes是用逗号分的不同模式列表。为了显式的清除SQL模式,可以在命令行中使用–sql-mode=””选项将SQL模式设置为空字符串,或者在选项文件中使用sql-mode=””。

MySQL安装程序可以在安装过程中会配置SQL模式。例如,mysql_install_db将在基本的安装目录中创建一个命名为my.cnf的缺省选项文件。这个文件包含设置SQL模式的记录。

如果SQL模式不同于缺省SQL模式或你所期待的SQL模式,可以检查服务器在启动时所读取的选项文件。

为了在运行时改变SQL模式,可以使用set语句来设置全局或会话级的sql_mode系统变量:
set global sql_mode=’modes’;
set session sql_mode=’modes’;

设置global变量需要有super权限并且影响所有连接的客户端操作。设置session变量只影响当前客户端。每个客户端可以在任何时间改变它会话的sql_mode值来达到改变SQL模式的目的。

为了判断当前全局或session级的sql_mode值,执行以下命令:

mysql> SELECT @@GLOBAL.sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@GLOBAL.sql_mode                                                                                                                         |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT @@SESSION.sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@SESSION.sql_mode                                                                                                                        |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

注意在创建分区表并插入数据之后改变服务器的SQL模式会对表的行为造成重大改变并且可能导致丢失或损坏数据。所以强烈建议在创建用户定义的分区表之后不要修改SQL模式。

当复制分区表时,在主从服务器之间不同的SQL模式也会导致一些问题。所以最好的结果就是在主从服务器上使用相同的SQL模式。

最重要的SQL模式
MySQL中最重要的sql_mode值可能是这些:
.ANSI
这种SQL模式改变语法和行为使其更接近标准SQL。它是一种特定的组合模式列表。

.STRICT_TRANS_TABLES
如果一个值不能以指定的方式插入到一个事务表,终止这个语句。对于非事务表,如果这个值在一个单行记录语句或在多行记录语句第一个出现时终止语句。从MySQL 5.7.5开始缺省的SQL模式包括STRICT_TRANS_TABLES。

.TRADITIONAL
使用MySQL行为像一个传统的SQL数据库系统。简单来说这种模式对于将一个不正确值插入到一个列中时抛出一个错误来代替一个警告。它是特定组合模式中的一种。insert或update一旦发现错误会立即终止。如果您正在使用非事务性存储引擎,这可能不是您想要的,因为在错误之前所做的数据更改可能无法回滚,从而导致“部分完成”的更新。

当提到”strict mode”严格模式时,它意味着是STRICT_TRANS_TABLES或STRICT_ALL_TABLES中的一种或两种都被启用。

完整的SQL模式列表
下面是所有支持的SQL模式列表:
.ALLOW_INVALID_DATES
不对日期执行完全检查。只对月份的范围从1到12和日期的范围从1到31执行检查。这对于Web应用程序非常方便,您可以在三个不同的字段中获得年、月和日,并且希望准确地存储用户插入的内容(不执行日期校验)。这种SQL模式应用于date和datetime列。它不应用于timestamp列,因为它总是请求一个合法的日期值。

这种SQL模式要求月分与每天的取值是合法值,并且范围分别不能超过1到12和1到31。当严格模式被禁用时,一个无效的日期值比如’2014-04-31’将被转换成’0000-00-00’并且生成一个警告。当使用严格模式时,一个无效的日期值会生成一个错误。为了允许这样的日期值,启用ALLOW_INVALID_DATES模式。

.ANSI_QUOTES
将”作为标识引用字符(像`引用字符)而不是字符串引用字符。当使用这种SQL模式时仍然可以使用`作为引用标识符,当ANSI_QUOTES被使用时,不能使用双引号来引用文本字符串,因为它被解析作标识符了。

.ERROR_FOR_DIVISION_BY_ZERO
ERROR_FOR_DIVISION_BY_ZERO模式影响对除以零的处理,这包含MOD(N,0)。对于数据修改操作(insert,update),它的影响也依赖于是否启用了严格SQL模式。
-如果这种模式没有被启用,除以零会插入NULL值并且没有警告。
-如果这种模式被启用,除以零会插入NULL值并生成警告。
-如果这种模式与严格SQL模式被启用,除以堆生成一个错误,除非IGNORE也被指定。对于insert ignore和update ignore,除以零会插入NULL值并生成警告。

对于select语句,除以零会返回NULL值。启用ERROR_FOR_DIVISION_BY_ZERO不管是否启用严格SQL模式会导致生成一个警告。

在MySQL 5.7.4中,ERROR_FOR_DIVISION_BY_ZERO被丢弃,在MySQL 5.7.4到5.7.7中当显式命名时ERROR_FOR_DIVISION_BY_ZERO不会做任何事。代替的是,它的影响被包含在严格SQL模式中。在MySQL 5.7.8和以后的版本中,当显式命名时ERROR_FOR_DIVISION_BY_ZERO会有影响并且没有包含在严格SQL模式中,就像MySQL5.7.4之前的版本一样。然而缺省情况下当严格模式启用下应该与它联合使用。如果ERROR_FOR_DIVISION_BY_ZERO被启用而没有启用严格模式或者当启用严格模式而没有启用ERROR_FOR_DIVISION_BY_ZERO时会出现这个警告。

因为ERROR_FOR_DIVISION_BY_ZERO被丢弃,它将在将来的版本中被删除并作为一个单独的模式名并且它的影响被包含在严格SQL模式。

.HIGH_NOT_PRECEDENCE
NOT操作的优先级就像NOT a between b and c会被解析成NOT (a between b and c)一样。在一些旧版本的MySQL中,表达式会被解析成(NOT a) between b and c。旧版本更高优先级行为可以通过启用HIGH_NOT_PRECEDENCE模式来获得。

mysql> SET sql_mode = '';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> SELECT NOT 1 BETWEEN -5 AND 5;
+------------------------+
| NOT 1 BETWEEN -5 AND 5 |
+------------------------+
|                      0 |
+------------------------+
1 row in set (0.00 sec)

mysql> SET sql_mode = 'HIGH_NOT_PRECEDENCE';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT NOT 1 BETWEEN -5 AND 5;
+------------------------+
| NOT 1 BETWEEN -5 AND 5 |
+------------------------+
|                      1 |
+------------------------+
1 row in set (0.00 sec)

.IGNORE_SPACE
允许在函数名与(符号之间存在空格。这会造成内置函数名被作为保留关键字对待。因此标识符与函数名相同必须有引号。例如,因为有一个count()函数,因此在下面的语句中使用count作为表名就会出错:

mysql> CREATE TABLE count (i INT);
Query OK, 0 rows affected (0.13 sec)

mysql> drop table count cascade;
Query OK, 0 rows affected (0.09 sec)

mysql> SET sql_mode = 'IGNORE_SPACE';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE count (i INT);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'count (i INT)' at line 1

mysql> create table `count` (i INT);
Query OK, 0 rows affected (0.16 sec)

IGNORE_SPACE模式应用到内置函数,而不是用户定义的函数或存储过程。它总是允许在用户定义的函数或存储过程名后有空格而不管是否启用了IGNORE_SAPCE模式。

.NO_AUTO_CREATE_USER
除非指定了身份验证信息,否则将阻止GRANT语句自动创建新用户帐户。这个语句必须使用identified by来指定非空密码或使用identified with来使用一种验证插件。

最好使用create user来创建MySQL账号,然后使用Grant语句。NO_AUTO_CREATE_USER已经被丢弃并且缺省的SQL模式包含了NO_AUTO_CREATE_USER模式。将sql_mode修改为NO_AUTO_CREATE_USER模式会生成一个警告,除了指定sql_mode为DEFAULT.NO_AUTO_CREATE_USER将会在将来的版本中被删除,到时它的影响将会一直被启用。

之前,在NO_AUTO_CREATE_USER被丢弃之前,一个不启用它的原因是它是不安全的复制。现在它可以被启用并且使用create user if not exists,drop user if exists和alter user if exists而不是grant语句来执行安全复制管理。这些语句当从服务器相对于主服务器有不同的授权时可以启用安全复制。

.NO_AUTO_VALUE_ON_ZERO
NO_AUTO_VALUE_ON_ZERO影响对AUTO_INCREMENT列的处理。正常来说通过向访列插入NULL或0来为列生成下一个序列值。NO_AUTO_VALUE_ON_ZERO会抑制0的行为因此只有插入NULL时会生成下一个序列值。如果0已经被存储到表的AUTO_INCREMENT列中这个SQL模式可能是有用的。(存储0是不建议的)例如,如果使用mysqldump来dump表并且然后再加载它,当遇到值0时MySQL正常来说会生成一个新的序列值,因此表的内容不同于被dump的内容。在加载dump文件之前启用NO_AUTO_VALUE_ON_ZERO来解决这个问题。mysqldump现在在它的输出中自动包含一个语句来启用NO_AUTO_VALUE_ON_ZERO来避免这个问题。

.NO_BACKSLASH_ESCAPES
禁用在字符串中使用反斜杠字符(\)作为转义字符。启用此模式后,反斜杠将成为与其他字符一样的普通字符。

.NO_DIR_IN_CREATE
创建表时,忽略所有索引目录和数据目录指令。此选项在从复制服务器上非常有用。

.NO_ENGINE_SUBSTITUTION
当一个语句比如create table或alter table指定一个存储引擎已经被禁用或没有被编译时控制缺省存储引擎的自动替换。缺省的SQL模式中包含了NO_ENGINE_SUBSTITUTION。因为存储引擎可以在运行时被附加进来,不可以存储引擎也以相同方式被对待:
当NO_ENGINE_SUBSTITUTION被禁用,对于create table的缺省存储引擎被使用并且如果期待的存储引擎不可用会出现一个警告。对于alter table,会出现一个警告并且表不能被修改。
当NO_ENGINE_SUBSTITUTION被启用时,如果期待的存储引擎不可用会出现一个警告并且表不会被创建或被修改。

.NO_FIELD_OPTIONS
在show create table输出中不打印特定MySQL列选项。这种SQL模式被mysqldump以可移植模式来使用。

.NO_KEY_OPTIONS
在show create table输出中不打印特定MySQL索引选项。这种SQL模式被mysqldump以可移植模式来使用。

.NO_TABLE_OPTIONS
在show create table输出中不打印特定MySQL表选项(比如ENGINE)。这种SQL模式被mysqldump以可移植模式来使用。

.NO_UNSIGNED_SUBTRACTION
两个整数相减,这里一种类型UNSIGNED,缺省情况下生成一个没有符号的结果。如果结果出现负数将会出现错误:

mysql> SET sql_mode = '';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> SELECT CAST(0 AS UNSIGNED) - 1;
ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(cast(0 as unsigned) - 1)'

如果NO_UNSIGNED_SUBTRACTION模式被启用,结果将是负数:

mysql> SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT CAST(0 AS UNSIGNED) - 1;
+-------------------------+
| CAST(0 AS UNSIGNED) - 1 |
+-------------------------+
|                      -1 |
+-------------------------+
1 row in set (0.00 sec)

如果使用此类操作的结果更新无符号整数列,则将该结果裁剪为该列类型的最大值,如果启用no_unsigned_subtract,则将其裁剪为0。如果严格SQL模式被启用,则会出现错误并且列会保持不变。

当no_unsigned_subtraction被启用时,就算任何操作数据是无符号的相减的结果是有符号的。例如比较表t1中的c2列与表t2中的c2列:

mysql> SET sql_mode='';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE test (c1 BIGINT UNSIGNED NOT NULL);
Query OK, 0 rows affected (0.20 sec)


mysql> CREATE TABLE t1 SELECT c1 - 1 AS c2 FROM test;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| c2    | bigint(21) unsigned | NO   |     | 0       |       |
+-------+---------------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> SET sql_mode='NO_UNSIGNED_SUBTRACTION';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE t2 SELECT c1 - 1 AS c2 FROM test;
Query OK, 0 rows affected (0.25 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t2;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| c2    | bigint(21) | NO   |     | 0       |       |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)

这意味着bigint unsigned在所有上下文中不是100%可以使用。

.NO_ZERO_DATE
NO_ZERO_DATE模式影响服务器是否允许’0000-00-00’作为一种有效的日期。它的影响也依赖于是否启用了严格SQL模式。
-如果模式没有被启用,’0000-00-00’被允许并且插入不会产生警告。
-如果模式被启用,’0000-00-00’被允许并且插入会产生警告。
-如果模式和严格SQL模式被启用,’0000-00-00’不被允许并且插入会出现错误,除非指定IGNORE选项。对于insert ignore和update ignore来说,’0000-00-00’被允许并且插入会产生警告。

在MySQL 5.7.4中,NO_ZERO_DATE被丢弃。在MySQL 5.7.4到5.7.7中,NO_ZERO_DATE当显式指定时不会工作。代替地是它的影响已经被包含在严格SQL模式中。在MySQL 5.7.8和以后的版本中,当显式地指定NO_ZERO_DATE模式时它会工作并且它不是严格SQL模式的一部分就像MySQL5.7.4版本之前作用一样。然而,缺省情况下它应该与严格SQL模式联合使用。如果启用NO_ZERO_DATE而没有启用严格SQL模式就是出现警告或者反之亦然。

因为NO_ZERO_DATE已经被丢弃,因此它会在将来的版本中作为单独的模式名被删除并且它的影响会包含在严格SQL模式中。

.NO_ZERO_IN_DATE
NO_ZERO_IN_DATE模式影响服务器是否允许日期中的年部分为非0但月或日部分为0。(这种模式影响日期比如’2010-00-01’或’2010-01-00’,而不是’0000-00-00’。为了控制服务器是否允许’0000-00-00’,使用NO_ZERO_DATE模式)。NO_ZERO_IN_DATE模式的影响也依赖于是否启用了严格SQL模式。
-如果这种模式没有启用,有为0部分折日期被允许并且插入不产生警告。
-如果这种模式被启用,有为0部分的日期值将以’0000-00-00’格式被插入并且生成一个警告。
-如果这种模式与严格SQL模式被启用,有为0部分的日期不被允许并且插入会产生一个错误,除非你指定ignore。对于insert ignore和update ignore来说,有为0部分的日期将以’0000-00-00’格式被插入并且生成一个警告。

在MySQL 5.7.4中,NO_ZERO_IN_DATE被丢弃。在MySQL 5.7.4到5.7.7中,NO_ZERO_DATE当显式指定时不会工作。代替地是它的影响已经被包含在严格SQL模式中。在MySQL 5.7.8和以后的版本中,当显式地指定NO_ZERO_IN_DATE模式时它会工作并且它不是严格SQL模式的一部分就像MySQL5.7.4版本之前作用一样。然而,缺省情况下它应该与严格SQL模式联合使用。如果启用NO_ZERO_IN_DATE而没有启用严格SQL模式就是出现警告或者反之亦然。

因为NO_ZERO_IN_DATE已经被丢弃,因此它会在将来的版本中作为单独的模式名被删除并且它的影响会包含在严格SQL模式中。

.ONLY_FULL_GROUP_BY
拒绝那些select list、HAVING condition或ORDER BY list引用非聚合列的查询,这些列既不在GROUP BY子句中命名,也不依赖于GROUP BY列(由GROUP BY列唯一确定)。

从MySQL 5.7.5开始,缺省的SQL模式包含ONLY_FULL_GROUP_BY模式。(在5.7.5之前,MySQL没有检测到功能依赖并且缺省情况下ONLY_FULL_GROUP_BY模式没有启用。

MySQL扩展了标准SQL来允许在having子句中引用select列表中的别名表达式。在MySQL 5.7.5之前,启用ONLY_FULL_GROUP_BY模式会禁用这种扩展,因此要求having子句以非别名表达式来书写。从MySQL5.7.5开始,这种限制被取消了因此having子句可以引用别名而不用管ONLY_FULL_GROUP_BY模式是否启用了。

.PAD_CAHR_TO_FULL_LENGTH
默认情况下,在检索时从CHAR列值中裁剪尾随空格。如果PAD_CHAR_TO_FULL_LENGTH被启用,裁剪不会发生并且在检索CHAR列值时填充到它的完整长度。这种模式不应用于varchar列。检索时为其保留尾随空格。

mysql> CREATE TABLE t1 (c1 CHAR(10));
Query OK, 0 rows affected (0.12 sec)

mysql> INSERT INTO t1 (c1) VALUES('xy');
Query OK, 1 row affected (0.03 sec)

mysql> SET sql_mode = '';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> SELECT c1, CHAR_LENGTH(c1) FROM t1;
+------+-----------------+
| c1   | CHAR_LENGTH(c1) |
+------+-----------------+
| xy   |               2 |
+------+-----------------+
1 row in set (0.00 sec)

mysql> SET sql_mode = 'PAD_CHAR_TO_FULL_LENGTH';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT c1, CHAR_LENGTH(c1) FROM t1;
+------------+-----------------+
| c1         | CHAR_LENGTH(c1) |
+------------+-----------------+
| xy         |              10 |
+------------+-----------------+
1 row in set (0.00 sec)

.PIPES_AS_CONCAT
将||作为字符串连接操作符(与concat()一样)而不是作为OR的同义词。

.REAL_AS_FLOAT
将REAL作为FLOAT的同义词。缺省情况下,MySQL将REAL作为DOUBLE的同义词。

.STRICT_ALL_TABLES
对所有的存储引擎启用严格SQL模式。无效的数据会被拒绝。从MySQL 5.7.4到5.7.7,STRICT_ALL_TABLES模式包含了ERROR_FOR_DIVISION_BY_ZERO,NO_ZERO_DATE和NO_ZERO_IN_DATE模式的影响。

.STRICT_TRANS_TABLES
对事务型存储引擎启用严格SQL模式并且在可能的情况下使用非事务型存储引擎。从MySQL 5.7.4到5.7.7,STRICT_TRANS_TABLES模式包含了ERROR_FOR_DIVISION_BY_ZERO,NO_ZERO_DATE和NO_ZERO_IN_DATE模式的影响。

mysqld –skip-grant-tables

mysqld的–skip-grant-tables选项
这个选项会导致不使用权限系统来启动服务器,它将让任何用户可以访问服务器并且不受限制的访问所有数据库。在不使用授权表启动服务器后可以通过shell来执行mysqladmin flush-privileges或mysqladmin reload命令或者在连接到服务器后执行flush privileges语句来让正在运行的服务器再次使用授权表。

使用–skip-grant-tables选项启动服务器

[root@localhost mysql]# service mysqld stop
Shutting down MySQL. SUCCESS! 


[root@localhost mysql]# service mysqld start --skip-grant-tables
Starting MySQL.. SUCCESS! 

现在就可以不使用用户和密码就可以登录服务器

[mysql@localhost ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log Source distribution

Copyright (c) 2000, 2019, 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>

现在可以执行mysqladin flush-privileges命令让正在运行的服务器再次使用授权表

[mysql@localhost ~]$ mysqladmin  flush-privileges

现在不使用用户和密码就不能登录服务器了,必须使用用户和密码才能登录了

[mysql@localhost ~]$ mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


[mysql@localhost ~]$ mysql -uroot -pabcd mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.26-log Source distribution

Copyright (c) 2000, 2019, 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.

再次使用–skip-grant-tables选项启动服务器

[root@localhost mysql]# service mysqld stop
Shutting down MySQL.. SUCCESS! 

[root@localhost mysql]# service mysqld start --skip-grant-tables
Starting MySQL.. SUCCESS! 

现在就可以不使用用户和密码就可以登录服务器

[mysql@localhost ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log Source distribution

Copyright (c) 2000, 2019, 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>

现在可以执行mysqladin reload命令让正在运行的服务器再次使用授权表

mysql@localhost ~]$ mysqladmin reload

现在不使用用户和密码就不能登录服务器了,必须使用用户和密码才能登录了

[mysql@localhost ~]$ mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[mysql@localhost ~]$ mysql -uroot -pabcd mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.26-log Source distribution

Copyright (c) 2000, 2019, 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.

再次使用–skip-grant-tables选项启动服务器

[root@localhost mysql]# service mysqld stop
Shutting down MySQL. SUCCESS! 


[root@localhost mysql]# service mysqld start --skip-grant-tables
Starting MySQL.. SUCCESS! 

现在就可以不使用用户和密码就可以登录服务器

[mysql@localhost ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log Source distribution

Copyright (c) 2000, 2019, 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>

现在可以执行flush privileges语句让正在运行的服务器再次使用授权表

mysql> flush privileges;
Query OK, 0 rows affected (0.12 sec)

现在不使用用户和密码就不能登录服务器了,必须使用用户和密码才能登录了

[mysql@localhost ~]$ mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[mysql@localhost ~]$ mysql -uroot -pabcd mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.26-log Source distribution

Copyright (c) 2000, 2019, 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.

–skip-grant-tables选项也可以在选项文件my.cnf中进行设置。这个选项还会导致服务器在启动过程中禁止加载用户定义函数(udf),调度事件和安装插件语句中安装的插件。为了以任何方式来加载插件,使用–plugin-load选项。–skip-grant-tables选项也会导致disabled_storage_engines系统变量失效。

flush privileges语句可以在服务器启动后通过执行其它操作来隐式执行。例如在升级过程中mysql_upgrade程序就会刷新权限。

mysqldump+mysqlbinlog执行备份与还原

服务器的二进制日志文件由用来描述修改数据库内容的事件组成。服务器以二进制方式来写这些文件。为了以文本格式来显示这些内容,可以使用mysqlbinlog工具。也可以使用mysqlbinlog来显示在复制环境中由从从slave服务器所写入中relay日志文件中的内容,因为其格式与二进制日志文件格式一样。
mysqlbinlog的使用语法如下:
Usage: mysqlbinlog [options] log-files

下面的命令用来显示名为binlog.000001的二进制日志文件的内容:

[mysql@localhost ~]$ mysqlbinlog  /mysqldata/mysql/binlog.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#191115 15:39:01 server id 1  end_log_pos 123 CRC32 0x2d9d7b4f  Start: binlog v 4, server v 5.7.26-log created 191115 15:39:01 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
FVbOXQ8BAAAAdwAAAHsAAAABAAQANS43LjI2LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAVVs5dEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AU97nS0=
'/*!*/;
# at 123
#191115 15:39:01 server id 1  end_log_pos 154 CRC32 0x42dcd61c  Previous-GTIDs
# [empty]
# at 154
#191115 15:51:15 server id 1  end_log_pos 219 CRC32 0x5bc0b021  Anonymous_GTID  last_committed=0        sequence_number=1       rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#191115 15:51:15 server id 1  end_log_pos 308 CRC32 0x7261eacb  Query   thread_id=2     exec_time=0     error_code=0
use `mysql`/*!*/;
SET TIMESTAMP=1573804275/*!*/;
SET @@session.pseudo_thread_id=2/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C gb2312 *//*!*/;
SET @@session.character_set_client=24,@@session.collation_connection=24,@@session.collation_server=45/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
truncate table person
/*!*/;
# at 308
#191115 15:51:38 server id 1  end_log_pos 373 CRC32 0x6d2e39aa  Anonymous_GTID  last_committed=1        sequence_number=2       rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 373
#191115 15:51:38 server id 1  end_log_pos 454 CRC32 0x7871c2ea  Query   thread_id=2     exec_time=0     error_code=0
SET TIMESTAMP=1573804298/*!*/;
BEGIN
/*!*/;
# at 454
# at 486
#191115 15:51:38 server id 1  end_log_pos 486 CRC32 0xb746cd30  Intvar
SET INSERT_ID=1/*!*/;
#191115 15:51:38 server id 1  end_log_pos 654 CRC32 0x0e926042  Query   thread_id=2     exec_time=0     error_code=0
SET TIMESTAMP=1573804298/*!*/;
insert into person(last_name,first_name,birth,death) values('yong','jing','1985-02-28',null)
/*!*/;
# at 654
#191115 15:51:38 server id 1  end_log_pos 736 CRC32 0xc5450308  Query   thread_id=2     exec_time=0     error_code=0
SET TIMESTAMP=1573804298/*!*/;
COMMIT
/*!*/;
# at 736
#191115 15:51:45 server id 1  end_log_pos 801 CRC32 0xc2c892b8  Anonymous_GTID  last_committed=2        sequence_number=3       rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 801
#191115 15:51:45 server id 1  end_log_pos 882 CRC32 0x51a9cd5c  Query   thread_id=2     exec_time=0     error_code=0
SET TIMESTAMP=1573804305/*!*/;
BEGIN
/*!*/;
# at 882
# at 914
#191115 15:51:45 server id 1  end_log_pos 914 CRC32 0x40a98fae  Intvar
SET INSERT_ID=2/*!*/;
#191115 15:51:45 server id 1  end_log_pos 1082 CRC32 0x3396c40d         Query   thread_id=2     exec_time=0     error_code=0
SET TIMESTAMP=1573804305/*!*/;
insert into person(last_name,first_name,birth,death) values('yan','huang','1990-08-25',null)
/*!*/;
# at 1082
#191115 15:51:45 server id 1  end_log_pos 1164 CRC32 0xf6f6efad         Query   thread_id=2     exec_time=0     error_code=0
SET TIMESTAMP=1573804305/*!*/;
COMMIT
/*!*/;
# at 1164
#191115 15:51:53 server id 1  end_log_pos 1229 CRC32 0x55b50dbe         Anonymous_GTID  last_committed=3        sequence_number=4       rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1229
#191115 15:51:53 server id 1  end_log_pos 1310 CRC32 0xd0f6a335         Query   thread_id=2     exec_time=0     error_code=0
SET TIMESTAMP=1573804313/*!*/;
BEGIN
/*!*/;
# at 1310
# at 1342
#191115 15:51:53 server id 1  end_log_pos 1342 CRC32 0xfad94baf         Intvar
SET INSERT_ID=3/*!*/;
#191115 15:51:53 server id 1  end_log_pos 1508 CRC32 0x26c5b3bb         Query   thread_id=2     exec_time=0     error_code=0
SET TIMESTAMP=1573804313/*!*/;
insert into person(last_name,first_name,birth,death) values('yali','ye','1994-12-23',null)
/*!*/;
# at 1508
#191115 15:51:53 server id 1  end_log_pos 1590 CRC32 0xbb6a2b4c         Query   thread_id=2     exec_time=0     error_code=0
SET TIMESTAMP=1573804313/*!*/;
COMMIT
/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

上面输出了binlog.000001二进制日志文件中所包含的内容。对于基于语句的日志,事件信息包括SQL语句,执行语句的服务器ID,语句被执行的时间戳,执行时间等。对于基于行记录的日志,事件信息指示行的改变而不是SQL语句。

# at 486
#191115 15:51:38 server id 1  end_log_pos 486 CRC32 0xb746cd30  Intvar SET INSERT_ID=1/*!*/;
#191115 15:51:38 server id 1  end_log_pos 654 CRC32 0x0e926042  Query   thread_id=2
exec_time=0     error_code=0 SET TIMESTAMP=1573804298/*!*/;

第一行,at后面的数字指示事件在二进制日志文件中的偏移量或开始位置。

第二行是以日期和时间开始指示语句开始执行的时间。对于复制来说,这个时间戳将传播到从属服务器。server id是事件起源服务器的server_id值。end_log_pos指示下一个事件开始的位置(它是当前事件的终止位置+1)。thread_id那个线程来执行这个事件。exec_time是在主服务器上执行事件所花费的时间。在从属服务器上,它是从属服务器上执行结束时间减去主服务器上的执行开始时间的差值。这种差值可以作为一种指示来表示复制进程落后于主服务器多长时间。error_code指示执行事件的结果。零意味着没有出现错误。

mysqlbinglog的输出可以用来重新执行日志文件中的语句(例如,通过使用mysql工具)。这在服务器崩溃时用来恢复是很有用的。

正常来说,使用mysqlbinlog直接读取二进制日志文件并应用它们到本地MySQL服务器。它也可以通过使用–read-from-remote-server选项来从远程服务器上读取二进制日志文件。为了读取远程二进制日志文件,连接参数选项可以被指定用来指示如何连接服务器。这些选项有–host,–password,–port,–protocol,–socket和–user,除非使用了–read-from-remote-server选项否则它们会被忽略。

当对一个大的二进制日志文件执行mysqlbinlog时,要小心注意文件系统是否有足够的空间来存储结果文件。为了配置目录给mysqlbinlog临时使用存储文件,使用TMPDIR环境变量。

使用mysqlbinlog备份二进制日志文件
缺省情况下,mysqlbinlog读取二进制日志文件并以文本格式来显示它的内容。这能让你更容易使用文件来检查发生的事件和重新执行它们(例如,通过使用输出作为mysql的输入)。mysqlbinlog可以直接从本地文件系统中读取日志文件或者使用–read-from-remote-server选项来连接远程服务器并从远程服务器上读取二进制日志文件。mysqlbinlog以文本格式将内容输出到标准输出,或者如果指定了–result-file=file_name选项会将内容写入文件。

mysqlbinlog可以读了二进制日志文件并将其包含的内容以二进制格式而不是文本格式写入新文件。这种能力可以让你以原来的格式来备份二进制日志文件。mysqlbinlog可以生成静态备份,在备份一组日志文件时当备份完最后的文件时而停止。它也可以生成一种连续(live)备份,当备份到最后的日志文件时仍然保持对服务器的连接并当生成新的事件时继续复制新的事件。在连续备份操作时,mysqlbinlog会运行到连接中断为止(比如,服务器退出)或mysqlbinlog被强制中断为止。当连接中断,mysqlbinlog不会进行等待并重新进行连接,不像从属复制服务器那样。为了在服务器重启之后继续一个live备份,必须重新启动mysqlbinlog。

二进制日志文件备份要求在调用mysqlbinlog时最少要使用两个选项:
.–read-from-remote-server(或-R)选项来告诉mysqlbinlog连接到一个服务器并读取它的二进制日志文件(这类似于一个从属复制服务器连接到它的主服务器).

.–raw选项告诉mysqlbinlog以原始(二进制)格式输出,而不是文本格式。

与–read-from-remote-server一起通常还指定其它选项:–host指示服务器运行在哪里,并且可能需要指定连接选项–user和password。

与–raw联合使用的几个其它选项:.–stop-never:在读取到最后日志文件后保持对服务器的连接并继续读取新的事件。

.–stop-never-slave-server-id=id:当–stop-never被使用时mysqlbinlog报告的服务器ID,缺省值65535。这可以避免与从属服务器或其它的mysqlbinlog进程的ID冲突。

.–result-file:输出文件名的前缀

为了使用mysqlbinlog来备份服务器的二进制日志文件,你必须指定在服务器上真实存在的文件名。如果你不知道文件名,连接到服务器并使用show binary logs语句来查看当前的日志文件名。

mysql> show binary logs;
+---------------+-----------+
| Log_name      | File_size |
+---------------+-----------+
| binlog.000001 |      2530 |
+---------------+-----------+
1 row in set (0.00 sec)

使用这些信息可以使用mysqlbinlog来备份二进制日志文件到当前目录:
为了对binlog.000130到binlog.000132的日志文件进行静态备份,使用以下命令:
mysqlbinlog –read-from-remote-server –host=host_name –raw
binlog.000130 binlog.000131 binlog.000132

mysqlbinlog –read-from-remote-server –host=host_name –raw
–to-last-log binlog.000130

第一个命令显式指定每个文件名。第二个只指定了第一个日志文件并使用了–to-last-log来读取到最后一个日志文件。在这些命令之间的差异是在mysqlbinlog到达binlog.000132的末尾之前如果服务器打开了binlog.000133文件,第一个命令将不会读取,但第二个命令会读取。

为了进行live备份mysqlbinlog从binlog.000130开始备份现有的日志文件,然后保持对服务器的连接来复制生成的新事件:
mysqlbinlog –read-from-remote-server –host=host_name –raw
–stop-never binlog.000130

使用–stop-never选项,不需要指定–to-last-log来读取最后的日志文件因为这个选项是隐含的

输出文件名
在没有使用–raw选项时,mysqlbinlog会生成文本格式的输出,如果指定–result-file选项,指定将所有输出写入一个文件中。使用–raw选项时,mysqlbinlog会将服务器的每个日志文件转换成一个二进制输出文件。缺省情况下,mysqlbinlog会在当前目录中生成与源日志文件同名的文件。为了修改输出文件名,使用–result-file选项。与–raw联合使用,–result-file选项值将作为前缀来命名输出文件名。

现在对远程服务器中的binlog.000001日志文件进行备份

[mysql@localhost ~]$ mysqlbinlog --read-from-remote-server --host=192.168.1.250 --raw binlog.000001 --result-file=jy_
[mysql@localhost ~]$ ls -lrt
-rw-r-----. 1 mysql mysql    2530 11月 22 10:24 jy_binlog.000001

可以看到备份的日志文件为以jy_为前缀,其文件名为jy_binlog.000001

使用mysqldump与mysqlbinlog执行备份与还原操作
下面将介绍一个简单的例子显示如何使用mysqldump与mysqlbinlog一起来备份MySQL服务器的数据和二进制日志文件以及在数据丢失时如何使用备份来还原数据。

现在主机上的MySQL服务器的第一个二进制日志文件为binlog.000001

mysql> show binary logs;
+---------------+-----------+
| Log_name      | File_size |
+---------------+-----------+
| binlog.000001 |      2530 |
+---------------+-----------+
1 row in set (0.01 sec)

使用mysqlbinlog来对二进制日志文件执行连续备份:

[mysql@localhost ~]$ mysqlbinlog --read-from-remote-server --host=192.168.1.250 --raw  --stop-never binlog.000001

[mysql@localhost ~]$ ls -lrt

-rw-r-----. 1 mysql mysql    2530 11月 22 10:38 binlog.000001

创建了一个名为t的测试表并插入了三行记录

mysql> select * from t;
+----+----------+------+
| id | name     | date |
+----+----------+------+
|  1 | jingyong | NULL |
|  2 | yeyali   | NULL |
|  3 | huangyan | NULL |
+----+----------+------+
3 rows in set (0.00 sec)

使用mysqldump来创建一个dump文件作为对MySQL服务器的数据快照。使用–all-databases,–events和–routines来备份所有的数据,–master-data=2用来指示在dump文件中包括当前的二进制日志文件。

[mysql@localhost ~]$ mysqldump --host=192.168.1.250 --port=3306 -uroot -pabcd --all-databases --events --routines --master-data=2> dump_mysql.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[mysql@localhost ~]$ ls -lrt
-rw-r--r--. 1 mysql mysql 3290497 11月 22 10:51 dump_mysql.sql

现在删除mysql库中的表t

mysql> drop table t;
Query OK, 0 rows affected (0.18 sec)

mysql> desc t;
ERROR 1146 (42S02): Table 'mysql.t' doesn't exist

现在假设mysql库中的表t丢失了,使用最近的dump文件来还原数据:

[mysql@localhost ~]$ mysql --host=192.168.1.250 --port=3306 -uroot -pabcd  mysql

还原数据后mysql.t表就恢复了

mysql> desc t;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(20) | NO   |     | NULL    |       |
| date  | date        | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> select * from t;
+----+----------+------+
| id | name     | date |
+----+----------+------+
|  1 | jingyong | NULL |
|  2 | yeyali   | NULL |
|  3 | huangyan | NULL |
+----+----------+------+
3 rows in set (0.00 sec)

现在向mysql.t表中插入一条记录并删除这条记录,然后使用备份的二进制日志文件来重新执行事件来恢复这条记录

mysql> insert into t value(4,'wenyao',NULL);
Query OK, 1 row affected (0.03 sec)

mysql> select * from t;
+----+----------+------+
| id | name     | date |
+----+----------+------+
|  1 | jingyong | NULL |
|  2 | yeyali   | NULL |
|  3 | huangyan | NULL |
|  4 | wenyao   | NULL |
+----+----------+------+
4 rows in set (0.00 sec)

mysql> delete from t where id=4;
Query OK, 1 row affected (0.13 sec)

mysql> select * from t;
+----+----------+------+
| id | name     | date |
+----+----------+------+
|  1 | jingyong | NULL |
|  2 | yeyali   | NULL |
|  3 | huangyan | NULL |
+----+----------+------+
3 rows in set (0.01 sec)

我们需要找到插入这条记录在日志文件中的开始与结束的位置

# at 3306211
#191122 11:04:34 server id 1  end_log_pos 3306323 CRC32 0x88f89864      Query   thread_id=11    exec_time=0     error_code=0
SET TIMESTAMP=1574391874/*!*/;
insert into t value(4,'wenyao',NULL)
/*!*/;
# at 3306323
#191122 11:04:34 server id 1  end_log_pos 3306354 CRC32 0x966500de      Xid = 1041
COMMIT/*!*/;
# at 3306354
#191122 11:07:26 server id 1  end_log_pos 3306419 CRC32 0x1f3e6e28      Anonymous_GTID  last_committed=160      sequence_number=161     rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 3306419
#191122 11:07:26 server id 1  end_log_pos 3306500 CRC32 0x883ecef4      Query   thread_id=11    exec_time=0     error_code=0
SET TIMESTAMP=1574392046/*!*/;
BEGIN
/*!*/;
# at 3306500
#191122 11:07:26 server id 1  end_log_pos 3306600 CRC32 0xecae0a57      Query   thread_id=11    exec_time=0     error_code=0
SET TIMESTAMP=1574392046/*!*/;
delete from t where id=4

从上面的日志文件内容可以看到插入的开始位置为3306211,结束位置为3306323

现在当前备份的二进制日志文件名为binlog.000001,重新执行事件的命令如下:

[mysql@localhost ~]$ mysqlbinlog --start-position=3306211 --stop-position=3306323 binlog.000001 | mysql --host=192.168.1.250 --port=3306 -uroot -pabcd  mysql
mysql: [Warning] Using a password on the command line interface can be insecure.

现在检查mysql.t表中的记录,可以看到被删除的这条记录恢复了。

mysql> select * from t;
+----+----------+------+
| id | name     | date |
+----+----------+------+
|  1 | jingyong | NULL |
|  2 | yeyali   | NULL |
|  3 | huangyan | NULL |
|  4 | wenyao   | NULL |
+----+----------+------+
4 rows in set (0.00 sec)

设置msyqlbinlog 服务器ID
在使用–read-from-remote-server选项来调用mysqlbinlog时,mysqlbinlog会连接到一个MySQL服务器,指定了一个服务器ID来标识它并且从该服务器获取所需要的二进制日志文件。可以使用mysqlbinlog以以下几种方式来从服务器中获取日志文件:
.对文件集指定显式的名字。对每个文件,mysqlbinlog会执行连接操作并执行binlog dump命令。服务器会发送文件并断开连接。每个文件都有一个连接。

.指定开始文件与–to-last-log选项,mysqlbinlog会执行连接并对所有的日志文件执行binlog dump命令。服务器会发送所有日志文件并断开连接

.指定开始文件与–stop-never选项(隐式实现–to-last-log选项的功能),mysqlbinlog会执行连接并对所有日志文件执行binlog dump命令。服务器会发送所有日志文件,但在发送最后一个日志文件后不会断开与服务器的连接。

只有使用–read-from-remote-server选项时,mysqlbinlog使用一个为0的server ID进行连接,它将告诉服务器在发送所请求的日志文件后断开连接。

使用–read-from-remote-server与–stop-never选项时,mysqlbinlog将使用一个非0的server ID进行连接,因此在最后的日志文件发送之后服务器不会断开连接。缺省的server ID为65535,但这个可以通过使用–stop-never-slave-server-id选项来修改。

因此,对于使用前两种方式来获取日志文件人,因为mysqlbinlog指定的server ID为0,所有服务器会断开连接,如果–stop-never选项被指定因为mysqlbinlog指定一个非0的server ID,所以服务器将不会断开连接。

 

mysql_config_editor 配置工具

mysql_config_editor工具能让你在一种加密的登录路径文件.mylogin.cnf中存储审核身份信息。在Windows中这个文件存储在%APPDATA%\MySQL目录中,在非Windows平台上存储在当前用户的home目录中。这种文件可以被MySQL客户端程序读取来获得连接MySQL服务器的审核身份信息。

没有加密的.mylogin.cnf登录路径文件由选项组组成。类似于其它的选项文件。在.mylogin.cnf文件中的每个选项组叫作”login path”登录路径,它是只允许特定选项的组:host,user,password,port和socket。可以把一个登录路径选项组认为是一组选项来指定使用那个用户来连接那个MySQL服务器的信息。下面是没有加密的登录路径信息的
例子:

[client]
user = mydefaultname
password = mydefaultpass
host = 127.0.0.1
[mypath]
user = myothername
password = myotherpass
host = localhost

当调用一个客户端程序连接MySQL服务器时,客户端使用.mylogin.cnf并结合其它的选项文件。它的优先级比其它的选项文件要高,但比在客户端命令行中显式指定的要低。

为了指定一个替代的登录路径文件名,设置MYSQL_TEST_LOGIN_FILE环境变量。这种变量通过mysql_config_editor,通过标准的MySQL客户端(mysql,mysqladmin等)工具和mysql-test-run.pl测试工具所识别。

程序以以下方式使用登录路径文件中的选项组:
.mysql_config_editor在你没有通过–login-path=name选项来显式指定登录路径时缺省情况下会使用client选项组。

.在没有使用–login-path选项的情况下,客户端程序将像从其它选项文件中读取信息一样从登录路径文件中读取选项组。比如:
shell>mysql
缺省情况下,mysql客户端程序将从其它的选项文件中读取[client]和[mysql]选项组,因此也会从登录路径文件中读取这些信息。

.使用–login-path选项,客户端程序额外从登录路径文件中讯取命名的登录路径。仍然与读取其它选项文件中的选项组一样。比如:
shell>mysql –login-path=mypath

mysql客户端程序将从其它选项文件中读取[client]和[mysql]选项组信息和从登录路径文件中读取[client]和[mysql]选项组信息。

.即使当–no-defaults选项被使用,客户端程序也会读取登录路径文件。这允许使用一种安全的方式来指定密码而不而在命令行中指定。

mysql_config_editor会对.mylogin.cnf文件进行加密因此它不能以明文方式被读取,并且当客户端程序解密时,它的内容只在内存中使用。通过这种方式,密码可以以非明文格式存储在文件中并且在以后的命令行或环境变量需要使用时不需要提供输入密码。mysql_config_editor提供了一个print命令来显示登录路径文件的内容,但即使在这种情况下,密码值也会被隐藏,这样就不会以其他用户可以看到的方式出现

通过mysql_config_editor加密阻止密码以明文方式出现在.mylogin.cnf文件中并通过阻止无意暴露密码提供了一种安全措施。例如,如果你在屏幕上以非加密方式来显示my.cnf选项文件中的信息时,它包含的任何密码对于任何人都是可见的。使用.mylogin.cnf文件不是这种情况。但是使用的加密不会阻止一个有决心的攻击者,你不应该认为它是不可攻破的。如果用户能够获得您机器上的系统管理权限来访问您的文件,那么他可以轻松地解密.mylogin.cnf文件

登录路径文件必须对当前用户可读和可写并且对其它用户来说不可以访问。否则,mysql_config_editor会忽略它,并且客户端程序不会使用它。

mysql_config_editor语法:

shell>mysql_config_editor [program options] [command [command options]]

如果登录路径文件不存在,mysql_config_editor会创建它。

mysql_config_editor命令有以下参数选项:
.program_options由通用的mysql_config_editor选项组成。

.command指示对.mylogin.cnf登录路径文件执行的操作。例如,set将写一个登录路径到文件中,remove将删除一个登录路径,print显示登录路径内容。

.command_options指示任何指定给命令的额外选项,比如登录路径名和登录路径所使用的值。

命令名在程序参数集中的位置很重要。例如,这些命令行具有相同的参数,但产生不同的结果:

[mysql@localhost ~]$ mysql_config_editor --help set
mysql_config_editor Ver 1.0 Distrib 5.7.26, for Linux on x86_64
Copyright (c) 2012, 2019, 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.

MySQL Configuration Utility.
Usage: mysql_config_editor [program options] [command [command options]]
  -#, --debug[=#]     This is a non-debug version. Catch this and exit.
  -?, --help          Display this help and exit.
  -v, --verbose       Write more information.
  -V, --version       Output version information and exit.

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- ----------------------------------------
verbose                           FALSE

Where command can be any one of the following :
       set [command options]     Sets user name/password/host name/socket/port
                                 for a given login path (section).
       remove [command options]  Remove a login path from the login file.
       print [command options]   Print all the options for a specified
                                 login path.
       reset [command options]   Deletes the contents of the login file.
       help                      Display this usage/help information.

[mysql@localhost ~]$ mysql_config_editor set --help
mysql_config_editor Ver 1.0 Distrib 5.7.26, for Linux on x86_64
Copyright (c) 2012, 2019, 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.

MySQL Configuration Utility.

Description: Write a login path to the login file.
Usage: mysql_config_editor [program options] [set [command options]]
  -?, --help          Display this help and exit.
  -h, --host=name     Host name to be entered into the login file.
  -G, --login-path=name 
                      Name of the login path to use in the login file. (Default
                      : client)
  -p, --password      Prompt for password to be entered into the login file.
  -u, --user=name     User name to be entered into the login file.
  -S, --socket=name   Socket path to be entered into login file.
  -P, --port=name     Port number to be entered into login file.
  -w, --warn          Warn and ask for confirmation if set command attempts to
                      overwrite an existing login path (enabled by default).
                      (Defaults to on; use --skip-warn to disable.)

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- ----------------------------------------
host                              (No default value)
login-path                        client
user                              (No default value)
socket                            (No default value)
port                              (No default value)
warn                              TRUE

第一个命令行显示了通用的mysql_config_editor的帮助信息并且忽略了set命令。第二个命令行是显示了set命令的特定帮助信息。

假设想要建立一个client登录路径来定义你的缺省连接参数和额外的remote登录路径来连接远程服务器。想要记录以下信息:
.缺省情况,连接到本地服务器的用户名与密码为root和xxzx7817600
.连接到远程服务器的用户名与密码为root和123456

为了在.mylogin.cnf文件中设置登录路径,使用下面的set命令。每个命令单独一行执行并且根据提示输入相关的密码:

[mysql@localhost ~]$ mysql_config_editor set --login-path=client --host=192.168.1.250 --user=root --password
Enter password: 
[mysql@localhost ~]$ mysql_config_editor set --login-path=remote --host=192.168.1.251 --user=root --password --port=33306
Enter password: 
[mysql@localhost ~]$ 

mysql_config_editor缺省情况下使用client登录路径,因此–login-path=client选项从第一个命令中可以被忽略而不会产生影响。

为了查询mysql_config_editor写入.mylogin.cnf文件的内容,执行print命令:

[mysql@localhost ~]$ mysql_config_editor print --all
[client]
user = root
password = *****
host = 192.168.1.250
[remote]
user = root
password = *****
host = 192.168.1.251
port = 33306

print命令以一组行集合来显示每个登录路径,在方括号中的选项组头指示了登录路径名,接着是登录路径的选项值。密码值以星号出现不是以明文来显示。

如果你在执行print命令时不指定–all选项来显示所有的登录路径或不使用–login-path=name来显示指定的登录路径,如果存会client登录路径,那么缺省情况下print命令只会显示client登录路径。

[mysql@localhost ~]$ mysql_config_editor print
[client]
user = root
password = *****
host = 192.168.1.250

通过上面的例子可以看到一个登录路径文件可以包含多个登录路径。使用这种方式,mysql_config_editor可以简单地多个个性化的登录路径来连接到不同的MySQL服务器或者使用不同的账号连接到指定的服务器。这些登录路径都可以在调用客户端程序时通过使用–login-path选项来使用。例如,为了连接到远程服务器,执行以下命令:

[mysql@localhost ~]$ mysql --login-path=remote  
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 56674
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 

上面的命令mysql从其它选项文件中读取[client]和[mysql]选项组并且从登录路径文件中读取[mysql]和[remote]选项组信息。

为了连接到本地服务器,执行以下命令

[mysql@localhost ~]$ mysql --login-path=client
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.26 Source distribution

Copyright (c) 2000, 2019, 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>

因为缺省情况下mysql读取登录路径文件中的client和mysql登录路径,在这种情况下–login-path选项不会增加其它登录路径。因此上面的命令等价下面的命令:

[mysql@localhost ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.26 Source distribution

Copyright (c) 2000, 2019, 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> 

从登录路径文件读取的选项优先于从其他选项文件读取的选项。从登录路径文件中稍后出现的登录路径组读取的选项优先于从文件中较早出现的登录路径组读取的选项。

mysql_config_editor向登录路径文件增加登录路径的顺序就是创建它们的顺序,因此应该先创建更多的通用登录路径,后创建特定登录路径。如果想要在登录路径文件中移动一个登录路径,可以先删除它,然后再重新创建它。例如一个client登录路径很通用,因为所有的客户端程序将会读取它,而mysqldump登录路径只能由mysqldump程序来读取。后指定的选项会覆盖先指定的选项,因此以client,mysqldump顺序来创建登录路径,mysqldump程序能让mysqldump的特定选项覆盖client的选项。

在使用mysql_config_editor的set命令来创建一个登录路径时不需要指定所有可能选项值(主机名,用户名,密码,端口号,socket)。只有指定的值会被写入登录路径。任何丢失而在调用客户端程序连接服务器时所需要的选项可以在其它选项文件或命令行中批定。任何在命令行中指定的选项值会覆盖在登录路径文件或其它选项文件中所指定的选项值。例如,如果在remote登录路径中指定了端口号33306,现在假设远程服务器端口变为3306了,那么连接服务器命令如下:

[mysql@localhost ~]$ mysql --login-path=remote -P3306
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 56674
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 

命令行指定的端口号覆盖登录路径中指定的端口号

可以使用remove命令来删除登录路径或登录路径中的某些选项删除remote登录路径中的–port选项

[mysql@localhost ~]$ mysql_config_editor print --login-path=remote
[remote]
user = root
password = *****
host = 192.168.1.250
port = 33306
[mysql@localhost ~]$ mysql_config_editor remove --login-path=remote --port
[mysql@localhost ~]$ mysql_config_editor print --login-path=remote
[remote]
user = root
password = *****
host = 192.168.1.250

删除删除remote登录路径

[mysql@localhost ~]$ mysql_config_editor remove --login-path=remote
[mysql@localhost ~]$ mysql_config_editor print --login-path=remote

在Windows中运行多个MySQL实例

在Windows中可以从命令行为每个实例使用合适的操作参数来手动启动多个MySQL实例或者通过以Windows服务方式来安装多个服务器来运行。

1.在windows中MySQL的base目录中创建两个新实例的数据目录data3307,data3308

2.为每个新实例设置配置文件来指定相关选项
my3307.cnf文件内容如下:

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4

[mysqld]
# 设置3307端口
port = 3307
# 设置mysql的安装目录
basedir=D:\mysql-5.7.25-win32
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
datadir=D:\mysql-5.7.25-win32\data3307
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
explicit_defaults_for_timestamp=true
log-error=D:\mysql-5.7.25-win32\mysql3307.err
pid-file=D:\mysql-5.7.25-win32\mysqld3307.pid
socket =D:\mysql-5.7.25-win32\mysql3307.sock

my3308.cnf文件内容如下:

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4

[mysqld]
# 设置3308端口
port = 3308
# 设置mysql的安装目录
basedir=D:\mysql-5.7.25-win32
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
datadir=D:\mysql-5.7.25-win32\data3308
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
explicit_defaults_for_timestamp=true
log-error=D:\mysql-5.7.25-win32\mysql3308.err
pid-file=D:\mysql-5.7.25-win32\mysqld3308.pid
socket =D:\mysql-5.7.25-win32\mysql3308.sock

3.初始化新数据库

D:\mysql-5.7.25-win32\bin>mysqld  --defaults-file=D:\mysql-5.7.25-win32\my3307.cnf --initialize --basedir=D:\mysql-5.7.25-win32 --datadir=D:\mysql-5.7.25-win32\data3307

D:\mysql-5.7.25-win32\bin>mysqld  --defaults-file=D:\mysql-5.7.25-win32\my3308.cnf --initialize --basedir=D:\mysql-5.7.25-win32 --datadir=D:\mysql-5.7.25-win32\data3308

4.启动数据库

D:\mysql-5.7.25-win32\bin>mysqld  --defaults-file=D:\mysql-5.7.25-win32\my3307.cnf


D:\mysql-5.7.25-win32\bin>mysqld  --defaults-file=D:\mysql-5.7.25-win32\my3308.cnf

每个服务器都在前台启动(在服务器稍后退出之前不会出现新的提示),所以您需要在单独的控制台窗口中发出这两个命令。

5.登录数据库修改密码

C:\Users\Administrator>mysql --port=3307 --host=127.0.0.1 --user=root --password=U0U?KinrdWHb
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.25

Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

C:\Users\Administrator>mysql --port=3307 --host=127.0.0.1 --user=root --password=123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.25 Source distribution

Copyright (c) 2000, 2019, 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


C:\Users\Administrator>mysql --port=3308 --host=127.0.0.1 --user=root
Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.25

Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye


C:\Users\Administrator>mysql --port=3308 --host=127.0.0.1 --user=root --password=123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.25 Source distribution

Copyright (c) 2000, 2019, 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>

6.为了关闭数据库服务器,使用合适的端口号连接到每个实例执行下面的命令:

C:\Users\Administrator>mysqladmin --port=3307 --host=127.0.0.1 --user=root --password=123456 shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.

C:\Users\Administrator>mysqladmin --port=3308 --host=127.0.0.1 --user=root --password=123456 shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.

上面的配置允许客户端通过TCP/IP来进行连接。如果你的Windows版本支持命名管道并且你也想使用命名管道来连接,指定启用命名管道选项和指定它的名称。每个实例支持命名管道连接必须使用一个唯一的管道名。例如:
my3307.cnf文件内容如下:

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4

[mysqld]
# 设置3307端口
port = 3307
# 设置mysql的安装目录
basedir=D:\mysql-5.7.25-win32
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
datadir=D:\mysql-5.7.25-win32\data3307
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
explicit_defaults_for_timestamp=true
log-error=D:\mysql-5.7.25-win32\mysql3307.err
pid-file=D:\mysql-5.7.25-win32\mysqld3307.pid
enable-named-pipe
socket =D:\mysql-5.7.25-win32\mysql3307.sock

my3308.cnf文件内容如下:

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4

[mysqld]
# 设置3308端口
port = 3308
# 设置mysql的安装目录
basedir=D:\mysql-5.7.25-win32
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
datadir=D:\mysql-5.7.25-win32\data3308
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
explicit_defaults_for_timestamp=true
log-error=D:\mysql-5.7.25-win32\mysql3308.err
pid-file=D:\mysql-5.7.25-win32\mysqld3308.pid
socket =D:\mysql-5.7.25-win32\mysql3308.sock

然后启动MySQL实例。想让客户端通过共享内存连接的过程与上面的过程类似。对每个连接使用–shared-memory选项并使用–shared-memory-base-name选项来为每个实例指定唯一的共享内存名字。

在Windows中作为Windows服务来启动多个MySQL实例
在Windows中,一个MySQL服务器可以以Windows服务来运行。为了设置多个MySQL服务,你必须确保每个实例使用不同的服务名另外其它参数每个实例必须都有唯一值。为了将MySQL服务器注册成为Windows服务,使用mysqld –install或mysqld –install-manual选项。

基于上面的信息,有几种方式来设置多个Windows服务。在注册Windows服务之前需要关闭并删除任何已经存在的Windows服务。

方法1
在一个标准的选项文件中指定所有服务选项。这样就需要为每个MySQL服务指定不同的服务名。假设端口为3307的MySQL实例的服务名为mysqld1,端口为3308的MySQL实例的服务名为mysqld2。那么D:\mysql-5.7.25-win32\my.ini设置如下:

[mysqld1]
# 设置3307端口
port = 3307
# 设置mysql的安装目录
basedir=D:\mysql-5.7.25-win32
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
datadir=D:\mysql-5.7.25-win32\data3307
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
explicit_defaults_for_timestamp=true
log-error=D:\mysql-5.7.25-win32\mysql3307.err
pid-file=D:\mysql-5.7.25-win32\mysqld3307.pid
socket =D:\mysql-5.7.25-win32\mysql3307.sock


[mysqld2]
# 设置3308端口
port = 3308
# 设置mysql的安装目录
basedir=D:\mysql-5.7.25-win32
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
datadir=D:\mysql-5.7.25-win32\data3308
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
explicit_defaults_for_timestamp=true
log-error=D:\mysql-5.7.25-win32\mysql3308.err
pid-file=D:\mysql-5.7.25-win32\mysqld3308.pid
socket =D:\mysql-5.7.25-win32\mysql3308.sock

注册服务,使用完整服务路径名来确保Windows为每个服务注册正确的可执行程序:

D:\mysql-5.7.25-win32\bin>mysqld --install mysqld1
Service successfully installed.

D:\mysql-5.7.25-win32\bin>mysqld --install mysqld2
Service successfully installed.

为了启动MySQL服务,使用服务管理器或者使用net start servicename:

C:\Users\Administrator>net start mysqld1
mysqld1 服务正在启动 .
mysqld1 服务已经启动成功。


C:\Users\Administrator>net start mysqld2
mysqld2 服务正在启动 .
mysqld2 服务已经启动成功。

为了停止MySQL服务,使用服务管理器或者使用net stop servicename:
C:\Users\Administrator>net stop mysqld1
mysqld1 服务正在停止.
mysqld1 服务已成功停止。


C:\Users\Administrator>net stop mysqld2
mysqld2 服务正在停止.
mysqld2 服务已成功停止。

删除服务

D:\mysql-5.7.25-win32\bin>mysqld --remove mysqld1
Service successfully removed.

D:\mysql-5.7.25-win32\bin>mysqld --remove mysqld2
Service successfully removed.

方法2
为每个MySQL服务使用单独的文件来指定选项并且在注册服务时使用–defaults-file选项来告诉每个服务器所要使用的选项文件。在这种情况下,每个文件都将使用[mysqld]选项组。
my3307.cnf文件内容如下:

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4

[mysqld]
# 设置3307端口
port = 3307
# 设置mysql的安装目录
basedir=D:\mysql-5.7.25-win32
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
datadir=D:\mysql-5.7.25-win32\data3307
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
explicit_defaults_for_timestamp=true
log-error=D:\mysql-5.7.25-win32\mysql3307.err
pid-file=D:\mysql-5.7.25-win32\mysqld3307.pid
enable-named-pipe
socket =D:\mysql-5.7.25-win32\mysql3307.sock

my3308.cnf文件内容如下:

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4

[mysqld]
# 设置3308端口
port = 3308
# 设置mysql的安装目录
basedir=D:\mysql-5.7.25-win32
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
datadir=D:\mysql-5.7.25-win32\data3308
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
explicit_defaults_for_timestamp=true
log-error=D:\mysql-5.7.25-win32\mysql3308.err
pid-file=D:\mysql-5.7.25-win32\mysqld3308.pid
socket =D:\mysql-5.7.25-win32\mysql3308.sock

将每个MySQL实例注册成Windows服务

D:\mysql-5.7.25-win32\bin>mysqld --install mysqld1 --defaults-file=D:\mysql-5.7.25-win32\my3307.cnf
Service successfully installed.


D:\mysql-5.7.25-win32\bin>mysqld --install mysqld2 --defaults-file=D:\mysql-5.7.25-win32\my3308.cnf
Service successfully installed.

启动服务

C:\Users\Administrator>net start mysqld1
mysqld1 服务正在启动 .
mysqld1 服务已经启动成功。


C:\Users\Administrator>net start mysqld2
mysqld2 服务正在启动 .
mysqld2 服务已经启动成功。

Linux中运行多个MySQL实例

这里将介绍使用mysqld_safe来启动多个MySQL实例。对于使用一个RPM发布版本安装的MySQL来说,在多数Linux平台中MySQL服务的启动和关闭是由systemd来管理的。在这些平台上没有安装msyqld_safe因为不需要。

一种在Linux上运行多个MySQL实例的方法是用不同的默认TCP/IP端口和Unix套接字文件编译不同的服务器,以便每个服务器都监听不同的网络接口。为了在每个不同的基本目录中进行编译,还会自动生成一个单独的、编译后的数据目录、日志文件和每个服务器的PID文件位置。

假设一个现有的5.6服务器被配置成TCP/IP端口为3306并且Unix socket文件为/tmp/mysql.sock。为了配置一个新的5.7.21服务器使用不同的操作参数,使用CMake命令进行编译:
shell> cmake . -DMYSQL_TCP_PORT=port_number \
-DMYSQL_UNIX_ADDR=file_name \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.21

这里,port_number和file_name必须与缺省的TCP/CP端口号和Unix socket文件路径名不同,并且CMAKE_INSTALL_PREFIX值指定的安装目录不能是现有MySQL安装目录相同的目录。

如果有一个MySQL服务器正在监听一个指定的端口号,你可以使用下面的命令来找出多个重要配置变量所使用的操作参数,包括base目录和Unix socket文件名:
shell> mysqladmin –host=host_name –port=port_number variables

通过命令所显示的信息,你也可以知道当配置另一个服务器时什么选项值将不能使用。

如果指定localhost作为主机名,mysqladmin缺省会使用一个Unix socket文件来进行连接而不是使用TCP/IP。为了显性指定连接协议,使用–protocol={TCP|SOCKET|PIPE|MEMORY}选项。

如果只是使用不同的Unix socket文件和TCP/IP端口来启动一个MySQL实例那么不需要编译一个新的MySQL服务器。可以使用相同的服务器二进制文件并且在运行时为每个MySQL实例使用不同的参数。一种方式是使用命令行选项:
shell>mysqld_safe –socket=file_name –port=port_number

为了启动第二个MySQL实例,给mysqld_safe提供不同的–socket和–port选项值并传一个–datadir=dir_name选项因此这个实例将使用不同的数据目录。

另一种方法是将每个MySQL实例的选项放入不同的选项文件,然后启动每个实例时使用–defaults-file选项来指定合适选项文件的路径。

shell> mysqld_safe --defaults-file=/usr/local/mysql/my.cnf
shell> mysqld_safe --defaults-file=/usr/local/mysql/my.cnf2

另一种方法来完成相同的功能是使用环境变量来设置Unix socket文件名和TCP/IP端口号:

shell> MYSQL_UNIX_PORT=/tmp/mysqld-new.sock
shell> MYSQL_TCP_PORT=3307
shell> export MYSQL_UNIX_PORT MYSQL_TCP_PORT
shell> mysql_install_db --user=mysql
shell> mysqld_safe --datadir=/path/to/datadir &

这是一种快速启动第二个实例进行测试的方法。它的好处是设置的环境变量可以应用到从相同shell执行调用的任何客户端程序。因此对于这些客户端连接会自动指向第二实例。

另一种方法是在Linux中使用mysqld_multi脚本来管理多个MySQL实例。
下面来创建三个实例(实例的端口号为3307,3308,3309)
创建存储这三个实例的数据库文件目录

-bash-4.2$ mkdir mysql3307
-bash-4.2$ mkdir mysql3308
-bash-4.2$ mkdir mysql3309
-bash-4.2$ ls -lrt
总用量 4
drwxrwxrwx. 5 mysql mysql 4096 8月  22 21:46 mysql
drwxr-xr-x. 2 mysql mysql    6 9月   4 10:26 mysql3307
drwxr-xr-x. 2 mysql mysql    6 9月   4 10:26 mysql3308
drwxr-xr-x. 2 mysql mysql    6 9月   4 10:26 mysql3309

为每个实例创建一个选项文件(my3307.cnf,my3308.cnf,my3309.cnf)
my3307.cnf文件内容如下:

-bash-4.2$ cat my3307.cnf
[mysqld]
basedir=/mysqlsoft/mysql
datadir=/mysqldata/mysql3307
bind-address=0.0.0.0
user=mysql
port=3307
log-error=/mysqldata/mysql3307/mysql.err
pid-file=/mysqldata/mysql3307/mysqld.pid
socket = /mysqldata/mysql3307/mysql.sock
character-set-server=utf8mb4
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true

my3308.cnf文件内容如下:

-bash-4.2$ cat my3308.cnf
[mysqld]
basedir=/mysqlsoft/mysql
datadir=/mysqldata/mysql3308
bind-address=0.0.0.0
user=mysql
port=3308
log-error=/mysqldata/mysql3308/mysql.err
pid-file=/mysqldata/mysql3308/mysqld.pid
socket = /mysqldata/mysql3308/mysql.sock
character-set-server=utf8mb4
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true

my3309.cnf文件内容如下:

-bash-4.2$ cat my3309.cnf
[mysqld]
basedir=/mysqlsoft/mysql
datadir=/mysqldata/mysql3309
bind-address=0.0.0.0
user=mysql
port=3309
log-error=/mysqldata/mysql3309/mysql.err
pid-file=/mysqldata/mysql3309/mysqld.pid
socket = /mysqldata/mysql3309/mysql.sock
character-set-server=utf8mb4
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true

初始化数据库

-bash-4.2$ mysqld  --defaults-file=/mysqlsoft/mysql/my3307.cnf --initialize --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql3307 --user=mysql
-bash-4.2$ mysqld  --defaults-file=/mysqlsoft/mysql/my3308.cnf --initialize --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql3308 --user=mysql
-bash-4.2$ mysqld  --defaults-file=/mysqlsoft/mysql/my3309.cnf --initialize --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql3309 --user=mysql

启动数据库

-bash-4.2$ mysqld_safe --defaults-file=/mysqlsoft/mysql/my3307.cnf &
[1] 10359
-bash-4.2$ 2019-09-05T09:39:35.467416Z mysqld_safe Logging to '/mysqldata/mysql3307/mysql.err'.
2019-09-05T09:39:35.545107Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3307


-bash-4.2$ mysqld_safe --defaults-file=/mysqlsoft/mysql/my3308.cnf &
[1] 10624
-bash-4.2$ 2019-09-05T09:42:28.457387Z mysqld_safe Logging to '/mysqldata/mysql3308/mysql.err'.
2019-09-05T09:42:28.532350Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3308


-bash-4.2$ mysqld_safe --defaults-file=/mysqlsoft/mysql/my3309.cnf &
[1] 10889
-bash-4.2$ 2019-09-05T09:45:03.772185Z mysqld_safe Logging to '/mysqldata/mysql3309/mysql.err'.
2019-09-05T09:45:03.847584Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3309

修改每个MySQL实例的用户密码并关闭实例

-bash-4.2$ mysql --port=3307 --host=127.0.0.1 --user=root --password=nCohVRg-=7LP 
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26

Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
-bash-4.2$ mysqladmin --port=3307 --host=127.0.0.1 --user=root --password=123456 shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.


-bash-4.2$ mysql --port=3308 --host=127.0.0.1 --user=root --password=g*tV/I%#s6j#
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26

Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
-bash-4.2$ mysqladmin --port=3308 --host=127.0.0.1 --user=root --password=123456 shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.

-bash-4.2$ mysql --port=3309 --host=127.0.0.1 --user=root --password=eIsXkThGK5*4
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26

Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
-bash-4.2$ mysqladmin --port=3309 --host=127.0.0.1 --user=root --password=123456 shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.

使用mysqld_multi来管理多个MySQL实例
修改配置文件my.cnf增加以下内容(增加三个实例的选项参数)

[mysqld_multi]
mysqld=/mysqlsoft/mysql/bin/mysqld_safe
mysqladmin =/mysqlsoft/mysql/bin/mysqladmin
log =/mysqlsoft/mysql/mysqld_multi.log

[mysqld1]
basedir=/mysqlsoft/mysql
datadir=/mysqldata/mysql3307
bind-address=0.0.0.0
user=mysql
port=3307
log-error=/mysqldata/mysql3307/mysql.err
pid-file=/mysqldata/mysql3307/mysqld.pid
socket = /mysqldata/mysql3307/mysql.sock
character-set-server=utf8mb4
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true


[mysqld2]
basedir=/mysqlsoft/mysql
datadir=/mysqldata/mysql3308
bind-address=0.0.0.0
user=mysql
port=3308
log-error=/mysqldata/mysql3308/mysql.err
pid-file=/mysqldata/mysql3308/mysqld.pid
socket = /mysqldata/mysql3308/mysql.sock
character-set-server=utf8mb4
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true


[mysqld3]
basedir=/mysqlsoft/mysql
datadir=/mysqldata/mysql3309
bind-address=0.0.0.0
user=mysql
port=3309
log-error=/mysqldata/mysql3309/mysql.err
pid-file=/mysqldata/mysql3309/mysqld.pid
socket = /mysqldata/mysql3309/mysql.sock
character-set-server=utf8mb4
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true

使用mysqld_multi来启动实例

[mysql@localhost ~]$ mysqld_multi start 1

报错了,从日志可以看到启动实例时调用了两次,我这里的测试环境在创建多个实例之前创建了一个mysqld实例(也许存在影响)

[mysql@localhost mysql]$ tail -f mysqld_multi.log 
Starting MySQL servers

2019-09-06T05:40:07.558168Z mysqld_safe Logging to '/mysqldata/mysql3307/mysql.err'.
2019-09-06T05:40:07.563783Z mysqld_safe Logging to '/mysqldata/mysql3307/mysql.err'.
2019-09-06T05:40:07.618543Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3307
2019-09-06T05:40:07.623821Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3307

root@localhost mysql3307]# tail -f mysql.err
2019-09-06T06:24:30.964335Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2019-09-06T06:24:31.964485Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2019-09-06T06:24:31.964573Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2019-09-06T06:24:32.964723Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2019-09-06T06:24:32.964812Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2019-09-06T06:24:33.964935Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2019-09-06T06:24:33.964987Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2019-09-06T06:24:34.965105Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2019-09-06T06:24:34.965178Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2019-09-06T06:24:35.965292Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2019-09-06T06:24:35.965340Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2019-09-06T06:24:36.965460Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2019-09-06T06:24:36.965509Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2019-09-06T06:24:37.965632Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11

所以这里在使用mysqld_multi来启动实例时使用–defaults-file

[mysql@localhost bin]$ mysqld_multi --defaults-file=/mysqlsoft/mysql/my.cnf start 1
[mysql@localhost bin]$ mysqld_multi --defaults-file=/mysqlsoft/mysql/my.cnf start 2,3

[mysql@localhost mysql]$ tail -f mysqld_multi.log 
Starting MySQL servers

2019-09-06T06:49:50.003877Z mysqld_safe Logging to '/mysqldata/mysql3307/mysql.err'.
2019-09-06T06:49:50.096954Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3307
mysqld_multi log file version 2.16; run: 五 9月  6 14:59:33 2019

Starting MySQL servers

2019-09-06T06:59:33.644263Z mysqld_safe Logging to '/mysqldata/mysql3308/mysql.err'.
2019-09-06T06:59:33.650226Z mysqld_safe Logging to '/mysqldata/mysql3309/mysql.err'.
2019-09-06T06:59:33.704593Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3308
2019-09-06T06:59:33.710937Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql3309

到此使用mysqld_multi来管理多个实例的操作就完成了。

MySQL Server Startup Script

MySQL Server Startup Script
在Unix和类似于Unix的系统中的MySQL发布版本包含一个名叫mysql.server的脚本,它将调用mysqld_safe来启动MySQL服务器。它也可以用于像Linux和Solaris系统上使用System V-style来运行指令来启动和停止系统服务。也可以被macOS启动项目来启动MySQL。

mysql.server是MySQL源码树中的脚本名称。其安装后的名称可能不一样,例如,mysqld或mysql。下面将会介绍将mysql.server脚本调整成适合于你系统的名称。注意对于某些Linux平台,使用RPM或Debian包安装的MySQL包含用来管理MySQL服务器启动和关闭的systemd的支持。在这些平台上,mysql.server和mysqld_safe不需要安装因为不需要它们。

为了使用mysql.server来手动启动或停止MySQL服务,可以从命令调用它并使用start或stop参数:

[root@localhost support-files]# ./mysql.server stop
Shutting down MySQL.. SUCCESS!
[root@localhost support-files]# ./mysql.server start
Starting MySQL.. SUCCESS!

相关日志信息如下:

2019-08-22T13:29:13.429419Z 0 [Note] Giving 0 client threads a chance to die gracefully
2019-08-22T13:29:13.429508Z 0 [Note] Shutting down slave threads
2019-08-22T13:29:13.429535Z 0 [Note] Forcefully disconnecting 0 remaining clients
2019-08-22T13:29:13.429553Z 0 [Note] Event Scheduler: Purging the queue. 0 events
2019-08-22T13:29:13.429630Z 0 [Note] Binlog end
2019-08-22T13:29:13.460102Z 0 [Note] Shutting down plugin 'ngram'
2019-08-22T13:29:13.460187Z 0 [Note] Shutting down plugin 'partition'
2019-08-22T13:29:13.460214Z 0 [Note] Shutting down plugin 'BLACKHOLE'
2019-08-22T13:29:13.460235Z 0 [Note] Shutting down plugin 'ARCHIVE'
2019-08-22T13:29:13.460277Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2019-08-22T13:29:13.460405Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
2019-08-22T13:29:13.460446Z 0 [Note] Shutting down plugin 'MyISAM'
2019-08-22T13:29:13.460553Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
2019-08-22T13:29:13.460599Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2019-08-22T13:29:13.460619Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2019-08-22T13:29:13.460637Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2019-08-22T13:29:13.460681Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2019-08-22T13:29:13.460736Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2019-08-22T13:29:13.460776Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2019-08-22T13:29:13.460898Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2019-08-22T13:29:13.460920Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2019-08-22T13:29:13.460938Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2019-08-22T13:29:13.460956Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2019-08-22T13:29:13.460973Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2019-08-22T13:29:13.460991Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2019-08-22T13:29:13.461008Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2019-08-22T13:29:13.461026Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2019-08-22T13:29:13.461044Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2019-08-22T13:29:13.461089Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
2019-08-22T13:29:13.461140Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
2019-08-22T13:29:13.461161Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2019-08-22T13:29:13.461180Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2019-08-22T13:29:13.461220Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2019-08-22T13:29:13.461286Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2019-08-22T13:29:13.461306Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2019-08-22T13:29:13.461325Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2019-08-22T13:29:13.461343Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
2019-08-22T13:29:13.461361Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2019-08-22T13:29:13.461378Z 0 [Note] Shutting down plugin 'INNODB_CMP'
2019-08-22T13:29:13.461396Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2019-08-22T13:29:13.461414Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
2019-08-22T13:29:13.461431Z 0 [Note] Shutting down plugin 'INNODB_TRX'
2019-08-22T13:29:13.461450Z 0 [Note] Shutting down plugin 'InnoDB'
2019-08-22T13:29:13.461776Z 0 [Note] InnoDB: FTS optimize thread exiting.
2019-08-22T13:29:13.462092Z 0 [Note] InnoDB: Starting shutdown...
2019-08-22T13:29:13.562479Z 0 [Note] InnoDB: Dumping buffer pool(s) to /mysqldata/mysql/ib_buffer_pool
2019-08-22T13:29:13.563934Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 190822 21:29:13
2019-08-22T13:29:14.807857Z 0 [Note] InnoDB: Shutdown completed; log sequence number 2530229
2019-08-22T13:29:14.810644Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2019-08-22T13:29:14.810690Z 0 [Note] Shutting down plugin 'MEMORY'
2019-08-22T13:29:14.810707Z 0 [Note] Shutting down plugin 'CSV'
2019-08-22T13:29:14.810721Z 0 [Note] Shutting down plugin 'sha256_password'
2019-08-22T13:29:14.810731Z 0 [Note] Shutting down plugin 'mysql_native_password'
2019-08-22T13:29:14.810961Z 0 [Note] Shutting down plugin 'binlog'
2019-08-22T13:29:14.811432Z 0 [Note] /mysqlsoft/mysql/bin/mysqld: Shutdown complete

2019-08-22T13:30:24.969246Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2019-08-22T13:30:24.969441Z 0 [Note] /mysqlsoft/mysql/bin/mysqld (mysqld 5.7.26) starting as process 32558 ...
2019-08-22T13:30:24.980591Z 0 [Note] InnoDB: PUNCH HOLE support available
2019-08-22T13:30:24.980678Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-08-22T13:30:24.980740Z 0 [Note] InnoDB: Uses event mutexes
2019-08-22T13:30:24.980758Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2019-08-22T13:30:24.980779Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-08-22T13:30:24.981460Z 0 [Note] InnoDB: Number of pools: 1
2019-08-22T13:30:24.981799Z 0 [Note] InnoDB: Using CPU crc32 instructions
2019-08-22T13:30:24.985591Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2019-08-22T13:30:25.002296Z 0 [Note] InnoDB: Completed initialization of buffer pool
2019-08-22T13:30:25.007382Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2019-08-22T13:30:25.021394Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2019-08-22T13:30:25.063462Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2019-08-22T13:30:25.063830Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2019-08-22T13:30:25.758116Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2019-08-22T13:30:25.760396Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2019-08-22T13:30:25.760448Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2019-08-22T13:30:25.761681Z 0 [Note] InnoDB: Waiting for purge to start
2019-08-22T13:30:25.811963Z 0 [Note] InnoDB: 5.7.26 started; log sequence number 2530229
2019-08-22T13:30:25.812330Z 0 [Note] InnoDB: Loading buffer pool(s) from /mysqldata/mysql/ib_buffer_pool
2019-08-22T13:30:25.813642Z 0 [Note] Plugin 'FEDERATED' is disabled.
2019-08-22T13:30:25.819058Z 0 [Note] InnoDB: Buffer pool(s) load completed at 190822 21:30:25
2019-08-22T13:30:25.824582Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2019-08-22T13:30:25.825236Z 0 [Warning] CA certificate ca.pem is self signed.
2019-08-22T13:30:25.827375Z 0 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
2019-08-22T13:30:25.827463Z 0 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
2019-08-22T13:30:25.829361Z 0 [Note] Server socket created on IP: '0.0.0.0'.
2019-08-22T13:30:25.859593Z 0 [Note] Event Scheduler: Loaded 0 events
2019-08-22T13:30:25.859938Z 0 [Note] /mysqlsoft/mysql/bin/mysqld: ready for connections.
Version: '5.7.26'  socket: '/mysqlsoft/mysql/mysql.sock'  port: 3306  Source distribution

mysql.server进入到MySQL安装目录,然后调用mysqld_safe。为了让某些特定的用户能够运行服务,在全局/etc/my.cnf选项文件中为[mysqld]组增加一个合适的user选项,后面将会介绍(如果你将二进制版本的MySQL安装在非标准位置,那么可能必须要修改mysql.server脚本。修改它是为了在运行mysqld_safe之前进入到合适的目录中。如果这样做,当将来升级MySQL时你修改的mysql.server版本可能会被覆盖,创建一个你修改版本的副本可以进行重新安装)。

mysql.server stop通过发送一个信号给它来停止MySQL服务。也可以通过执行mysqladmin shutdown来手动关闭服务。

[root@localhost bin]# ./mysqladmin -uroot -p123456 shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.

日志信息如下:

2019-08-22T13:44:57.170282Z 0 [Note] Giving 0 client threads a chance to die gracefully
2019-08-22T13:44:57.170357Z 0 [Note] Shutting down slave threads
2019-08-22T13:44:57.170919Z 0 [Note] Forcefully disconnecting 0 remaining clients
2019-08-22T13:44:57.171016Z 0 [Note] Event Scheduler: Purging the queue. 0 events
2019-08-22T13:44:57.171140Z 0 [Note] Binlog end
2019-08-22T13:44:57.172096Z 0 [Note] Shutting down plugin 'ngram'
2019-08-22T13:44:57.172142Z 0 [Note] Shutting down plugin 'partition'
2019-08-22T13:44:57.172203Z 0 [Note] Shutting down plugin 'BLACKHOLE'
2019-08-22T13:44:57.172225Z 0 [Note] Shutting down plugin 'ARCHIVE'
2019-08-22T13:44:57.172259Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2019-08-22T13:44:57.172317Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
2019-08-22T13:44:57.172348Z 0 [Note] Shutting down plugin 'MyISAM'
2019-08-22T13:44:57.172398Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
2019-08-22T13:44:57.172428Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2019-08-22T13:44:57.172441Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2019-08-22T13:44:57.172452Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2019-08-22T13:44:57.172476Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2019-08-22T13:44:57.172506Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2019-08-22T13:44:57.172564Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2019-08-22T13:44:57.172579Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2019-08-22T13:44:57.172590Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2019-08-22T13:44:57.172601Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2019-08-22T13:44:57.172612Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2019-08-22T13:44:57.172623Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2019-08-22T13:44:57.172634Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2019-08-22T13:44:57.172644Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2019-08-22T13:44:57.172655Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2019-08-22T13:44:57.172666Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2019-08-22T13:44:57.172676Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
2019-08-22T13:44:57.172687Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
2019-08-22T13:44:57.172698Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2019-08-22T13:44:57.172708Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2019-08-22T13:44:57.172734Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2019-08-22T13:44:57.172746Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2019-08-22T13:44:57.172769Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2019-08-22T13:44:57.172802Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2019-08-22T13:44:57.172814Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
2019-08-22T13:44:57.172838Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2019-08-22T13:44:57.172849Z 0 [Note] Shutting down plugin 'INNODB_CMP'
2019-08-22T13:44:57.172872Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2019-08-22T13:44:57.172884Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
2019-08-22T13:44:57.172895Z 0 [Note] Shutting down plugin 'INNODB_TRX'
2019-08-22T13:44:57.172906Z 0 [Note] Shutting down plugin 'InnoDB'
2019-08-22T13:44:57.173010Z 0 [Note] InnoDB: FTS optimize thread exiting.
2019-08-22T13:44:57.173195Z 0 [Note] InnoDB: Starting shutdown...
2019-08-22T13:44:57.274546Z 0 [Note] InnoDB: Dumping buffer pool(s) to /mysqldata/mysql/ib_buffer_pool
2019-08-22T13:44:57.275296Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 190822 21:44:57
2019-08-22T13:44:58.528547Z 0 [Note] InnoDB: Shutdown completed; log sequence number 2530257
2019-08-22T13:44:58.531650Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2019-08-22T13:44:58.531698Z 0 [Note] Shutting down plugin 'MEMORY'
2019-08-22T13:44:58.531736Z 0 [Note] Shutting down plugin 'CSV'
2019-08-22T13:44:58.531752Z 0 [Note] Shutting down plugin 'sha256_password'
2019-08-22T13:44:58.531764Z 0 [Note] Shutting down plugin 'mysql_native_password'
2019-08-22T13:44:58.531967Z 0 [Note] Shutting down plugin 'binlog'
2019-08-22T13:44:58.532460Z 0 [Note] /mysqlsoft/mysql/bin/mysqld: Shutdown complete

为了在服务器上自动启动和停止MySQL服务,你必须在你的/etc/rc*文件中合适的地方添加启动和停止命令:
.如果使用Linux服务RPM包(MySQL-server-VERSION.rpm)或一个原生的Linux包安装,那么mysql.server脚本可能以mysqld或mysql的名称安装到/etc/init.d目录中。

.如果使用源码或二进制版本来安装MySQL时不能自动安装mysql.server,可以手动安装这个脚本。它可以在MySQL安装目录或MySQL源码树下的support-files目录中找到。将这个脚本复制到/etc/init.d目录中并命名为mysql并且修改权限让其它可以执行:

[root@cs2 ~]# cp /mysqlsoft/mysql/support-files/mysql.server  /etc/init.d/mysqld
[root@cs2 ~]# chmod 755 /etc/init.d/mysqld

在安装脚本后,依赖于操作系统命令需要在系统启动时来激活它。在Linux上,你可以使用chkconfig:

[root@localhost init.d]# chkconfig --add mysqld

在有一些Linux系统中,也需要执行以下命令来完全启用mysqld脚本:

[root@localhost init.d]# chkconfig --level 345 mysqld on

在FreeBSD系统中,启动脚本通常应该在/usr/local/etc/rc.d/目录中,安装的mysql.server脚本就是/usr/local/etc/rc.d/mysql.server.sh来启用自动启动。rc(8)手册页指出,只有当脚本的基本名称与*.sh shell文件名模式匹配时,才会执行该目录中的脚本。该目录中存在的任何其他文件或目录都将被默认忽略。

作为上述设置的一种替代方案,有些操作系统也会使用/etc/rc.local或/etc/init.d/boot.local来在操作系统启动时启动额外的服务。为了使用这种方法来启动MySQL。需要在合适的启动文件中增加类似下面的命令:

/bin/sh -c 'cd /usr/local/mysql; ./bin/mysqld_safe --user=mysql &'

对于其它系统,咨询你操作系统文档来查看如何安装启动脚本。

mysql.server会读取选项文件中的[mysql.server]和[mysqld]部分的选项。为了向后兼容,它也会读取[mysql_server]部分的选项,但是现在你应该将它重命名为[mysql.server]。

可以在全局/etc/my.cnf文件中为mysql.server增加选项。一个常见的my.cnf文件可能看到类似于以下所示:

[mysqld]
basedir=/mysqlsoft/mysql
datadir=/mysqldata/mysql
user=mysql
port=3306
socket = /mysqlsoft/mysql/mysql.sock

[mysql.server]
basedir=/usr/local/mysql

mysql.server脚本支持下表中所列出的选项。如果指定,他们必须被存储在一个选项文件中,而不是在命令行中设置。mysql.server只支持start和stop作为命令行参数

表4.4 mysql.server选项文件选项
Option Name Description Type
basedir MySQL安装目录路径 目录名
datadir MySQL数据目录路径 目录名
pid-file 服务器将它的进程ID需要写入的文件 文件名
service-startup-timeout 服务器在启动时所等待的时间长度 整数

basedir=dir_name
MySQL安装目录路径

datadir=dir_name
MySQL数据目录路径

pid-file=file_name
服务器将它的进程ID需要写入的文件的路径名。如果这个选项没有指定,mysql.server将使用缺省值host_name.pid这种PID文件值传送给mysql_safe会覆盖在[mysqld_safe]选项文件组中所指定的任何值。因为mysql.server会读取[mysqld]选项文件组但不会读取[mysqld_safe]组,你也可以确保通过在[mysqld_safe]和[mysqld]组中设置相同的pid-file值使用在手动从mysql.server脚本中调用mysqld_safe时可以得到相同的值。

service-startup-timeout=seconds
确认服务器启动时可以等待的时间长度。如果服务器在这个时间内没有启动成功,mysql.server会抛出错误并退出。缺省时间为900秒。0意味着在启动时不会执行等待。负数意味着将会永久等待(不会超时)。

Linux使用源码来安装MySQL 5.7

Linux使用源码安装MySQL
有两种方法来使用源码安装MySQL:
.使用一种标准的MySQL源码安装。标准的可用源码文件被压缩成tar文件,zip文件或RPM包。文件名的格式为mysql-version.tar.gz,mysql-version.zip或mysql-version.rpm。其中version是版本号比例如5.7.25。发布的源码文件名与预先编译好的二进制文件名是有区别的,源码文件名通常不包含平台名,而二进制文件名包含平台名,例如pc-linux-i686或winx64。

.使用MySQL开发树。如何构建开发树将在使用开发源码树来安装MySQL中进行介绍。

使用源码安装MySQL操作系统所要满足的条件
使用源码来安装MySQL需要几个开发工具。无论您使用的是标准的源代码分发版还是开发源代码树,都需要这些工具中的一部分。是否需要其它工具依赖于你的安装方法。

为了使用源码来安装MySQL,不管使用何种安装方法下面的要求必须要满足:
.CMake,用来在所有平台上构建框架,可以从http://www.cmake.org来下载CMake。

.一个好的make程序,虽然一些平台有它们自己的make实现,但强烈建议使用GNU make 3.75或更高版本。它可能已经存在于你的系统中了gmake。可以从http://www.gnu.org/software/make/下载GNU make。

.ANSI C++编译器。可以对force_unsupported_compiler的描述。

.为了构建MySQL需要Boost C++库(但不使用它)。Boost 1.59.0必须被安装。为了获得Boost与它的安装指令,可以访问https://www.boost.org/网址。在Boost安装后,当调用CMake时通过使用WITH_BOOST选项来告诉构建系统在哪里定位Boost文件。

shell>cmake . -DWITH_BOOST=/usr/local/boost_1_59_0

根据实际情况来调整路径。

.ncurses库 https://www.gnu.org/software/ncurses/ncurses.html

.足够的空闲内存。当编译很大的源文件时如果遇到了”internal compiler error”错误,那么可能是因为你没有足够的内存。如果在一个虚拟机上进行编译,尝试增加内存分配。

.如果你希望运行测试脚本那么就需要安装perl。大多数像Unix的系统已经包含了perl。在windows上可以使用ActiveState Perl。

为了使用标准的源码发布版本来安装MySQL,需要以下一种工具来解压发布的源码文件:
.对于一个.tar.gz的压缩tar文件,GNU gunzip可以用来解压tar用来unpack。如果你的tar程序支持z选项,它可以用来解压与 unpack文件。GNU tar众所周知是有效的。一些操作系统提供的标准tar不能对MySQL发布版本的长文件名进行unpack操作。你应该安装GUN tar或者可能的话,使用预安装版本的GNU tar。常用的有gnutar,gtar,tar或者自由软件目录,比如/usr/sfw/bin或/usr/local/bin。GNU tar可以从http://www.gnu.org/software/tar/进行下载。

.对于一个.zip归档文件,winzip或另外的工具可以读取.zip文件。

.对于一个.rpm的RPM包,rpmbuild程序用来对构建的发布版本进行unpack操作。

为了使用开发源码树来安装MySQL,需要安装以下额外的工具:
.需要Git版本控制系统来获得开发源代码。https://help.github.com/en提供了指令在不同平台上下载与安装Git。MySQL官方组织已经于2014年9月加入了GitHub。

.bison 2.1或更高版本,可以从http://www.gnu.org/software/bison/网址进行下载(1版本不再支持),尽可能的使用最新版本,如果你遇到了问题,那么升级到最新的版本,而不是回退到更早的版本。

bison可以从http://www.gnu.org/software/bison/网址进行下载,对于Windows平台可以从http://gnuwin32.sourceforge.net/packages/bison.htm网址进行下载。在Windows平台上,bison的缺省目录是C:\Program Files\GnuWin32目录。有一些工具可以在查找bison会失败,因为在目录名中存在空隔。同样,Visual Studio如果在路径中存在空隔可能会出现hang住。为了解决这种问题的出现,可以将bison安装到C:\GnuWin32目录。

.在Solaris平台上,除了bison之外m4必须被安装。m4可以从http://www.gnu.org/software/m4/网址下载。

注意在安装任何程序之后,需要修改你的PATH环境变量来包含定位程序的目录。

源码安装MySQL的布局
缺省情况下,当使用源码编译后安装MySQL,安装操作会将文件安装在/usr/local/mysql目录中。这与使用二进制文件安装的安装目录是一样的。

使用标准源码安装MySQL
1.安装所需要的工具包

[root@localhost ~]# yum -y install gcc* gcc-c++ ncurses* ncurses-devel* cmake* bison* libgcrypt* perl* make*
.......
已安装:
  gcc-gnat.x86_64 0:4.8.5-36.0.1.el7                         gcc-objc.x86_64 0:4.8.5-36.0.1.el7                        gcc-objc++.x86_64 0:4.8.5-36.0.1.el7                    libgcrypt-devel.x86_64 0:1.5.3-14.el7
  ncurses-devel.x86_64 0:5.9-14.20130511.el7_4               ncurses-term.noarch 0:5.9-14.20130511.el7_4               perl-Algorithm-Diff.noarch 0:1.1902-17.el7              perl-App-cpanminus.noarch 0:1.6922-2.el7
  perl-Archive-Extract.noarch 1:0.68-3.el7                   perl-Archive-Zip.noarch 0:1.30-11.el7                     perl-Authen-SASL.noarch 0:2.15-10.el7                   perl-B-Lint.noarch 0:1.17-3.el7
  perl-Bit-Vector.x86_64 0:7.3-3.el7                         perl-CGI.noarch 0:3.63-4.el7                              perl-CGI-Session.noarch 0:4.35-16.el7                   perl-CPAN.noarch 0:1.9800-293.el7
  perl-CPAN-Meta.noarch 0:2.120921-5.el7                     perl-CPAN-Meta-Requirements.noarch 0:2.122-7.el7          perl-CPAN-Meta-YAML.noarch 0:0.008-14.el7               perl-CPANPLUS.noarch 0:0.91.38-4.el7
  perl-CPANPLUS-Dist-Build.noarch 0:0.70-3.el7               perl-Carp-Clan.noarch 0:6.04-10.el7                       perl-Class-ISA.noarch 0:0.36-1010.el7                   perl-Class-Load.noarch 0:0.20-3.el7
  perl-Class-Singleton.noarch 0:1.4-14.el7                   perl-Convert-ASN1.noarch 0:0.26-4.el7                     perl-DBD-MySQL.x86_64 0:4.023-6.0.1.el7                 perl-DBD-Pg.x86_64 0:2.19.3-4.el7
  perl-DBIx-Simple.noarch 0:1.35-7.el7                       perl-Data-OptList.noarch 0:0.107-9.el7                    perl-Date-Calc.noarch 0:6.3-14.el7                      perl-Date-Manip.noarch 0:6.41-2.el7
  perl-DateTime.x86_64 2:1.04-6.el7                          perl-DateTime-Format-DateParse.noarch 0:0.05-5.el7        perl-DateTime-Locale.noarch 0:0.45-6.el7                perl-DateTime-TimeZone.noarch 0:1.70-1.el7
  perl-Devel-Symdump.noarch 1:2.10-2.el7                     perl-Digest-SHA1.x86_64 0:2.13-9.el7                      perl-Env.noarch 0:1.04-2.el7                            perl-ExtUtils-CBuilder.noarch 1:0.28.2.6-293.el7
  perl-ExtUtils-Embed.noarch 0:1.30-293.el7                  perl-FCGI.x86_64 1:0.74-8.el7                             perl-File-CheckTree.noarch 0:4.42-3.el7                 perl-File-Fetch.noarch 0:0.42-2.el7
  perl-Font-AFM.noarch 0:1.20-13.el7                         perl-FreezeThaw.noarch 0:0.5001-10.el7                    perl-GD.x86_64 0:2.49-3.el7                             perl-GSSAPI.x86_64 0:0.28-9.el7
  perl-HTML-Format.noarch 0:2.10-7.el7                       perl-HTML-Tree.noarch 1:5.03-2.el7                        perl-IO-stringy.noarch 0:2.110-22.el7                   perl-IPC-Cmd.noarch 1:0.80-4.el7
  perl-JSON.noarch 0:2.59-2.el7                              perl-JSON-PP.noarch 0:2.27202-2.el7                       perl-LDAP.noarch 1:0.56-6.el7                           perl-LWP-Protocol-https.noarch 0:6.04-4.el7
  perl-List-MoreUtils.x86_64 0:0.33-9.el7                    perl-Locale-Codes.noarch 0:3.26-2.el7                     perl-Locale-Maketext.noarch 0:1.23-3.el7                perl-Locale-Maketext-Simple.noarch 1:0.21-293.el7
  perl-Log-Message.noarch 1:0.08-3.el7                       perl-Log-Message-Simple.noarch 0:0.10-2.el7               perl-Module-Build.noarch 2:0.40.05-2.el7                perl-Module-CoreList.noarch 1:2.76.02-293.el7
  perl-Module-Implementation.noarch 0:0.06-6.el7             perl-Module-Load.noarch 1:0.24-3.el7                      perl-Module-Load-Conditional.noarch 0:0.54-3.el7        perl-Module-Loaded.noarch 1:0.08-293.el7
  perl-Module-Metadata.noarch 0:1.000018-2.el7               perl-Module-Pluggable.noarch 1:4.8-3.el7                  perl-Module-Runtime.noarch 0:0.013-4.el7                perl-Module-Signature.noarch 0:0.73-2.el7
  perl-Mozilla-CA.noarch 0:20130114-5.el7                    perl-Newt.x86_64 0:1.08-36.el7                            perl-Object-Accessor.noarch 1:0.42-293.el7              perl-PAR-Dist.noarch 0:0.49-2.el7
  perl-Package-DeprecationManager.noarch 0:0.13-7.el7        perl-Package-Stash.noarch 0:0.34-2.el7                    perl-Package-Stash-XS.x86_64 0:0.26-3.el7               perl-Params-Check.noarch 1:0.38-2.el7
  perl-Params-Util.x86_64 0:1.07-6.el7                       perl-Params-Validate.x86_64 0:1.08-4.el7                  perl-Parse-CPAN-Meta.noarch 1:1.4404-5.el7              perl-Perl-OSType.noarch 0:1.003-3.el7
  perl-Perl4-CoreLibs.noarch 0:0.003-7.el7                   perl-Pod-Checker.noarch 0:1.60-2.el7                      perl-Pod-Coverage.noarch 0:0.23-3.el7                   perl-Pod-LaTeX.noarch 0:0.61-2.el7
  perl-Pod-Parser.noarch 0:1.61-2.el7                        perl-Pod-Plainer.noarch 0:1.03-4.el7                      perl-SGMLSpm.noarch 0:1.03ii-31.el7                     perl-SNMP_Session.noarch 0:1.13-5.el7
  perl-String-ShellQuote.noarch 0:1.04-10.el7                perl-Sub-Install.noarch 0:0.926-6.el7                     perl-Sys-CPU.x86_64 0:0.54-4.el7                        perl-Sys-Guestfs.x86_64 1:1.38.2-12.0.1.el7
  perl-Sys-MemInfo.x86_64 0:0.91-7.el7                       perl-Sys-Virt.x86_64 0:4.5.0-2.el7                        perl-Term-UI.noarch 0:0.36-2.el7                        perl-Test-Pod.noarch 0:1.48-3.el7
  perl-Test-Pod-Coverage.noarch 0:1.08-21.el7                perl-Test-Simple.noarch 0:0.98-243.el7                    perl-Text-Diff.noarch 0:1.41-5.el7                      perl-Text-Soundex.x86_64 0:3.04-4.el7
  perl-Text-Unidecode.noarch 0:0.04-20.el7                   perl-Time-Piece.x86_64 0:1.20.1-293.el7                   perl-Try-Tiny.noarch 0:0.12-2.el7                       perl-Version-Requirements.noarch 0:0.101022-244.el7
  perl-XML-Dumper.noarch 0:0.81-17.el7                       perl-XML-Filter-BufferText.noarch 0:1.01-17.el7           perl-XML-Grove.noarch 0:0.46alpha-52.el7                perl-XML-SAX-Writer.noarch 0:0.53-4.el7
  perl-XML-Twig.noarch 0:3.44-2.el7                          perl-XML-Writer.noarch 0:0.623-3.el7                      perl-XML-XPath.noarch 0:1.13-22.el7                     perl-YAML.noarch 0:0.84-5.el7
  perl-YAML-Tiny.noarch 0:1.51-6.el7                         perl-autodie.noarch 0:2.16-2.el7                          perl-core.x86_64 0:5.16.3-293.el7                       perl-gettext.x86_64 0:1.05-28.el7
  perl-homedir.noarch 0:1.008010-4.el7                       perl-libintl.x86_64 0:1.20-12.el7                         perl-libxml-perl.noarch 0:0.08-19.el7                   perl-local-lib.noarch 0:1.008010-4.el7
  perltidy.noarch 0:20121207-3.el7

作为依赖被安装:
  glusterfs-cli.x86_64 0:3.12.2-18.el7                                           glusterfs-client-xlators.x86_64 0:3.12.2-18.el7                              libgnat.x86_64 0:4.8.5-36.0.1.el7
  libgnat-devel.x86_64 0:4.8.5-36.0.1.el7                                        libgpg-error-devel.x86_64 0:1.12-3.el7                                       libobjc.x86_64 0:4.8.5-36.0.1.el7
  libvirt-bash-completion.x86_64 0:4.5.0-10.el7                                  libvirt-daemon-driver-storage-core.x86_64 0:4.5.0-10.el7                     libvirt-daemon-driver-storage-disk.x86_64 0:4.5.0-10.el7
  libvirt-daemon-driver-storage-gluster.x86_64 0:4.5.0-10.el7                    libvirt-daemon-driver-storage-iscsi.x86_64 0:4.5.0-10.el7                    libvirt-daemon-driver-storage-logical.x86_64 0:4.5.0-10.el7
  libvirt-daemon-driver-storage-mpath.x86_64 0:4.5.0-10.el7                      libvirt-daemon-driver-storage-rbd.x86_64 0:4.5.0-10.el7                      libvirt-daemon-driver-storage-scsi.x86_64 0:4.5.0-10.el7
  libvirt-libs.x86_64 0:4.5.0-10.el7                                             openjade.x86_64 0:1.3.2-45.el7                                               opensp.x86_64 0:1.5.2-19.el7
  pcp-selinux.x86_64 0:4.1.0-4.el7                                               postgresql-libs.x86_64 0:9.2.24-1.el7_5                                      squashfs-tools.x86_64 0:4.3-0.21.gitaae0aff4.el7

更新完毕:
  bison.x86_64 0:3.0.4-2.el7                 gcc.x86_64 0:4.8.5-36.0.1.el7                    gcc-c++.x86_64 0:4.8.5-36.0.1.el7                 gcc-gfortran.x86_64 0:4.8.5-36.0.1.el7           libgcrypt.x86_64 0:1.5.3-14.el7
  make.x86_64 1:3.82-23.el7                  ncurses.x86_64 0:5.9-14.20130511.el7_4           ncurses-base.noarch 0:5.9-14.20130511.el7_4       ncurses-libs.x86_64 0:5.9-14.20130511.el7_4      perl.x86_64 4:5.16.3-293.el7
  perl-Digest-SHA.x86_64 1:5.85-4.el7        perl-ExtUtils-Install.noarch 0:1.58-293.el7      perl-ExtUtils-ParseXS.noarch 1:3.18-3.el7         perl-Getopt-Long.noarch 0:2.40-3.el7             perl-Git.noarch 0:1.8.3.1-19.el7
  perl-HTTP-Daemon.noarch 0:6.01-8.el7       perl-IO-Socket-IP.noarch 0:0.21-5.el7            perl-IO-Socket-SSL.noarch 0:1.94-7.el7            perl-IO-Zlib.noarch 1:1.10-293.el7               perl-Net-DNS.x86_64 0:0.72-6.el7
  perl-Net-SSLeay.x86_64 0:1.55-6.el7        perl-PCP-PMDA.x86_64 0:4.1.0-4.el7               perl-Package-Constants.noarch 1:0.02-293.el7      perl-Pod-Escapes.noarch 1:1.04-293.el7           perl-Socket.x86_64 0:2.010-4.el7
  perl-Test-Harness.noarch 0:3.28-3.el7      perl-devel.x86_64 4:5.16.3-293.el7               perl-hivex.x86_64 0:1.3.10-6.9.el7                perl-libs.x86_64 4:5.16.3-293.el7                perl-macros.x86_64 4:5.16.3-293.el7
  perl-version.x86_64 3:0.99.07-3.el7

作为依赖被升级:
  cpp.x86_64 0:4.8.5-36.0.1.el7                              git.x86_64 0:1.8.3.1-19.el7                                 glusterfs.x86_64 0:3.12.2-18.el7                       glusterfs-api.x86_64 0:3.12.2-18.el7
  glusterfs-fuse.x86_64 0:3.12.2-18.el7                      glusterfs-libs.x86_64 0:3.12.2-18.el7                       glusterfs-rdma.x86_64 0:3.12.2-18.el7                  hivex.x86_64 0:1.3.10-6.9.el7
  libgcc.x86_64 0:4.8.5-36.0.1.el7                           libgfortran.x86_64 0:4.8.5-36.0.1.el7                       libgomp.x86_64 0:4.8.5-36.0.1.el7                      libguestfs.x86_64 1:1.38.2-12.0.1.el7
  libquadmath.x86_64 0:4.8.5-36.0.1.el7                      libquadmath-devel.x86_64 0:4.8.5-36.0.1.el7                 libstdc++.x86_64 0:4.8.5-36.0.1.el7                    libstdc++-devel.x86_64 0:4.8.5-36.0.1.el7
  libvirt.x86_64 0:4.5.0-10.el7                              libvirt-client.x86_64 0:4.5.0-10.el7                        libvirt-daemon.x86_64 0:4.5.0-10.el7                   libvirt-daemon-config-network.x86_64 0:4.5.0-10.el7
  libvirt-daemon-config-nwfilter.x86_64 0:4.5.0-10.el7       libvirt-daemon-driver-interface.x86_64 0:4.5.0-10.el7       libvirt-daemon-driver-lxc.x86_64 0:4.5.0-10.el7        libvirt-daemon-driver-network.x86_64 0:4.5.0-10.el7
  libvirt-daemon-driver-nodedev.x86_64 0:4.5.0-10.el7        libvirt-daemon-driver-nwfilter.x86_64 0:4.5.0-10.el7        libvirt-daemon-driver-qemu.x86_64 0:4.5.0-10.el7       libvirt-daemon-driver-secret.x86_64 0:4.5.0-10.el7
  libvirt-daemon-driver-storage.x86_64 0:4.5.0-10.el7        libvirt-daemon-kvm.x86_64 0:4.5.0-10.el7                    openssl.x86_64 1:1.0.2k-16.0.1.el7                     openssl-libs.x86_64 1:1.0.2k-16.0.1.el7
  pcp.x86_64 0:4.1.0-4.el7                                   pcp-conf.x86_64 0:4.1.0-4.el7                               pcp-libs.x86_64 0:4.1.0-4.el7                          python-pcp.x86_64 0:4.1.0-4.el7
  supermin5.x86_64 0:5.1.19-1.el7

完毕!

2.使用命令检查是否安装有MySQL Server包

[root@localhost ~]# rpm -qa | grep mysql


[root@localhost ~]# rpm -aq | grep mysql
mysql-community-libs-5.6.23-3.el7.x86_64
qt-mysql-4.8.5-8.0.1.el7.x86_64
akonadi-mysql-1.9.2-4.0.1.el7.x86_64
mysql-community-common-5.6.23-3.el7.x86_64
mysql-community-server-5.6.23-3.el7.x86_64
mysql-community-client-5.6.23-3.el7.x86_64

删除操作有以下两种
rpm -e mysql-libs //普通删除模式
rpm -e –nodeps mysql-lib// 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除

[root@localhost ~]# rpm -e --nodeps mysql-community-libs
[root@localhost ~]# rpm -e --nodeps akonadi-mysql
[root@localhost ~]# rpm -e --nodeps mysql-community-common
[root@localhost ~]# rpm -e --nodeps mysql-community-server
[root@localhost ~]# rpm -e --nodeps mysql-community-client
[root@localhost ~]# rpm -e --nodeps qt-mysql
[root@localhost ~]# rpm -aq | grep mysql

3.下载源码文件mysql-5.7.26.tar.gz并上传到服务器的/soft目录中

[root@localhost soft]# ls -lrt
总用量 79960
-rw-r--r--. 1 root root 54056899 5月  30 16:36 mysql-5.7.26.tar.gz

4.下载boost包并上传到服务器的/soft目录中

5. 创建mysql用户与用户组

[root@localhost /]# groupadd mysql
[root@localhost /]# useradd -r -g mysql -s /bin/false mysql

因为用户只用于所有权目的,而不是登录目的,useradd命令使用-r与-s /bin/false选项来创建一个用户没有登录服务器主机的权限。

6.创建并修改/mysqlsoft/mysql与/mysqldata/mysql目录权限

[root@localhost /]# mkdir -p /mysqlsoft/mysql
[root@localhost /]# mkdir -p /mysqldata/mysql
[root@localhost /]# chown -R mysql:mysql /mysqlsoft
[root@localhost /]# chown -R mysql:mysql /mysqldata/mysql
[root@localhost /]# chmod -R 775 /mysqlsoft/mysql
[root@localhost /]# chmod -R 775 /mysqldata/mysql

7.解压boost软件包与MySQL源码包

[root@localhost soft]# unzip boost_1_59_0.zip
[root@localhost soft]# ls -lrt
总用量 201556
drwx------. 8 root root      4096 8月  12 2015 boost_1_59_0
-rw-r--r--. 1 root root  54056899 5月  30 16:36 mysql-5.7.26.tar.gz
-rw-r--r--. 1 root root 124506259 6月  13 21:07 boost_1_59_0.zip

将MySQL文件解压到/soft目录下

[root@localhost soft]# tar zxvf mysql-5.7.26.tar.gz
[root@localhost soft]# ls -lrt
总用量 201560
drwx------.  8 root root       4096 8月  12 2015 boost_1_59_0
drwxr-xr-x. 35 7161 31415      4096 4月  13 21:46 mysql-5.7.26
-rw-r--r--.  1 root root   54056899 5月  30 16:36 mysql-5.7.26.tar.gz
-rw-r--r--.  1 root root   27816900 6月  10 11:32 docker-17.03.0-ce.tgz
-rw-r--r--.  1 root root  124506259 6月  13 21:07 boost_1_59_0.zip
总用量 4


[root@localhost soft]# chown -R mysql:mysql mysql-5.7.26
[root@localhost soft]# chmod -R 775 mysql-5.7.26

8.预编译MySQL

[root@localhost soft]# su - mysql
上一次登录:一 6月 17 12:21:54 CST 2019pts/0 上
-bash-4.2$ cd /soft/mysql-5.7.26/

-bash-4.2$ cmake . -DCMAKE_INSTALL_PREFIX=/mysqlsoft/mysql \
> -DMYSQL_DATADIR=/mysqldata/mysql \
> -DMYSQL_UNIX_ADDR=/mysqlsoft/mysql/mysql.sock \
> -DWITH_BOOST=../boost_1_59_0 \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DWITH_FEDERATED_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_MYISAM_STORAGE_ENGINE=1 \
> -DENABLED_LOCAL_INFILE=1 \
> -DENABLE_DTRACE=0 \
> -DDEFAULT_CHARSET=utf8mb4 \
> -DDEFAULT_COLLATION=utf8mb4_general_ci \
> -DWITH_EMBEDDED_SERVER=1

....................
-- Found PkgConfig: /bin/pkg-config (found version "0.27.1")
-- checking for module 'libtirpc'
--   package 'libtirpc' not found
-- Performing Test HAVE_STRUCT_SOCKADDR_SA_LEN
-- Performing Test HAVE_STRUCT_SOCKADDR_SA_LEN - Failed
-- Performing Test HAVE_STRUCT_IFREQ_IFR_NAME
-- Performing Test HAVE_STRUCT_IFREQ_IFR_NAME - Success
-- Performing Test HAVE_XDR_OPS_X_PUTINT32
-- Performing Test HAVE_XDR_OPS_X_PUTINT32 - Success
-- Performing Test HAVE_XDR_OPS_X_GETINT32
-- Performing Test HAVE_XDR_OPS_X_GETINT32 - Success
-- Performing Test HAVE___CONST
-- Performing Test HAVE___CONST - Success
-- Performing Test HAVE_RPC_INLINE_T
-- Performing Test HAVE_RPC_INLINE_T - Failed
-- Performing Test OLD_XDR
-- Performing Test OLD_XDR - Failed
-- Performing Test X_GETPOSTN_NOT_USE_CONST
-- Performing Test X_GETPOSTN_NOT_USE_CONST - Success
-- Performing Test HAS_INCOMPATIBLE_POINTER_TYPES
-- Performing Test HAS_INCOMPATIBLE_POINTER_TYPES - Failed
-- Performing Test X_PUTLONG_NOT_USE_CONST
-- Performing Test X_PUTLONG_NOT_USE_CONST - Failed
-- checking for module 'libtirpc'
--   package 'libtirpc' not found
-- RPC_INCLUDE_DIRS /usr/include
-- Using Boost headers from /soft/boost_1_59_0
-- Performing Test CXX_HAVE_SIGN_COMPARE
-- Performing Test CXX_HAVE_SIGN_COMPARE - Success
-- Performing Test CXX_HAVE_UNUSED_VARIABLE
-- Performing Test CXX_HAVE_UNUSED_VARIABLE - Success
-- MYSQLX - Text log of protobuf messages enabled
-- Performing Test HAVE_UNUSED_PARAMETER
-- Performing Test HAVE_UNUSED_PARAMETER - Success
-- Googletest was not found. gtest-based unit tests will be disabled. You can run cmake . -DENABLE_DOWNLOADS=1 to automatically download and build required components from source.
-- If you are inside a firewall, you may need to use an https proxy: export https_proxy=http://example.com:80
-- Performing Test HAVE_MISLEADING_INDENTATION
-- Performing Test HAVE_MISLEADING_INDENTATION - Failed
-- executable target mysqld debug_target /soft/debug/sql/mysqld
-- Library mysqlserver depends on OSLIBS -lpthread;m;rt;crypt;dl
-- MERGE_CONVENIENCE_LIBRARIES TARGET mysqlserver
-- MERGE_CONVENIENCE_LIBRARIES LIBS dbug;strings;regex;mysys;mysys_ssl;vio;zlib;yassl;taocrypt;crypt;dl;archive_embedded;blackhole_embedded;csv_embedded;federated_embedded;heap_embedded;innobase_embedded;lz4_lib;myisam_embedded;myisammrg_embedded;partition_embedded;ngram_parser_embedded;sql_embedded
-- MERGE_CONVENIENCE_LIBRARIES MYLIBS dbug;strings;regex;mysys;mysys_ssl;vio;zlib;yassl;taocrypt;archive_embedded;blackhole_embedded;csv_embedded;federated_embedded;heap_embedded;innobase_embedded;lz4_lib;myisam_embedded;myisammrg_embedded;partition_embedded;ngram_parser_embedded;sql_embedded
-- library target mysqlserver debug_target /soft/debug/archive_output_directory/libmysqld.a
-- INSTALL mysqlclient.pc lib/pkgconfig
-- Skipping deb packaging on unsupported platform .
-- CMAKE_BUILD_TYPE: RelWithDebInfo
-- COMPILE_DEFINITIONS: _GNU_SOURCE;_FILE_OFFSET_BITS=64;HAVE_CONFIG_H;HAVE_LIBEVENT2
-- CMAKE_C_FLAGS:  -Wall -Wextra -Wformat-security -Wvla -Wwrite-strings -Wdeclaration-after-statement
-- CMAKE_CXX_FLAGS:  -Wall -Wextra -Wformat-security -Wvla -Woverloaded-virtual -Wno-unused-parameter
-- CMAKE_C_LINK_FLAGS:
-- CMAKE_CXX_LINK_FLAGS:
-- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- Configuring done
-- Generating done
-- Build files have been written to: /soft/mysql-5.7.26

9.编译源码,这时使用-j 6选项来使用6个进程同时进行编译

-bash-4.2$ make -j 6
.........
Building CXX object sql/CMakeFiles/sql.dir/event_data_objects.cc.o
Building CXX object sql/CMakeFiles/sql.dir/des_key_file.cc.o
Building CXX object sql/CMakeFiles/sql.dir/event_db_repository.cc.o
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/event_parse_data.cc.o
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/event_queue.cc.o
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/event_scheduler.cc.o
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/events.cc.o
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/mf_iocache.cc.o
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/mysqld.cc.o
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/mysqld_thd_manager.cc.o
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/protocol_callback.cc.o
[ 97%] Building CXX object sql/CMakeFiles/sql.dir/signal_handler.cc.o
[ 98%] Building CXX object sql/CMakeFiles/sql.dir/sql_audit.cc.o
[ 98%] Building CXX object sql/CMakeFiles/sql.dir/sql_client.cc.o
[ 98%] Building CXX object sql/CMakeFiles/sql.dir/srv_session.cc.o
[ 98%] Building CXX object sql/CMakeFiles/sql.dir/srv_session_info_service.cc.o
[ 98%] Building CXX object sql/CMakeFiles/sql.dir/srv_session_service.cc.o
[ 98%] Building CXX object sql/CMakeFiles/sql.dir/auth/sha2_password_common.cc.o
[ 98%] Building CXX object sql/CMakeFiles/sql.dir/mysqld_daemon.cc.o
Linking CXX static library ../archive_output_directory/libsql.a
[ 98%] Built target sql
Scanning dependencies of target mysqld
Scanning dependencies of target pfs_connect_attr-t
[ 99%] Building CXX object sql/CMakeFiles/mysqld.dir/main.cc.o
Linking CXX executable mysqld
[100%] [100%] [100%] Building CXX object storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/__/__/__/sql/sql_builtin.cc.o
Building CXX object storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/pfs_connect_attr-t.cc.o
Building C object storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/__/__/__/mysys/string.c.o
Linking CXX executable pfs_connect_attr-t
[100%] Built target mysqld
[100%] Built target pfs_connect_attr-t

10.安装

-bash-4.2$ make install
.................
-- Installing: /mysqlsoft/mysql/mysql-test/./t/wl6661.test
-- Installing: /mysqlsoft/mysql/mysql-test/./t/wl6711_heap_to_disk.test
-- Installing: /mysqlsoft/mysql/mysql-test/./t/wl6978.test
-- Installing: /mysqlsoft/mysql/mysql-test/./t/xa.test
-- Installing: /mysqlsoft/mysql/mysql-test/./t/xa_debug.test
-- Installing: /mysqlsoft/mysql/mysql-test/./t/xa_gtid-master.opt
-- Installing: /mysqlsoft/mysql/mysql-test/./t/xa_gtid.test
-- Installing: /mysqlsoft/mysql/mysql-test/./t/xa_prepared_binlog_off-master.opt
-- Installing: /mysqlsoft/mysql/mysql-test/./t/xa_prepared_binlog_off.test
-- Installing: /mysqlsoft/mysql/mysql-test/./t/xml.test
-- Installing: /mysqlsoft/mysql/mysql-test/./valgrind.supp
-- Installing: /mysqlsoft/mysql/mysql-test/./mtr
-- Installing: /mysqlsoft/mysql/mysql-test/./mysql-test-run
-- Installing: /mysqlsoft/mysql/mysql-test/./Makefile
-- Installing: /mysqlsoft/mysql/mysql-test/./cmake_install.cmake
-- Installing: /mysqlsoft/mysql/mysql-test/./CTestTestfile.cmake
-- Installing: /mysqlsoft/mysql/./COPYING-test
-- Installing: /mysqlsoft/mysql/./README-test
-- Up-to-date: /mysqlsoft/mysql/mysql-test/mtr
-- Up-to-date: /mysqlsoft/mysql/mysql-test/mysql-test-run
-- Installing: /mysqlsoft/mysql/mysql-test/lib/My/SafeProcess/my_safe_process
-- Up-to-date: /mysqlsoft/mysql/mysql-test/lib/My/SafeProcess/my_safe_process
-- Installing: /mysqlsoft/mysql/mysql-test/lib/My/SafeProcess/Base.pm
-- Installing: /mysqlsoft/mysql/support-files/mysqld_multi.server
-- Installing: /mysqlsoft/mysql/support-files/mysql-log-rotate
-- Installing: /mysqlsoft/mysql/support-files/magic
-- Installing: /mysqlsoft/mysql/share/aclocal/mysql.m4
-- Installing: /mysqlsoft/mysql/support-files/mysql.server

[root@localhost mysqlsoft]# chmod -R 775 mysql

11.配置mysql参数,只是设置几个简单的mysql运行参数

[root@localhost mysql]#  vi /mysqlsoft/mysql/my.cnf
[mysqld]
basedir=/mysqlsoft/mysql
datadir=/mysqldata/mysql
bind-address=0.0.0.0
user=mysql
port=3306
log-error=/mysqldata/mysql/mysql.err
pid-file=/mysqldata/mysql/mysqld.pid
socket = /mysqlsoft/mysql/mysql.sock
character-set-server=utf8mb4
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true

[root@localhost mysql]# cd bin
[root@localhost bin]# ./mysqld  --defaults-file=/mysqlsoft/mysql/my.cnf --initialize --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql --user=mysql

查看日志信息

[root@localhost mysql]# tail -f mysql.err
2019-06-17T06:28:34.137849Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-06-17T06:28:35.342112Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-06-17T06:28:35.465187Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 2296e390-90c9-11e9-9a9f-005056a390e6.
2019-06-17T06:28:35.471829Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-06-17T06:28:35.474533Z 1 [Note] A temporary password is generated for root@localhost: Ldp#HUPR(7hH

其中[Note] A temporary password is generated for root@localhost:后面跟的是mysql数据库登录的临时密码,各人安装生成的临时密码不一样。可以看到到日志文件没有报错,而且有了临时密码,表示初始化成功。

12.如果想服务能够部署自动支持安全连接,使用mysql_ssl_rsa_setup工具来创建缺省SSL与RSA文件

[root@localhost bin]# ./mysql_ssl_rsa_setup --datadir=/mysqldata/mysql
Generating a 2048 bit RSA private key
...........................+++
.......+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
.......................................................................................................+++
...................................................................................................................................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
.....................................+++
...........+++
writing new private key to 'client-key.pem'
-----

13.启动MySQL

[root@localhost bin]# ./mysqld_safe --user=mysql &
[1] 19442
[root@localhost bin]# 2019-06-17T06:34:01.654203Z mysqld_safe Logging to '/mysqldata/mysql/mysql.err'.
2019-06-17T06:34:01.750954Z mysqld_safe Starting mysqld daemon with databases from /mysqldata/mysql

14.配置自动启动MySQL

[root@localhost ~]# cp /mysqlsoft/mysql/support-files/mysql.server  /etc/init.d/mysqld
[root@localhost ~]# chmod 755 /etc/init.d/mysqld
[root@localhost ~]# systemctl enable mysqld
mysqld.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mysqld on

14.配置环境变量

[root@localhost ~]# vi /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

export MYSQL_HOME=/mysqlsoft/mysql/
export PATH=$PATH:$MYSQL_HOME/bin

15.登录MySQL并修改root用户密码

-bash-4.2$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26

Copyright (c) 2000, 2019, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

到此使用源码来安装MySQL就完成了。

Install MySQL 5.7 in the Docker

Install Docker on Oracle Linux 7
在Oracle Linux 7.1上安装Docker
1.首先使用正确的yum设置来升级Oracle Linux 7.1实例。为了安装最新的Docker版本(18.9.1.ce),需要ol7_latest,ol7_uekr4与ol7_addons启用

[root@localhost /]# cd /etc/yum.repos.d/
[root@localhost /]#wget http://yum.oracle.com/public-yum-ol7.repo
[root@localhost yum.repos.d]# vi public-yum-ol7.repo
[ol7_latest]
name=Oracle Linux $releasever Latest ($basearch)
baseurl=http://yum.oracle.com/repo/OracleLinux/OL7/latest/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1

[ol7_UEKR4]
name=Latest Unbreakable Enterprise Kernel Release 4 for Oracle Linux $releasever ($basearch)
baseurl=http://yum.oracle.com/repo/OracleLinux/OL7/UEKR4/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1

[ol7_addons]
name=Oracle Linux $releasever Add ons ($basearch)
baseurl=http://yum.oracle.com/repo/OracleLinux/OL7/addons/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1

2.开始安装docker

[root@localhost yum.repos.d]# yum install docker-engine
Loaded plugins: langpacks
ol7_UEKR4                                                                                                                                                                                                            | 2.5 kB  00:00:00
ol7_addons                                                                                                                                                                                                           | 1.2 kB  00:00:00
ol7_latest                                                                                                                                                                                                           | 2.7 kB  00:00:00
(1/4): ol7_addons/x86_64/updateinfo                                                                                                                                                                                  |  60 kB  00:00:00
(2/4): ol7_UEKR4/x86_64/updateinfo                                                                                                                                                                                   |  82 kB  00:00:01
(3/4): ol7_addons/x86_64/primary                                                                                                                                                                                     |  82 kB  00:00:02
(4/4): ol7_UEKR4/x86_64/primary_db                                                                                                                                                                                   | 4.0 MB  00:03:38
ol7_addons                                                                                                                                                                                                                          328/328
Resolving Dependencies
--> Running transaction check
---> Package docker-engine.x86_64 0:18.09.1.ol-1.0.5.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.77 for package: docker-engine-18.09.1.ol-1.0.5.el7.x86_64
--> Processing Dependency: libseccomp >= 2.3 for package: docker-engine-18.09.1.ol-1.0.5.el7.x86_64
--> Processing Dependency: containerd for package: docker-engine-18.09.1.ol-1.0.5.el7.x86_64
--> Processing Dependency: docker-cli for package: docker-engine-18.09.1.ol-1.0.5.el7.x86_64
--> Processing Dependency: runc for package: docker-engine-18.09.1.ol-1.0.5.el7.x86_64
--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_97)(64bit) for package: docker-engine-18.09.1.ol-1.0.5.el7.x86_64
--> Processing Dependency: libsystemd.so.0(LIBSYSTEMD_209)(64bit) for package: docker-engine-18.09.1.ol-1.0.5.el7.x86_64
--> Processing Dependency: libsystemd.so.0()(64bit) for package: docker-engine-18.09.1.ol-1.0.5.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.77-5.el7 will be installed
--> Processing Dependency: selinux-policy-base >= 3.13.1-216.el7 for package: 2:container-selinux-2.77-5.el7.noarch
--> Processing Dependency: selinux-policy >= 3.13.1-216.el7 for package: 2:container-selinux-2.77-5.el7.noarch
--> Processing Dependency: policycoreutils >= 2.5-11 for package: 2:container-selinux-2.77-5.el7.noarch
--> Processing Dependency: selinux-policy-targeted >= 3.13.1-216.el7 for package: 2:container-selinux-2.77-5.el7.noarch
---> Package containerd.x86_64 0:1.2.0-1.0.5.el7 will be installed
---> Package device-mapper-libs.x86_64 7:1.02.93-3.el7 will be updated
--> Processing Dependency: device-mapper-libs = 7:1.02.93-3.el7 for package: 7:device-mapper-1.02.93-3.el7.x86_64
---> Package device-mapper-libs.x86_64 7:1.02.149-10.0.3.el7_6.8 will be an update
---> Package docker-cli.x86_64 0:18.09.1.ol-1.0.5.el7 will be installed
---> Package libseccomp.x86_64 0:2.1.1-2.el7 will be updated
---> Package libseccomp.x86_64 0:2.3.1-3.el7 will be an update
---> Package runc.x86_64 0:1.0.0-19.rc5.git4bb1fe4.0.3.el7 will be installed
--> Processing Dependency: criu for package: runc-1.0.0-19.rc5.git4bb1fe4.0.3.el7.x86_64
---> Package systemd-libs.x86_64 0:208-20.0.1.el7 will be updated
--> Processing Dependency: systemd-libs = 208-20.0.1.el7 for package: systemd-208-20.0.1.el7.x86_64
---> Package systemd-libs.x86_64 0:219-62.0.4.el7_6.7 will be an update
--> Processing Dependency: liblz4.so.1()(64bit) for package: systemd-libs-219-62.0.4.el7_6.7.x86_64
--> Running transaction check
---> Package criu.x86_64 0:3.9-5.el7 will be installed
--> Processing Dependency: libprotobuf-c.so.1(LIBPROTOBUF_C_1.0.0)(64bit) for package: criu-3.9-5.el7.x86_64
--> Processing Dependency: libnl-3.so.200(libnl_3)(64bit) for package: criu-3.9-5.el7.x86_64
--> Processing Dependency: libprotobuf-c.so.1()(64bit) for package: criu-3.9-5.el7.x86_64
--> Processing Dependency: libnet.so.1()(64bit) for package: criu-3.9-5.el7.x86_64
---> Package device-mapper.x86_64 7:1.02.93-3.el7 will be updated
--> Processing Dependency: device-mapper = 7:1.02.93-3.el7 for package: 7:device-mapper-event-1.02.93-3.el7.x86_64
---> Package device-mapper.x86_64 7:1.02.149-10.0.3.el7_6.8 will be an update
---> Package lz4.x86_64 0:1.7.5-2.0.1.el7 will be installed
---> Package policycoreutils.x86_64 0:2.2.5-15.0.1.el7 will be updated
--> Processing Dependency: policycoreutils = 2.2.5-15.0.1.el7 for package: policycoreutils-python-2.2.5-15.0.1.el7.x86_64
---> Package policycoreutils.x86_64 0:2.5-29.0.1.el7_6.1 will be an update
--> Processing Dependency: libsemanage >= 2.5-14 for package: policycoreutils-2.5-29.0.1.el7_6.1.x86_64
--> Processing Dependency: libsepol >= 2.5-10 for package: policycoreutils-2.5-29.0.1.el7_6.1.x86_64
--> Processing Dependency: libselinux-utils >= 2.5-14 for package: policycoreutils-2.5-29.0.1.el7_6.1.x86_64
--> Processing Dependency: libsepol.so.1(LIBSEPOL_1.1)(64bit) for package: policycoreutils-2.5-29.0.1.el7_6.1.x86_64
--> Processing Dependency: libsemanage.so.1(LIBSEMANAGE_1.1)(64bit) for package: policycoreutils-2.5-29.0.1.el7_6.1.x86_64
--> Processing Dependency: libsepol.so.1(LIBSEPOL_1.0)(64bit) for package: policycoreutils-2.5-29.0.1.el7_6.1.x86_64
---> Package selinux-policy.noarch 0:3.13.1-23.0.1.el7 will be updated
---> Package selinux-policy.noarch 0:3.13.1-229.0.3.el7_6.12 will be an update
---> Package selinux-policy-targeted.noarch 0:3.13.1-23.0.1.el7 will be updated
---> Package selinux-policy-targeted.noarch 0:3.13.1-229.0.3.el7_6.12 will be an update
---> Package systemd.x86_64 0:208-20.0.1.el7 will be updated
--> Processing Dependency: systemd = 208-20.0.1.el7 for package: libgudev1-208-20.0.1.el7.x86_64
--> Processing Dependency: systemd = 208-20.0.1.el7 for package: systemd-sysv-208-20.0.1.el7.x86_64
--> Processing Dependency: systemd = 208-20.0.1.el7 for package: systemd-python-208-20.0.1.el7.x86_64
---> Package systemd.x86_64 0:219-62.0.4.el7_6.7 will be an update
--> Processing Dependency: kmod >= 18-4 for package: systemd-219-62.0.4.el7_6.7.x86_64
--> Processing Dependency: libcryptsetup.so.12(CRYPTSETUP_2.0)(64bit) for package: systemd-219-62.0.4.el7_6.7.x86_64
--> Processing Dependency: libcryptsetup.so.12()(64bit) for package: systemd-219-62.0.4.el7_6.7.x86_64
--> Running transaction check
---> Package cryptsetup-libs.x86_64 0:1.6.6-3.el7 will be updated
--> Processing Dependency: cryptsetup-libs = 1.6.6-3.el7 for package: cryptsetup-python-1.6.6-3.el7.x86_64
--> Processing Dependency: cryptsetup-libs(x86-64) = 1.6.6-3.el7 for package: cryptsetup-1.6.6-3.el7.x86_64
---> Package cryptsetup-libs.x86_64 0:2.0.3-3.el7 will be an update
---> Package device-mapper-event.x86_64 7:1.02.93-3.el7 will be updated
--> Processing Dependency: device-mapper-event = 7:1.02.93-3.el7 for package: 7:lvm2-libs-2.02.115-3.el7.x86_64
---> Package device-mapper-event.x86_64 7:1.02.149-10.0.3.el7_6.8 will be an update
--> Processing Dependency: device-mapper-event-libs = 7:1.02.149-10.0.3.el7_6.8 for package: 7:device-mapper-event-1.02.149-10.0.3.el7_6.8.x86_64
---> Package kmod.x86_64 0:14-10.el7 will be updated
---> Package kmod.x86_64 0:20-23.0.1.el7 will be an update
---> Package libgudev1.x86_64 0:208-20.0.1.el7 will be updated
---> Package libgudev1.x86_64 0:219-62.0.4.el7_6.7 will be an update
--> Processing Dependency: glib2 >= 2.42 for package: libgudev1-219-62.0.4.el7_6.7.x86_64
---> Package libnet.x86_64 0:1.1.6-7.el7 will be installed
---> Package libnl3.x86_64 0:3.2.21-8.0.1.el7 will be updated
--> Processing Dependency: libnl3 = 3.2.21-8.0.1.el7 for package: libnl3-cli-3.2.21-8.0.1.el7.x86_64
---> Package libnl3.x86_64 0:3.2.28-4.el7 will be an update
---> Package libselinux-utils.x86_64 0:2.2.2-6.el7 will be updated
---> Package libselinux-utils.x86_64 0:2.5-14.1.el7 will be an update
--> Processing Dependency: libselinux(x86-64) = 2.5-14.1.el7 for package: libselinux-utils-2.5-14.1.el7.x86_64
---> Package libsemanage.x86_64 0:2.1.10-16.el7 will be updated
--> Processing Dependency: libsemanage = 2.1.10-16.el7 for package: libsemanage-python-2.1.10-16.el7.x86_64
---> Package libsemanage.x86_64 0:2.5-14.el7 will be an update
---> Package libsepol.x86_64 0:2.1.9-3.el7 will be updated
---> Package libsepol.x86_64 0:2.5-10.el7 will be an update
---> Package policycoreutils-python.x86_64 0:2.2.5-15.0.1.el7 will be updated
---> Package policycoreutils-python.x86_64 0:2.5-29.0.1.el7_6.1 will be an update
--> Processing Dependency: setools-libs >= 3.3.8-4 for package: policycoreutils-python-2.5-29.0.1.el7_6.1.x86_64
---> Package protobuf-c.x86_64 0:1.0.2-3.el7 will be installed
---> Package systemd-python.x86_64 0:208-20.0.1.el7 will be updated
---> Package systemd-python.x86_64 0:219-62.0.4.el7_6.7 will be an update
---> Package systemd-sysv.x86_64 0:208-20.0.1.el7 will be updated
---> Package systemd-sysv.x86_64 0:219-62.0.4.el7_6.7 will be an update
--> Running transaction check
---> Package cryptsetup.x86_64 0:1.6.6-3.el7 will be updated
---> Package cryptsetup.x86_64 0:2.0.3-3.el7 will be an update
---> Package cryptsetup-python.x86_64 0:1.6.6-3.el7 will be updated
---> Package cryptsetup-python.x86_64 0:2.0.3-3.el7 will be an update
---> Package device-mapper-event-libs.x86_64 7:1.02.93-3.el7 will be updated
---> Package device-mapper-event-libs.x86_64 7:1.02.149-10.0.3.el7_6.8 will be an update
---> Package glib2.x86_64 0:2.40.0-4.el7 will be updated
---> Package glib2.x86_64 0:2.56.1-4.el7_6 will be an update
---> Package libnl3-cli.x86_64 0:3.2.21-8.0.1.el7 will be updated
---> Package libnl3-cli.x86_64 0:3.2.28-4.el7 will be an update
---> Package libselinux.x86_64 0:2.2.2-6.el7 will be updated
--> Processing Dependency: libselinux = 2.2.2-6.el7 for package: libselinux-python-2.2.2-6.el7.x86_64
---> Package libselinux.x86_64 0:2.5-14.1.el7 will be an update
---> Package libsemanage-python.x86_64 0:2.1.10-16.el7 will be updated
---> Package libsemanage-python.x86_64 0:2.5-14.el7 will be an update
---> Package lvm2-libs.x86_64 7:2.02.115-3.el7 will be updated
--> Processing Dependency: lvm2-libs = 7:2.02.115-3.el7 for package: 7:lvm2-2.02.115-3.el7.x86_64
--> Processing Dependency: lvm2-libs = 7:2.02.115-3.el7 for package: 7:lvm2-python-libs-2.02.115-3.el7.x86_64
---> Package lvm2-libs.x86_64 7:2.02.180-10.0.3.el7_6.8 will be an update
---> Package setools-libs.x86_64 0:3.3.7-46.el7 will be updated
---> Package setools-libs.x86_64 0:3.3.8-4.el7 will be an update
--> Running transaction check
---> Package libselinux-python.x86_64 0:2.2.2-6.el7 will be updated
---> Package libselinux-python.x86_64 0:2.5-14.1.el7 will be an update
---> Package lvm2.x86_64 7:2.02.115-3.el7 will be updated
---> Package lvm2.x86_64 7:2.02.180-10.0.3.el7_6.8 will be an update
--> Processing Dependency: device-mapper-persistent-data >= 0.7.0-0.1.rc6 for package: 7:lvm2-2.02.180-10.0.3.el7_6.8.x86_64
---> Package lvm2-python-libs.x86_64 7:2.02.115-3.el7 will be updated
---> Package lvm2-python-libs.x86_64 7:2.02.180-10.0.3.el7_6.8 will be an update
--> Running transaction check
---> Package device-mapper-persistent-data.x86_64 0:0.4.1-2.el7 will be updated
---> Package device-mapper-persistent-data.x86_64 0:0.7.3-3.el7 will be an update
--> Processing Conflict: systemd-219-62.0.4.el7_6.7.x86_64 conflicts initscripts < 9.49.28-1
--> Restarting Dependency Resolution with new changes.
--> Running transaction check
---> Package initscripts.x86_64 0:9.49.24-1.0.1.el7 will be updated
---> Package initscripts.x86_64 0:9.49.46-1.0.1.el7 will be an update
--> Processing Conflict: initscripts-9.49.46-1.0.1.el7.x86_64 conflicts redhat-release < 7.5-0.11
--> Restarting Dependency Resolution with new changes.
--> Running transaction check
---> Package redhat-release-server.x86_64 1:7.1-1.0.2.el7 will be updated
---> Package redhat-release-server.x86_64 1:7.6-4.0.1.el7 will be an update
--> Processing Conflict: initscripts-9.49.46-1.0.1.el7.x86_64 conflicts oraclelinux-release < 7:7.5-1.0.3
--> Restarting Dependency Resolution with new changes.
--> Running transaction check
---> Package oraclelinux-release.x86_64 7:7.1-1.0.5.el7 will be updated
---> Package oraclelinux-release.x86_64 7:7.6-1.0.15.el7 will be an update
--> Processing Conflict: systemd-219-62.0.4.el7_6.7.x86_64 conflicts dracut < 033-243
--> Restarting Dependency Resolution with new changes.
--> Running transaction check
---> Package dracut.x86_64 0:033-240.0.1.el7 will be updated
--> Processing Dependency: dracut = 033-240.0.1.el7 for package: dracut-config-rescue-033-240.0.1.el7.x86_64
--> Processing Dependency: dracut = 033-240.0.1.el7 for package: dracut-network-033-240.0.1.el7.x86_64
---> Package dracut.x86_64 0:033-554.0.3.el7 will be an update
--> Running transaction check
---> Package dracut-config-rescue.x86_64 0:033-240.0.1.el7 will be updated
---> Package dracut-config-rescue.x86_64 0:033-554.0.3.el7 will be an update
---> Package dracut-network.x86_64 0:033-240.0.1.el7 will be updated
---> Package dracut-network.x86_64 0:033-554.0.3.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================================================================================
 Package                                                            Arch                                        Version                                                               Repository                                       Size
============================================================================================================================================================================================================================================
Installing:
 docker-engine                                                      x86_64                                      18.09.1.ol-1.0.5.el7                                                  ol7_addons                                       19 M
Updating:
 dracut                                                             x86_64                                      033-554.0.3.el7                                                       ol7_latest                                      328 k
 initscripts                                                        x86_64                                      9.49.46-1.0.1.el7                                                     ol7_latest                                      439 k
 oraclelinux-release                                                x86_64                                      7:7.6-1.0.15.el7                                                      ol7_latest                                       54 k
 redhat-release-server                                              x86_64                                      1:7.6-4.0.1.el7                                                       ol7_latest                                      9.8 k
Installing for dependencies:
 container-selinux                                                  noarch                                      2:2.77-5.el7                                                          ol7_addons                                       37 k
 containerd                                                         x86_64                                      1.2.0-1.0.5.el7                                                       ol7_addons                                       21 M
 criu                                                               x86_64                                      3.9-5.el7                                                             ol7_latest                                      432 k
 docker-cli                                                         x86_64                                      18.09.1.ol-1.0.5.el7                                                  ol7_addons                                       14 M
 libnet                                                             x86_64                                      1.1.6-7.el7                                                           ol7_latest                                       57 k
 lz4                                                                x86_64                                      1.7.5-2.0.1.el7                                                       ol7_latest                                       98 k
 protobuf-c                                                         x86_64                                      1.0.2-3.el7                                                           ol7_latest                                       27 k
 runc                                                               x86_64                                      1.0.0-19.rc5.git4bb1fe4.0.3.el7                                       ol7_addons                                      1.9 M
Updating for dependencies:
 cryptsetup                                                         x86_64                                      2.0.3-3.el7                                                           ol7_latest                                      153 k
 cryptsetup-libs                                                    x86_64                                      2.0.3-3.el7                                                           ol7_latest                                      337 k
 cryptsetup-python                                                  x86_64                                      2.0.3-3.el7                                                           ol7_latest                                       35 k
 device-mapper                                                      x86_64                                      7:1.02.149-10.0.3.el7_6.8                                             ol7_latest                                      293 k
 device-mapper-event                                                x86_64                                      7:1.02.149-10.0.3.el7_6.8                                             ol7_latest                                      188 k
 device-mapper-event-libs                                           x86_64                                      7:1.02.149-10.0.3.el7_6.8                                             ol7_latest                                      188 k
 device-mapper-libs                                                 x86_64                                      7:1.02.149-10.0.3.el7_6.8                                             ol7_latest                                      320 k
 device-mapper-persistent-data                                      x86_64                                      0.7.3-3.el7                                                           ol7_latest                                      404 k
 dracut-config-rescue                                               x86_64                                      033-554.0.3.el7                                                       ol7_latest                                       60 k
 dracut-network                                                     x86_64                                      033-554.0.3.el7                                                       ol7_latest                                      102 k
 glib2                                                              x86_64                                      2.56.1-4.el7_6                                                        ol7_latest                                      2.5 M
 kmod                                                               x86_64                                      20-23.0.1.el7                                                         ol7_latest                                      121 k
 libgudev1                                                          x86_64                                      219-62.0.4.el7_6.7                                                    ol7_latest                                       96 k
 libnl3                                                             x86_64                                      3.2.28-4.el7                                                          ol7_latest                                      277 k
 libnl3-cli                                                         x86_64                                      3.2.28-4.el7                                                          ol7_latest                                      159 k
 libseccomp                                                         x86_64                                      2.3.1-3.el7                                                           ol7_latest                                       55 k
 libselinux                                                         x86_64                                      2.5-14.1.el7                                                          ol7_latest                                      162 k
 libselinux-python                                                  x86_64                                      2.5-14.1.el7                                                          ol7_latest                                      235 k
 libselinux-utils                                                   x86_64                                      2.5-14.1.el7                                                          ol7_latest                                      151 k
 libsemanage                                                        x86_64                                      2.5-14.el7                                                            ol7_latest                                      150 k
 libsemanage-python                                                 x86_64                                      2.5-14.el7                                                            ol7_latest                                      112 k
 libsepol                                                           x86_64                                      2.5-10.el7                                                            ol7_latest                                      297 k
 lvm2                                                               x86_64                                      7:2.02.180-10.0.3.el7_6.8                                             ol7_latest                                      1.3 M
 lvm2-libs                                                          x86_64                                      7:2.02.180-10.0.3.el7_6.8                                             ol7_latest                                      1.1 M
 lvm2-python-libs                                                   x86_64                                      7:2.02.180-10.0.3.el7_6.8                                             ol7_latest                                      186 k
 policycoreutils                                                    x86_64                                      2.5-29.0.1.el7_6.1                                                    ol7_latest                                      916 k
 policycoreutils-python                                             x86_64                                      2.5-29.0.1.el7_6.1                                                    ol7_latest                                      455 k
 selinux-policy                                                     noarch                                      3.13.1-229.0.3.el7_6.12                                               ol7_latest                                      484 k
 selinux-policy-targeted                                            noarch                                      3.13.1-229.0.3.el7_6.12                                               ol7_latest                                      6.9 M
 setools-libs                                                       x86_64                                      3.3.8-4.el7                                                           ol7_latest                                      620 k
 systemd                                                            x86_64                                      219-62.0.4.el7_6.7                                                    ol7_latest                                      5.1 M
 systemd-libs                                                       x86_64                                      219-62.0.4.el7_6.7                                                    ol7_latest                                      407 k
 systemd-python                                                     x86_64                                      219-62.0.4.el7_6.7                                                    ol7_latest                                      133 k
 systemd-sysv                                                       x86_64                                      219-62.0.4.el7_6.7                                                    ol7_latest                                       84 k

Transaction Summary
============================================================================================================================================================================================================================================
Install  1 Package  (+ 8 Dependent packages)
Upgrade  4 Packages (+34 Dependent packages)

Total download size: 81 M
Is this ok [y/d/N]: y
Downloading packages:
No Presto metadata available for ol7_latest
warning: /var/cache/yum/x86_64/7Server/ol7_latest/packages/cryptsetup-2.0.3-3.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY                                                      ] 373 kB/s | 1.6 MB  00:03:39 ETA
Public key for cryptsetup-2.0.3-3.el7.x86_64.rpm is not installed
(1/47): cryptsetup-2.0.3-3.el7.x86_64.rpm                                                                                                                                                                            | 153 kB  00:00:04
(2/47): criu-3.9-5.el7.x86_64.rpm                                                                                                                                                                                    | 432 kB  00:00:04
Public key for container-selinux-2.77-5.el7.noarch.rpm is not installed
(3/47): container-selinux-2.77-5.el7.noarch.rpm                                                                                                                                                                      |  37 kB  00:00:04
(4/47): cryptsetup-python-2.0.3-3.el7.x86_64.rpm                                                                                                                                                                     |  35 kB  00:00:00
(5/47): cryptsetup-libs-2.0.3-3.el7.x86_64.rpm                                                                                                                                                                       | 337 kB  00:00:01
(6/47): device-mapper-event-1.02.149-10.0.3.el7_6.8.x86_64.rpm                                                                                                                                                       | 188 kB  00:00:00
(7/47): device-mapper-1.02.149-10.0.3.el7_6.8.x86_64.rpm                                                                                                                                                             | 293 kB  00:00:01
(8/47): device-mapper-event-libs-1.02.149-10.0.3.el7_6.8.x86_64.rpm                                                                                                                                                  | 188 kB  00:00:00
(9/47): device-mapper-persistent-data-0.7.3-3.el7.x86_64.rpm                                                                                                                                                         | 404 kB  00:00:00
(10/47): device-mapper-libs-1.02.149-10.0.3.el7_6.8.x86_64.rpm                                                                                                                                                       | 320 kB  00:00:02
(11/47): containerd-1.2.0-1.0.5.el7.x86_64.rpm                                                                                                                                                                       |  21 MB  00:00:13
(12/47): dracut-config-rescue-033-554.0.3.el7.x86_64.rpm                                                                                                                                                             |  60 kB  00:00:01
(13/47): dracut-network-033-554.0.3.el7.x86_64.rpm                                                                                                                                                                   | 102 kB  00:00:00
(14/47): dracut-033-554.0.3.el7.x86_64.rpm                                                                                                                                                                           | 328 kB  00:00:02
(15/47): initscripts-9.49.46-1.0.1.el7.x86_64.rpm                                                                                                                                                                    | 439 kB  00:00:00
(16/47): kmod-20-23.0.1.el7.x86_64.rpm                                                                                                                                                                               | 121 kB  00:00:00
(17/47): libgudev1-219-62.0.4.el7_6.7.x86_64.rpm                                                                                                                                                                     |  96 kB  00:00:00
(18/47): glib2-2.56.1-4.el7_6.x86_64.rpm                                                                                                                                                                             | 2.5 MB  00:00:02
(19/47): libnl3-3.2.28-4.el7.x86_64.rpm                                                                                                                                                                              | 277 kB  00:00:00
(20/47): docker-cli-18.09.1.ol-1.0.5.el7.x86_64.rpm                                                                                                                                                                  |  14 MB  00:00:12
(21/47): libnet-1.1.6-7.el7.x86_64.rpm                                                                                                                                                                               |  57 kB  00:00:01
(22/47): libseccomp-2.3.1-3.el7.x86_64.rpm                                                                                                                                                                           |  55 kB  00:00:00
(23/47): libselinux-2.5-14.1.el7.x86_64.rpm                                                                                                                                                                          | 162 kB  00:00:00
(24/47): libnl3-cli-3.2.28-4.el7.x86_64.rpm                                                                                                                                                                          | 159 kB  00:00:00
(25/47): libselinux-python-2.5-14.1.el7.x86_64.rpm                                                                                                                                                                   | 235 kB  00:00:00
(26/47): libselinux-utils-2.5-14.1.el7.x86_64.rpm                                                                                                                                                                    | 151 kB  00:00:00
(27/47): libsemanage-python-2.5-14.el7.x86_64.rpm                                                                                                                                                                    | 112 kB  00:00:00
(28/47): libsemanage-2.5-14.el7.x86_64.rpm                                                                                                                                                                           | 150 kB  00:00:00
(29/47): libsepol-2.5-10.el7.x86_64.rpm                                                                                                                                                                              | 297 kB  00:00:00
(30/47): lvm2-2.02.180-10.0.3.el7_6.8.x86_64.rpm                                                                                                                                                                     | 1.3 MB  00:00:00
(31/47): lvm2-python-libs-2.02.180-10.0.3.el7_6.8.x86_64.rpm                                                                                                                                                         | 186 kB  00:00:00
(32/47): lvm2-libs-2.02.180-10.0.3.el7_6.8.x86_64.rpm                                                                                                                                                                | 1.1 MB  00:00:01
(33/47): lz4-1.7.5-2.0.1.el7.x86_64.rpm                                                                                                                                                                              |  98 kB  00:00:00
(34/47): oraclelinux-release-7.6-1.0.15.el7.x86_64.rpm                                                                                                                                                               |  54 kB  00:00:00
(35/47): policycoreutils-2.5-29.0.1.el7_6.1.x86_64.rpm                                                                                                                                                               | 916 kB  00:00:00
(36/47): policycoreutils-python-2.5-29.0.1.el7_6.1.x86_64.rpm                                                                                                                                                        | 455 kB  00:00:00
(37/47): redhat-release-server-7.6-4.0.1.el7.x86_64.rpm                                                                                                                                                              | 9.8 kB  00:00:00
(38/47): docker-engine-18.09.1.ol-1.0.5.el7.x86_64.rpm                                                                                                                                                               |  19 MB  00:00:10
(39/47): selinux-policy-3.13.1-229.0.3.el7_6.12.noarch.rpm                                                                                                                                                           | 484 kB  00:00:00
(40/47): protobuf-c-1.0.2-3.el7.x86_64.rpm                                                                                                                                                                           |  27 kB  00:00:02
(41/47): setools-libs-3.3.8-4.el7.x86_64.rpm                                                                                                                                                                         | 620 kB  00:00:00
(42/47): runc-1.0.0-19.rc5.git4bb1fe4.0.3.el7.x86_64.rpm                                                                                                                                                             | 1.9 MB  00:00:04
(43/47): selinux-policy-targeted-3.13.1-229.0.3.el7_6.12.noarch.rpm                                                                                                                                                  | 6.9 MB  00:00:03
(44/47): systemd-libs-219-62.0.4.el7_6.7.x86_64.rpm                                                                                                                                                                  | 407 kB  00:00:00
(45/47): systemd-python-219-62.0.4.el7_6.7.x86_64.rpm                                                                                                                                                                | 133 kB  00:00:00
(46/47): systemd-sysv-219-62.0.4.el7_6.7.x86_64.rpm                                                                                                                                                                  |  84 kB  00:00:00
(47/47): systemd-219-62.0.4.el7_6.7.x86_64.rpm                                                                                                                                                                       | 5.1 MB  00:00:04
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                       2.8 MB/s |  81 MB  00:00:29
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
Importing GPG key 0xEC551F03:
 Userid     : "Oracle OSS group (Open Source Software group) "
 Fingerprint: 4214 4123 fecf c55b 9086 313d 72f9 7b74 ec55 1f03
 Package    : 7:oraclelinux-release-7.1-1.0.5.el7.x86_64 (@anaconda/7.1)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : libsepol-2.5-10.el7.x86_64                                                                                                                                                                                              1/85
  Updating   : libselinux-2.5-14.1.el7.x86_64                                                                                                                                                                                          2/85
  Updating   : libsemanage-2.5-14.el7.x86_64                                                                                                                                                                                           3/85
  Installing : lz4-1.7.5-2.0.1.el7.x86_64                                                                                                                                                                                              4/85
  Updating   : systemd-libs-219-62.0.4.el7_6.7.x86_64                                                                                                                                                                                  5/85
  Updating   : 1:redhat-release-server-7.6-4.0.1.el7.x86_64                                                                                                                                                                            6/85
  Updating   : cryptsetup-libs-2.0.3-3.el7.x86_64                                                                                                                                                                                      7/85
  Updating   : 7:device-mapper-libs-1.02.149-10.0.3.el7_6.8.x86_64                                                                                                                                                                     8/85
  Updating   : 7:device-mapper-1.02.149-10.0.3.el7_6.8.x86_64                                                                                                                                                                          9/85
  Updating   : systemd-219-62.0.4.el7_6.7.x86_64                                                                                                                                                                                      10/85
  Updating   : dracut-033-554.0.3.el7.x86_64                                                                                                                                                                                          11/85
  Updating   : kmod-20-23.0.1.el7.x86_64                                                                                                                                                                                              12/85
  Updating   : 7:device-mapper-event-libs-1.02.149-10.0.3.el7_6.8.x86_64                                                                                                                                                              13/85
  Updating   : libseccomp-2.3.1-3.el7.x86_64                                                                                                                                                                                          14/85
  Updating   : glib2-2.56.1-4.el7_6.x86_64                                                                                                                                                                                            15/85
  Updating   : libselinux-utils-2.5-14.1.el7.x86_64                                                                                                                                                                                   16/85
  Updating   : policycoreutils-2.5-29.0.1.el7_6.1.x86_64                                                                                                                                                                              17/85
  Updating   : selinux-policy-3.13.1-229.0.3.el7_6.12.noarch                                                                                                                                                                          18/85
  Updating   : libnl3-3.2.28-4.el7.x86_64                                                                                                                                                                                             19/85
  Updating   : selinux-policy-targeted-3.13.1-229.0.3.el7_6.12.noarch                                                                                                                                                                 20/85
  Updating   : 7:device-mapper-event-1.02.149-10.0.3.el7_6.8.x86_64                                                                                                                                                                   21/85
  Updating   : 7:lvm2-libs-2.02.180-10.0.3.el7_6.8.x86_64                                                                                                                                                                             22/85
  Updating   : 7:oraclelinux-release-7.6-1.0.15.el7.x86_64                                                                                                                                                                            23/85
  Updating   : libsemanage-python-2.5-14.el7.x86_64                                                                                                                                                                                   24/85
  Updating   : libselinux-python-2.5-14.1.el7.x86_64                                                                                                                                                                                  25/85
  Updating   : setools-libs-3.3.8-4.el7.x86_64                                                                                                                                                                                        26/85
  Updating   : policycoreutils-python-2.5-29.0.1.el7_6.1.x86_64                                                                                                                                                                       27/85
  Installing : 2:container-selinux-2.77-5.el7.noarch                                                                                                                                                                                  28/85
  Installing : containerd-1.2.0-1.0.5.el7.x86_64                                                                                                                                                                                      29/85
  Installing : docker-cli-18.09.1.ol-1.0.5.el7.x86_64                                                                                                                                                                                 30/85
  Installing : libnet-1.1.6-7.el7.x86_64                                                                                                                                                                                              31/85
  Installing : protobuf-c-1.0.2-3.el7.x86_64                                                                                                                                                                                          32/85
  Installing : criu-3.9-5.el7.x86_64                                                                                                                                                                                                  33/85
  Installing : runc-1.0.0-19.rc5.git4bb1fe4.0.3.el7.x86_64                                                                                                                                                                            34/85
  Updating   : device-mapper-persistent-data-0.7.3-3.el7.x86_64                                                                                                                                                                       35/85
  Updating   : 7:lvm2-2.02.180-10.0.3.el7_6.8.x86_64                                                                                                                                                                                  36/85
Created symlink from /etc/systemd/system/sysinit.target.wants/lvm2-lvmpolld.socket to /usr/lib/systemd/system/lvm2-lvmpolld.socket.
  Installing : docker-engine-18.09.1.ol-1.0.5.el7.x86_64                                                                                                                                                                              37/85
  Updating   : initscripts-9.49.46-1.0.1.el7.x86_64                                                                                                                                                                                   38/85
  Updating   : 7:lvm2-python-libs-2.02.180-10.0.3.el7_6.8.x86_64                                                                                                                                                                      39/85
  Updating   : libnl3-cli-3.2.28-4.el7.x86_64                                                                                                                                                                                         40/85
  Updating   : libgudev1-219-62.0.4.el7_6.7.x86_64                                                                                                                                                                                    41/85
  Updating   : dracut-network-033-554.0.3.el7.x86_64                                                                                                                                                                                  42/85
  Updating   : dracut-config-rescue-033-554.0.3.el7.x86_64                                                                                                                                                                            43/85
  Updating   : systemd-sysv-219-62.0.4.el7_6.7.x86_64                                                                                                                                                                                 44/85
  Updating   : systemd-python-219-62.0.4.el7_6.7.x86_64                                                                                                                                                                               45/85
  Updating   : cryptsetup-python-2.0.3-3.el7.x86_64                                                                                                                                                                                   46/85
  Updating   : cryptsetup-2.0.3-3.el7.x86_64                                                                                                                                                                                          47/85
  Cleanup    : 7:lvm2-2.02.115-3.el7.x86_64                                                                                                                                                                                           48/85
  Cleanup    : policycoreutils-python-2.2.5-15.0.1.el7.x86_64                                                                                                                                                                         49/85

UPGRADE: Automatically re-enabling default systemd units:
        brandbot.path
        rhel-autorelabel.service
        rhel-autorelabel-mark.service
        rhel-configure.service
        rhel-dmesg.service
        rhel-domainname.service
        rhel-import-state.service
        rhel-loadmodules.service
        rhel-readonly.service

  Cleanup    : initscripts-9.49.24-1.0.1.el7.x86_64                                                                                                                                                                                   50/85
  Cleanup    : libgudev1-208-20.0.1.el7.x86_64                                                                                                                                                                                        51/85
  Cleanup    : selinux-policy-targeted-3.13.1-23.0.1.el7.noarch                                                                                                                                                                       52/85
  Cleanup    : selinux-policy-3.13.1-23.0.1.el7.noarch                                                                                                                                                                                53/85
  Cleanup    : 7:oraclelinux-release-7.1-1.0.5.el7.x86_64                                                                                                                                                                             54/85
  Cleanup    : dracut-config-rescue-033-240.0.1.el7.x86_64                                                                                                                                                                            55/85
  Cleanup    : systemd-sysv-208-20.0.1.el7.x86_64                                                                                                                                                                                     56/85
  Cleanup    : dracut-network-033-240.0.1.el7.x86_64                                                                                                                                                                                  57/85
  Cleanup    : policycoreutils-2.2.5-15.0.1.el7.x86_64                                                                                                                                                                                58/85
  Cleanup    : systemd-python-208-20.0.1.el7.x86_64                                                                                                                                                                                   59/85
  Cleanup    : dracut-033-240.0.1.el7.x86_64                                                                                                                                                                                          60/85
  Cleanup    : setools-libs-3.3.7-46.el7.x86_64                                                                                                                                                                                       61/85
  Cleanup    : libselinux-utils-2.2.2-6.el7.x86_64                                                                                                                                                                                    62/85
  Cleanup    : glib2-2.40.0-4.el7.x86_64                                                                                                                                                                                              63/85
  Cleanup    : libselinux-python-2.2.2-6.el7.x86_64                                                                                                                                                                                   64/85
  Cleanup    : libsemanage-python-2.1.10-16.el7.x86_64                                                                                                                                                                                65/85
  Cleanup    : libsemanage-2.1.10-16.el7.x86_64                                                                                                                                                                                       66/85
  Cleanup    : cryptsetup-1.6.6-3.el7.x86_64                                                                                                                                                                                          67/85
  Cleanup    : libnl3-cli-3.2.21-8.0.1.el7.x86_64                                                                                                                                                                                     68/85
  Cleanup    : cryptsetup-python-1.6.6-3.el7.x86_64                                                                                                                                                                                   69/85
  Cleanup    : 7:lvm2-python-libs-2.02.115-3.el7.x86_64                                                                                                                                                                               70/85
  Cleanup    : 7:lvm2-libs-2.02.115-3.el7.x86_64                                                                                                                                                                                      71/85
  Cleanup    : 7:device-mapper-event-1.02.93-3.el7.x86_64                                                                                                                                                                             72/85
  Cleanup    : 7:device-mapper-event-libs-1.02.93-3.el7.x86_64                                                                                                                                                                        73/85
  Cleanup    : cryptsetup-libs-1.6.6-3.el7.x86_64                                                                                                                                                                                     74/85
  Cleanup    : systemd-208-20.0.1.el7.x86_64                                                                                                                                                                                          75/85
  Cleanup    : 7:device-mapper-1.02.93-3.el7.x86_64                                                                                                                                                                                   76/85
  Cleanup    : 7:device-mapper-libs-1.02.93-3.el7.x86_64                                                                                                                                                                              77/85
  Cleanup    : 1:redhat-release-server-7.1-1.0.2.el7.x86_64                                                                                                                                                                           78/85
  Cleanup    : systemd-libs-208-20.0.1.el7.x86_64                                                                                                                                                                                     79/85
  Cleanup    : libselinux-2.2.2-6.el7.x86_64                                                                                                                                                                                          80/85
  Cleanup    : libsepol-2.1.9-3.el7.x86_64                                                                                                                                                                                            81/85
  Cleanup    : kmod-14-10.el7.x86_64                                                                                                                                                                                                  82/85
  Cleanup    : libnl3-3.2.21-8.0.1.el7.x86_64                                                                                                                                                                                         83/85
  Cleanup    : device-mapper-persistent-data-0.4.1-2.el7.x86_64                                                                                                                                                                       84/85
  Cleanup    : libseccomp-2.1.1-2.el7.x86_64                                                                                                                                                                                          85/85
  Verifying  : 7:device-mapper-event-libs-1.02.149-10.0.3.el7_6.8.x86_64                                                                                                                                                               1/85
  Verifying  : glib2-2.56.1-4.el7_6.x86_64                                                                                                                                                                                             2/85
  Verifying  : docker-engine-18.09.1.ol-1.0.5.el7.x86_64                                                                                                                                                                               3/85
  Verifying  : policycoreutils-2.5-29.0.1.el7_6.1.x86_64                                                                                                                                                                               4/85
  Verifying  : libseccomp-2.3.1-3.el7.x86_64                                                                                                                                                                                           5/85
  Verifying  : kmod-20-23.0.1.el7.x86_64                                                                                                                                                                                               6/85
  Verifying  : 7:device-mapper-libs-1.02.149-10.0.3.el7_6.8.x86_64                                                                                                                                                                     7/85
  Verifying  : 7:lvm2-python-libs-2.02.180-10.0.3.el7_6.8.x86_64                                                                                                                                                                       8/85
  Verifying  : libgudev1-219-62.0.4.el7_6.7.x86_64                                                                                                                                                                                     9/85
  Verifying  : cryptsetup-libs-2.0.3-3.el7.x86_64                                                                                                                                                                                     10/85
  Verifying  : selinux-policy-3.13.1-229.0.3.el7_6.12.noarch                                                                                                                                                                          11/85
  Verifying  : 7:lvm2-2.02.180-10.0.3.el7_6.8.x86_64                                                                                                                                                                                  12/85
  Verifying  : 2:container-selinux-2.77-5.el7.noarch                                                                                                                                                                                  13/85
  Verifying  : initscripts-9.49.46-1.0.1.el7.x86_64                                                                                                                                                                                   14/85
  Verifying  : cryptsetup-python-2.0.3-3.el7.x86_64                                                                                                                                                                                   15/85
  Verifying  : libsemanage-python-2.5-14.el7.x86_64                                                                                                                                                                                   16/85
  Verifying  : 1:redhat-release-server-7.6-4.0.1.el7.x86_64                                                                                                                                                                           17/85
  Verifying  : 7:oraclelinux-release-7.6-1.0.15.el7.x86_64                                                                                                                                                                            18/85
  Verifying  : device-mapper-persistent-data-0.7.3-3.el7.x86_64                                                                                                                                                                       19/85
  Verifying  : dracut-network-033-554.0.3.el7.x86_64                                                                                                                                                                                  20/85
  Verifying  : systemd-sysv-219-62.0.4.el7_6.7.x86_64                                                                                                                                                                                 21/85
  Verifying  : containerd-1.2.0-1.0.5.el7.x86_64                                                                                                                                                                                      22/85
  Verifying  : policycoreutils-python-2.5-29.0.1.el7_6.1.x86_64                                                                                                                                                                       23/85
  Verifying  : protobuf-c-1.0.2-3.el7.x86_64                                                                                                                                                                                          24/85
  Verifying  : dracut-config-rescue-033-554.0.3.el7.x86_64                                                                                                                                                                            25/85
  Verifying  : libselinux-2.5-14.1.el7.x86_64                                                                                                                                                                                         26/85
  Verifying  : systemd-219-62.0.4.el7_6.7.x86_64                                                                                                                                                                                      27/85
  Verifying  : criu-3.9-5.el7.x86_64                                                                                                                                                                                                  28/85
  Verifying  : libsemanage-2.5-14.el7.x86_64                                                                                                                                                                                          29/85
  Verifying  : systemd-libs-219-62.0.4.el7_6.7.x86_64                                                                                                                                                                                 30/85
  Verifying  : libnl3-cli-3.2.28-4.el7.x86_64                                                                                                                                                                                         31/85
  Verifying  : systemd-python-219-62.0.4.el7_6.7.x86_64                                                                                                                                                                               32/85
  Verifying  : libselinux-python-2.5-14.1.el7.x86_64                                                                                                                                                                                  33/85
  Verifying  : libnet-1.1.6-7.el7.x86_64                                                                                                                                                                                              34/85
  Verifying  : libsepol-2.5-10.el7.x86_64                                                                                                                                                                                             35/85
  Verifying  : runc-1.0.0-19.rc5.git4bb1fe4.0.3.el7.x86_64                                                                                                                                                                            36/85
  Verifying  : selinux-policy-targeted-3.13.1-229.0.3.el7_6.12.noarch                                                                                                                                                                 37/85
  Verifying  : libselinux-utils-2.5-14.1.el7.x86_64                                                                                                                                                                                   38/85
  Verifying  : dracut-033-554.0.3.el7.x86_64                                                                                                                                                                                          39/85
  Verifying  : 7:device-mapper-event-1.02.149-10.0.3.el7_6.8.x86_64                                                                                                                                                                   40/85
  Verifying  : 7:device-mapper-1.02.149-10.0.3.el7_6.8.x86_64                                                                                                                                                                         41/85
  Verifying  : libnl3-3.2.28-4.el7.x86_64                                                                                                                                                                                             42/85
  Verifying  : docker-cli-18.09.1.ol-1.0.5.el7.x86_64                                                                                                                                                                                 43/85
  Verifying  : 7:lvm2-libs-2.02.180-10.0.3.el7_6.8.x86_64                                                                                                                                                                             44/85
  Verifying  : setools-libs-3.3.8-4.el7.x86_64                                                                                                                                                                                        45/85
  Verifying  : lz4-1.7.5-2.0.1.el7.x86_64                                                                                                                                                                                             46/85
  Verifying  : cryptsetup-2.0.3-3.el7.x86_64                                                                                                                                                                                          47/85
  Verifying  : libselinux-utils-2.2.2-6.el7.x86_64                                                                                                                                                                                    48/85
  Verifying  : libsemanage-python-2.1.10-16.el7.x86_64                                                                                                                                                                                49/85
  Verifying  : dracut-config-rescue-033-240.0.1.el7.x86_64                                                                                                                                                                            50/85
  Verifying  : systemd-libs-208-20.0.1.el7.x86_64                                                                                                                                                                                     51/85
  Verifying  : systemd-sysv-208-20.0.1.el7.x86_64                                                                                                                                                                                     52/85
  Verifying  : policycoreutils-2.2.5-15.0.1.el7.x86_64                                                                                                                                                                                53/85
  Verifying  : policycoreutils-python-2.2.5-15.0.1.el7.x86_64                                                                                                                                                                         54/85
  Verifying  : cryptsetup-python-1.6.6-3.el7.x86_64                                                                                                                                                                                   55/85
  Verifying  : 7:oraclelinux-release-7.1-1.0.5.el7.x86_64                                                                                                                                                                             56/85
  Verifying  : 7:device-mapper-event-libs-1.02.93-3.el7.x86_64                                                                                                                                                                        57/85
  Verifying  : dracut-033-240.0.1.el7.x86_64                                                                                                                                                                                          58/85
  Verifying  : cryptsetup-1.6.6-3.el7.x86_64                                                                                                                                                                                          59/85
  Verifying  : setools-libs-3.3.7-46.el7.x86_64                                                                                                                                                                                       60/85
  Verifying  : initscripts-9.49.24-1.0.1.el7.x86_64                                                                                                                                                                                   61/85
  Verifying  : kmod-14-10.el7.x86_64                                                                                                                                                                                                  62/85
  Verifying  : systemd-python-208-20.0.1.el7.x86_64                                                                                                                                                                                   63/85
  Verifying  : cryptsetup-libs-1.6.6-3.el7.x86_64                                                                                                                                                                                     64/85
  Verifying  : dracut-network-033-240.0.1.el7.x86_64                                                                                                                                                                                  65/85
  Verifying  : glib2-2.40.0-4.el7.x86_64                                                                                                                                                                                              66/85
  Verifying  : libnl3-cli-3.2.21-8.0.1.el7.x86_64                                                                                                                                                                                     67/85
  Verifying  : libnl3-3.2.21-8.0.1.el7.x86_64                                                                                                                                                                                         68/85
  Verifying  : 7:device-mapper-event-1.02.93-3.el7.x86_64                                                                                                                                                                             69/85
  Verifying  : libselinux-python-2.2.2-6.el7.x86_64                                                                                                                                                                                   70/85
  Verifying  : libsemanage-2.1.10-16.el7.x86_64                                                                                                                                                                                       71/85
  Verifying  : libseccomp-2.1.1-2.el7.x86_64                                                                                                                                                                                          72/85
  Verifying  : libsepol-2.1.9-3.el7.x86_64                                                                                                                                                                                            73/85
  Verifying  : 7:lvm2-python-libs-2.02.115-3.el7.x86_64                                                                                                                                                                               74/85
  Verifying  : 7:device-mapper-1.02.93-3.el7.x86_64                                                                                                                                                                                   75/85
  Verifying  : selinux-policy-3.13.1-23.0.1.el7.noarch                                                                                                                                                                                76/85
  Verifying  : libselinux-2.2.2-6.el7.x86_64                                                                                                                                                                                          77/85
  Verifying  : 1:redhat-release-server-7.1-1.0.2.el7.x86_64                                                                                                                                                                           78/85
  Verifying  : libgudev1-208-20.0.1.el7.x86_64                                                                                                                                                                                        79/85
  Verifying  : 7:lvm2-2.02.115-3.el7.x86_64                                                                                                                                                                                           80/85
  Verifying  : 7:lvm2-libs-2.02.115-3.el7.x86_64                                                                                                                                                                                      81/85
  Verifying  : 7:device-mapper-libs-1.02.93-3.el7.x86_64                                                                                                                                                                              82/85
  Verifying  : device-mapper-persistent-data-0.4.1-2.el7.x86_64                                                                                                                                                                       83/85
  Verifying  : selinux-policy-targeted-3.13.1-23.0.1.el7.noarch                                                                                                                                                                       84/85
  Verifying  : systemd-208-20.0.1.el7.x86_64                                                                                                                                                                                          85/85

Installed:
  docker-engine.x86_64 0:18.09.1.ol-1.0.5.el7

Dependency Installed:
  container-selinux.noarch 2:2.77-5.el7     containerd.x86_64 0:1.2.0-1.0.5.el7               criu.x86_64 0:3.9-5.el7     docker-cli.x86_64 0:18.09.1.ol-1.0.5.el7     libnet.x86_64 0:1.1.6-7.el7     lz4.x86_64 0:1.7.5-2.0.1.el7
  protobuf-c.x86_64 0:1.0.2-3.el7           runc.x86_64 0:1.0.0-19.rc5.git4bb1fe4.0.3.el7

Updated:
  dracut.x86_64 0:033-554.0.3.el7                   initscripts.x86_64 0:9.49.46-1.0.1.el7                   oraclelinux-release.x86_64 7:7.6-1.0.15.el7                   redhat-release-server.x86_64 1:7.6-4.0.1.el7

Dependency Updated:
  cryptsetup.x86_64 0:2.0.3-3.el7                              cryptsetup-libs.x86_64 0:2.0.3-3.el7                          cryptsetup-python.x86_64 0:2.0.3-3.el7                  device-mapper.x86_64 7:1.02.149-10.0.3.el7_6.8
  device-mapper-event.x86_64 7:1.02.149-10.0.3.el7_6.8         device-mapper-event-libs.x86_64 7:1.02.149-10.0.3.el7_6.8     device-mapper-libs.x86_64 7:1.02.149-10.0.3.el7_6.8     device-mapper-persistent-data.x86_64 0:0.7.3-3.el7
  dracut-config-rescue.x86_64 0:033-554.0.3.el7                dracut-network.x86_64 0:033-554.0.3.el7                       glib2.x86_64 0:2.56.1-4.el7_6                           kmod.x86_64 0:20-23.0.1.el7
  libgudev1.x86_64 0:219-62.0.4.el7_6.7                        libnl3.x86_64 0:3.2.28-4.el7                                  libnl3-cli.x86_64 0:3.2.28-4.el7                        libseccomp.x86_64 0:2.3.1-3.el7
  libselinux.x86_64 0:2.5-14.1.el7                             libselinux-python.x86_64 0:2.5-14.1.el7                       libselinux-utils.x86_64 0:2.5-14.1.el7                  libsemanage.x86_64 0:2.5-14.el7
  libsemanage-python.x86_64 0:2.5-14.el7                       libsepol.x86_64 0:2.5-10.el7                                  lvm2.x86_64 7:2.02.180-10.0.3.el7_6.8                   lvm2-libs.x86_64 7:2.02.180-10.0.3.el7_6.8
  lvm2-python-libs.x86_64 7:2.02.180-10.0.3.el7_6.8            policycoreutils.x86_64 0:2.5-29.0.1.el7_6.1                   policycoreutils-python.x86_64 0:2.5-29.0.1.el7_6.1      selinux-policy.noarch 0:3.13.1-229.0.3.el7_6.12
  selinux-policy-targeted.noarch 0:3.13.1-229.0.3.el7_6.12     setools-libs.x86_64 0:3.3.8-4.el7                             systemd.x86_64 0:219-62.0.4.el7_6.7                     systemd-libs.x86_64 0:219-62.0.4.el7_6.7
  systemd-python.x86_64 0:219-62.0.4.el7_6.7                   systemd-sysv.x86_64 0:219-62.0.4.el7_6.7

Complete!

一旦安装完成,就可以启动docker服务

[root@localhost soft]# service docker start
Redirecting to /bin/systemctl start docker.service

[root@localhost soft]# systemctl enable docker

[root@localhost soft]# service docker status
Redirecting to /bin/systemctl status docker.service
鈼[0m docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/docker.service.d
           鈹斺攢docker-sysconfig.conf
   Active: active (running) since Mon 2019-06-10 23:15:05 CST; 6h left
     Docs: https://docs.docker.com
 Main PID: 1452 (dockerd)
   Memory: 132.2M
   CGroup: /system.slice/docker.service
           鈹溾攢1452 /usr/bin/dockerd --selinux-enabled --storage-driver devicemapper --storage-opt dm.basesize=25G
           鈹斺攢2436 containerd --config /var/run/docker/containerd/containerd.toml --log-level info

Jun 10 23:15:03 localhost.localdomain dockerd[1452]: time="2019-06-10T23:15:03.843382248+08:00" level=info msg="Successfully created filesystem xfs on device docker-253:0-1047242-base" storage-driver=devicemapper
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time="2019-06-10T23:15:04.076789613+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time="2019-06-10T23:15:04.078665673+08:00" level=warning msg="mountpoint for pids not found"
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time="2019-06-10T23:15:04.079906275+08:00" level=info msg="Loading containers: start."
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time="2019-06-10T23:15:04.554467519+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to...rred IP address"
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time="2019-06-10T23:15:04.859786800+08:00" level=info msg="Loading containers: done."
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time="2019-06-10T23:15:04.959099871+08:00" level=info msg="Docker daemon" commit=c3ab8a8 graphdriver(s)=devicemapper version=18.09.1-ol
Jun 10 23:15:04 localhost.localdomain dockerd[1452]: time="2019-06-10T23:15:04.959938124+08:00" level=info msg="Daemon has completed initialization"
Jun 10 23:15:05 localhost.localdomain dockerd[1452]: time="2019-06-10T23:15:05.053166130+08:00" level=info msg="API listen on /var/run/docker.sock"
Jun 10 23:15:05 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

然后可以下载镜像文件

[root@localhost soft]# docker pull mysql/mysql-server:5.7
Trying to pull repository docker.io/mysql/mysql-server ...
5.7: Pulling from docker.io/mysql/mysql-server
35defbf6c365: Pull complete
0fa46ab0f51d: Pull complete
f70f5000008c: Pull complete
892ac46af8c0: Pull complete
Digest: sha256:ddb046076781a15200d36cb01f8f512431c3481bedebd5e92646d8c617ae212c
Status: Downloaded newer image for mysql/mysql-server:5.7
[root@localhost soft]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
mysql/mysql-server   5.7                 857eadf53a54        6 weeks ago         258MB



使用Docker来部署MySQL
下载MySQL Server Docker镜像文件
严格来说,不需要在单独的步骤中下载服务器镜像,但是,在创建Docker容器之前执行此步骤可以确保本地映像是最新的。为了下载MySQL社区版镜像,执行以下命令:

docker pull mysql/mysql-server:tag

tag是你想要pull镜像版本的标识(例如,5.5,5.6,5.7,8.0或最新版本)。如果:tag被忽略,latest标记会被使用并且最新通用可用版本的MySQL社区版本镜像将会被下载。网址
https://hub.docker.com/r/mysql/mysql-server/tags/列出了所有可用版本信息.

[root@localhost soft]# docker pull mysql/mysql-server:5.7
Trying to pull repository docker.io/mysql/mysql-server ...
5.7: Pulling from docker.io/mysql/mysql-server
35defbf6c365: Pull complete
0fa46ab0f51d: Pull complete
f70f5000008c: Pull complete
892ac46af8c0: Pull complete
Digest: sha256:ddb046076781a15200d36cb01f8f512431c3481bedebd5e92646d8c617ae212c
Status: Downloaded newer image for mysql/mysql-server:5.7

如果要显示Docker镜像可以执行以下命令:

[root@localhost soft]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
mysql/mysql-server   5.7                 857eadf53a54        6 weeks ago         258MB

为了下载MySQL企业版本镜像,执行以下命令:

docker pull store/oracle/mysql-enterprise-server:5.7

5.7是我们想要pull的镜像版本。MySQL企业版本容器只提供了最新的通用可用版本5.7

启动MySQL Server实例
为MySQL社区版本服务器启动一个新的Docker容器执行以下命令:

[root@localhost ~]# docker run --name=mysql1 -d mysql/mysql-server:5.7
5128014e440f10c557b52e6203445a01c97728e432d0f231c6027bf4b15520bd

为MySQL企业版本服务器启动一个新的Docker容器执行以下命令:

docker run --name=mysql1 -d store/oracle/mysql-enterprise-server:tag

–name选项,用来为你的服务容器指定名字(例如mysql1),它是可选项。如果没有提供容器名,会生成一个随机容器名。如果之前的docker pull或docker run命令所指定名称或标记的Docker镜像没有下载,那么镜像现在就会被下载。在下载完成后,开始初始化容器,并且当你执行docker ps命令时容器会出现在正在运行的容器列表中。例如:

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                             PORTS                 NAMES
5128014e440f        mysql/mysql-server:5.7   "/entrypoint.sh mysq鈥   42 seconds ago      Up 39 seconds (health: starting)   3306/tcp, 33060/tcp   mysql1

IMAGE列的输出对于MySQL社区版本读取的值为mysql/mysql-server,对于MySQL企业版本读取的值为store/oracle/mysql-enterprise-server。
容器初始化可能会花费一些时间。当服务准备好使用时,docker ps命令输出的STATUS列将从(health: starting)改变为(healthy)。

在上面的docker run命令中-d选项使容器在后台运行。使用下面的命令可以监控容器的输出信息:

[root@localhost ~]# docker logs mysql1
[Entrypoint] MySQL Docker Image 5.7.26-1.1.11
[Entrypoint] No password option specified for new database.
[Entrypoint]   A random onetime password will be generated.
[Entrypoint] Initializing database
[Entrypoint] Database initialized
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
[Entrypoint] GENERATED ROOT PASSWORD: 3z[omLAk4Jaz@sYxDiLYnyPySYfR

[Entrypoint] ignoring /docker-entrypoint-initdb.d/*

[Entrypoint] Server shut down
[Entrypoint] Setting root user as expired. Password will need to be changed before database can be used.

[Entrypoint] MySQL init process done. Ready for start up.

[Entrypoint] Starting MySQL 5.7.26-1.1.11

一旦初始化完成,命令的输出将会包含为root用户生成随机密码的信息,也可以执行下面的命令来检查密码:

[root@localhost ~]# docker logs mysql1 2>&1 | grep GENERATED
[Entrypoint] GENERATED ROOT PASSWORD: 3z[omLAk4Jaz@sYxDiLYnyPySYfR

在容器中连接MySQL服务器
一旦服务可以使用后,可以在刚刚启动的MySQL服务容器中运行mysql客户端程序并连接到MySQL服务。在刚刚启动的Docker容器中使用docker exec -it命令来启动mysql客户端程序,例如:

[root@localhost ~]# docker exec -it mysql1 mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 107
Server version: 5.7.26

Copyright (c) 2000, 2019, 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>

在执行上面的命令时需要输入root用户生成的密码。因为mysql_onetime_password选项缺省情况为true(对于MySQL社区版本来说),在mysql客户端连接到MySQL服务后,必须要通过执行以下命令来修改root用户的密码:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

一旦修改密码后,MySQL服务就可以使用。

容器Shell访问
为了使用shell访问你的MySQL服务器容器,在容器中使用docker exec -it命令来启动bash shell:

[root@localhost ~]# docker exec -it mysql1 bash
bash-4.2#

在启动bash shell后,在容器中可以运行linux命令,例如,为了查看容器中的MySQL数据库的数据目录中的内容,执行以下命令:

bash-4.2# ls /var/lib/mysql
auto.cnf    ca.pem           client-key.pem  ib_logfile0  ibdata1  mysql       mysql.sock.lock     private_key.pem  server-cert.pem  sys
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile1  ibtmp1   mysql.sock  performance_schema  public_key.pem   server-key.pem

停止与删除一个MySQL容器
为了停止已经创建的MySQL服务容器执行下面的命令:

[root@localhost ~]# docker stop mysql1
mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                     PORTS               NAMES
5128014e440f        mysql/mysql-server:5.7   "/entrypoint.sh mysql"   21 hours ago        Exited (0) 8 seconds ago                       mysql1

docker stop将发送一个SIGTERM信号给mysqld进程,因此服务将被优雅的关闭。
还可以观察到当一个容器的主进程(一个MySQL服务容器的主进程为mysqld)被关闭,Docker容器也会自动关闭。

启动MySQL服务容器:

[root@localhost ~]# docker start mysql1
mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                            PORTS                 NAMES
5128014e440f        mysql/mysql-server:5.7   "/entrypoint.sh mysql"   22 hours ago        Up 5 seconds (health: starting)   3306/tcp, 33060/tcp   mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                             PORTS                 NAMES
5128014e440f        mysql/mysql-server:5.7   "/entrypoint.sh mysql"   22 hours ago        Up 15 seconds (health: starting)   3306/tcp, 33060/tcp   mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                             PORTS                 NAMES
5128014e440f        mysql/mysql-server:5.7   "/entrypoint.sh mysql"   22 hours ago        Up 21 seconds (health: starting)   3306/tcp, 33060/tcp   mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                    PORTS                 NAMES
5128014e440f        mysql/mysql-server:5.7   "/entrypoint.sh mysql"   22 hours ago        Up 32 seconds (healthy)   3306/tcp, 33060/tcp   mysql1

为了使用单个命令停止与再次启动MySQL服务容器:

[root@localhost ~]# docker restart mysql1
mysql1
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                            PORTS                 NAMES
5128014e440f        mysql/mysql-server:5.7   "/entrypoint.sh mysql"   24 hours ago        Up 6 seconds (health: starting)   3306/tcp, 33060/tcp   mysql1

为了删除MySQL容器,首先停止容器,然后再使用docker rm命令:

[root@localhost ~]# docker stop mysql1
mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                      PORTS               NAMES
5128014e440f        mysql/mysql-server:5.7   "/entrypoint.sh mysq1"  24 hours ago        Exited (0) 12 seconds ago                       mysql1
[root@localhost ~]# docker rm mysql1
mysql1
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

为Docker优化MySQL的安装
MySQL的Docker映像针对代码大小进行了优化,这意味着它们只包含与大多数在Docker容器中运行MySQL实例的用户相关的关键组件。一个MySQL Docker安装与常见的非Docker安装在以下方面不同:
包含的二进制文件是有限的:

/usr/bin/my_print_defaults
/usr/bin/mysql
/usr/bin/mysql_config
/usr/bin/mysql_install_db
/usr/bin/mysql_tzinfo_to_sql
/usr/bin/mysql_upgrade
/usr/bin/mysqladmin
/usr/bin/mysqlcheck
/usr/bin/mysqldump
/usr/bin/mysqlpump
/usr/sbin/mysqld

所有的二进制文件被stripped;它们不包含调试信息。

配置MySQL Server
当你启动MySQL Docker容器时,可以通过docker run命令来传递配置选项给服务,例如,对于MySQL社区版本服务器

docker run --name mysql1 -d mysql/mysql-server:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_col

对于MySQL企业版本服务器:

docker run --name mysql1 -d store/oracle/mysql-enterprise-server --character-set-server=utf8mb4 --collation-server=utf8mb4_col

命令以utf8mb4作为默认字符集和utf8mb4col作为您的数据库的默认排序启动MySQL服务器。另一种配置MySQL服务器的方法是准备一个配置文件,并将其挂载到容器内的服务器配置文件的位置。有关详细信息,请参阅持久数据和配置更改。

持久数据和配置更改
Docker容器在原则上是临时的,如果容器被删除或损坏,任何数据或配置都将丢失(参见讨论)。然而,Docker卷提供了一种机制,可以在Docker容器中保存数据。在初始化时,MySQL服务器容器为服务器数据目录创建Docker卷。运行docker检查容器命令的JSON输出有一个挂载键,其值提供数据目录卷的信息:

[root@localhost ~]# docker inspect mysql1
...
"Mounts": [
            {
                "Type": "volume",
                "Name": "a74fb4e3348635ddc0b3eb32e3f82b4feb38eeadd8d5b3ae60b4389ab83a86d8",
                "Source": "/var/lib/docker/volumes/a74fb4e3348635ddc0b3eb32e3f82b4feb38eeadd8d5b3ae60b4389ab83a86d8/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
...

输出显示源文件夹
/var/lib/docker/volumes/a74fb4e3348635ddc0b3eb32e3f82b4feb38eeadd8d5b3ae60b4389ab83a86d8/_data将数据持久化到主机上的数据,已经安装在/var/lib/mysql中,容器内的服务器数据目录。

保存数据的另一种方法是在创建容器时使用 –mount 选项挂载主机目录。同样的技术可以用来持久化服务器的配置。
下面的命令创建一个MySQL服务器容器,并将数据目录和服务器配置文件绑定在一起:

docker run --name=mysql1 \
--mount type=bind,src=/path-on-host-machine/my.cnf,dst=/etc/my.cnf \
--mount type=bind,src=/path-on-host-machine/datadir,dst=/var/lib/mysql \
-d mysql/mysql-server:tag

配置文件path-on-host-machine/my.cnf必须存在,同时包含指定的用户:

[mysqld]
user=mysql

path-on-host-machine.datadir目录必须存在。要发生服务器初始化,目录必须是空的。您还可以安装一个带有数据的目录,并使用它启动服务器;但是,您必须确保启动Docker容器,其配置与创建数据的服务器具有相同的配置,并且启动容器时所需的任何主机文件或目录都被安装。

运行额外的初始化脚本
如果您想在创建后立即在数据库上运行任何.sh或.sql脚本,您可以将它们放入主机目录中,然后mount目录在/docker-entrypoint-initdb.d

docker run --name=mysql1 \
--mount type=bind,src=/path-on-host-machine/scripts/,dst=/docker-entrypoint-initdb.d/ \
-d mysql/mysql-server:tag

对于MySQL企业版本服务器容器:

docker run --name=mysql1 \
--mount type=bind,src=/path-on-host-machine/scripts/,dst=/docker-entrypoint-initdb.d/ \
-d store/oracle/mysql-enterprise-server:tag

从另一个Docker容器中的应用程序连接到MySQL
通过建立一个Docker网络,你可以让多个Docker容器相互通信,这样,在另一个Docker容器中的客户端应用程序就可以在服务器容器中访问MySQL服务器。首先,创建一个Docker网络:

docker network create my-custom-net

然后,当您创建并启动服务器和客户端容器时,使用-network选项将它们放在您创建的网络上。例如:

docker run --name=mysql1 --network=my-custom-net -d mysql/mysql-server
docker run --name=myapp1 --network=my-custom-net -d myapp

当使用MySQL企业版本服务器容器:

docker run --name=mysql1 --network=my-custom-net -d store/oracle/mysql-enterprise-server
docker run --name=myapp1 --network=my-custom-net -d myapp

myapp1容器随后可以与mysql1主机连接到mysql1容器,反之亦然,因为Docker会自动为给定的容器名称设置一个DNS。在下面的例子中,我们从myapp1容器中运行mysql客户端,以便在自己的容器中连接主机mysql1:

docker exec -it myapp1 mysql --host=mysql1 --user=myuser --password