Top

NSD RDBMS2 DAY05

  1. 案例1:安装软件
  2. 案例2:配置服务
  3. 案例3:测试配置
  4. 案例4:MySQL存储引擎的配置
  5. 案例5:事务特性

1 案例1:安装软件

1.1 问题

1.2 方案

准备3台虚拟主机,配置ip地址和主机名。具体如图-1所示:(不需要安装任何MySQL服务软件)

图-1

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:环境准备

配置主机名与ip地址绑定

配置服务器192.168.4.71

  1. ]# vim /etc/hosts
  2. 192.168.4.71     pxcnode71
  3. 192.168.4.72     pxcnode72
  4. 192.168.4.73     pxcnode73
  5. :wq
  6. ]#hostname pxcnode71

配置服务器192.168.4.72

  1. ]# vim /etc/hosts
  2. 192.168.4.71     pxcnode71
  3. 192.168.4.72     pxcnode72
  4. 192.168.4.73     pxcnode73
  5. :wq
  6. ]#hostname pxcnode72

配置服务器192.168.4.73

  1. ]# vim /etc/hosts
  2. 192.168.4.71     pxcnode71
  3. 192.168.4.72     pxcnode72
  4. 192.168.4.73     pxcnode73
  5. :wq
  6. ]#hostname pxcnode73

在任意一台服务器上ping 对方的主机名,ping通为配置成功。

  1. [root@host71 ~]# ping -c 2 pxcnode71 //成功
  2. PING pxcnode71 (192.168.4.71) 56(84) bytes of data.
  3. 64 bytes from pxcnode71 (192.168.4.71): icmp_seq=1 ttl=255 time=0.011 ms
  4. 64 bytes from pxcnode71 (192.168.4.71): icmp_seq=2 ttl=255 time=0.020 ms
  5.  
  6. --- pxcnode71 ping statistics ---
  7. 2 packets transmitted, 2 received, 0% packet loss, time 999ms
  8. rtt min/avg/max/mdev = 0.011/0.015/0.020/0.006 ms
  9. [root@host71 ~]#
  10. [root@host71 ~]#
  11. [root@host71 ~]# ping -c 2 pxcnode72 //成功
  12. PING pxcnode72 (192.168.4.72) 56(84) bytes of data.
  13. 64 bytes from pxcnode72 (192.168.4.72): icmp_seq=1 ttl=255 time=0.113 ms
  14. 64 bytes from pxcnode72 (192.168.4.72): icmp_seq=2 ttl=255 time=0.170 ms
  15.  
  16. --- pxcnode72 ping statistics ---
  17. 2 packets transmitted, 2 received, 0% packet loss, time 1000ms
  18. rtt min/avg/max/mdev = 0.113/0.141/0.170/0.030 ms
  19. [root@host71 ~]#
  20. [root@host71 ~]#
  21. [root@host71 ~]# ping -c 2 pxcnode73 //成功
  22. PING pxcnode73 (192.168.4.73) 56(84) bytes of data.
  23. 64 bytes from pxcnode73 (192.168.4.73): icmp_seq=1 ttl=255 time=0.198 ms
  24. 64 bytes from pxcnode73 (192.168.4.73): icmp_seq=2 ttl=255 time=0.155 ms
  25.  
  26. --- pxcnode73 ping statistics ---
  27. 2 packets transmitted, 2 received, 0% packet loss, time 1000ms
  28. rtt min/avg/max/mdev = 0.155/0.176/0.198/0.025 ms
  29. [root@host71 ~]#

步骤二:安装软件包

1)在192.168.4.71 服务器安装软件包

软件包之间有依赖注意软件包安装顺序

  1. ]# rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm //安装依赖
  2. ]# yum -y install percona-xtrabackup-24-2.4.13-1.el7.x86_64.rpm
  3.  
  4. ]# rpm -ivh qpress-1.1-14.11.x86_64.rpm //安装依赖
  5. ]# tar -xvf Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar
  6. ]# yum -y install Percona-XtraDB-Cluster-*.rpm

2)在192.168.4.72 服务器安装软件包

  1. ]# rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm //安装依赖
  2. ]# yum -y install percona-xtrabackup-24-2.4.13-1.el7.x86_64.rpm
  3. ]# rpm -ivh qpress-1.1-14.11.x86_64.rpm //安装依赖
  4. ]# tar -xvf Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar
  5. ]# yum -y install Percona-XtraDB-Cluster-*.rpm

3)在192.168.4.73 服务器安装软件包

  1. ]# rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm //安装依赖
  2. ]# yum -y install percona-xtrabackup-24-2.4.13-1.el7.x86_64.rpm
  3. ]# rpm -ivh qpress-1.1-14.11.x86_64.rpm //安装依赖
  4. ]# tar -xvf Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar
  5. ]# yum -y install Percona-XtraDB-Cluster-*.rpm

2 案例2:配置服务

2.1 问题

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:修改mysqld.cnf文件

  1. [root@pxcnode71 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
  2. [mysqld]
  3. server-id=71                 //server-id 不允许重复
  4. datadir=/var/lib/mysql             //数据库目录
  5. socket=/var/lib/mysql/mysql.sock     //socket文件
  6. log-error=/var/log/mysqld.log        //日志文件
  7. pid-file=/var/run/mysqld/mysqld.pid    //pid文件
  8. log-bin                    //启用binlog日志
  9. log_slave_updates            //启用链式复制
  10. expire_logs_days=7            //日志文件保留天数
  11. :wq

修改服务器192.168.4.72

  1. [root@pxcnode72 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
  2. [mysqld]
  3. server-id=72                 //server-id 不允许重复
  4. datadir=/var/lib/mysql             //数据库目录
  5. socket=/var/lib/mysql/mysql.sock     //socket文件
  6. log-error=/var/log/mysqld.log        //日志文件
  7. pid-file=/var/run/mysqld/mysqld.pid    //pid文件
  8. log-bin                    //启用binlog日志
  9. log_slave_updates            //启用链式复制
  10. expire_logs_days=7            //日志文件保留天数
  11. :wq

修改服务器192.168.4.73

  1. [root@pxcnode73 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
  2. [mysqld]
  3. server-id=73                 //server-id 不允许重复
  4. datadir=/var/lib/mysql             //数据库目录
  5. socket=/var/lib/mysql/mysql.sock     //socket文件
  6. log-error=/var/log/mysqld.log        //日志文件
  7. pid-file=/var/run/mysqld/mysqld.pid    //pid文件
  8. log-bin                    //启用binlog日志
  9. log_slave_updates            //启用链式复制
  10. expire_logs_days=7            //日志文件保留天数
  11. :wq

步骤二:修改mysqld_safe.cnf文件

1)分别修改3台服务器的mysqld_safe.cnf (使用默认配置即可)

  1. [root@pxcnode71 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld_safe.cnf
  2. [mysqld_safe]
  3. pid-file = /var/run/mysqld/mysqld.pid
  4. socket = /var/lib/mysql/mysql.sock
  5. nice = 0
  6. :wq

修改服务器192.168.4.72

  1. [root@pxcnode72 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld_safe.cnf
  2. [mysqld_safe]
  3. pid-file = /var/run/mysqld/mysqld.pid
  4. socket = /var/lib/mysql/mysql.sock
  5. nice = 0
  6. :wq

修改服务器192.168.4.73

  1. [root@pxcnode73 ~]# vim /etc/percona-xtradb-cluster.conf.d/mysqld_safe.cnf
  2. [mysqld_safe]
  3. pid-file = /var/run/mysqld/mysqld.pid
  4. socket = /var/lib/mysql/mysql.sock
  5. nice = 0
  6. :wq

步骤三:修改wsrep.cnf文件

1)分别修改3台服务器的wsrep.cnf

  1. [root@pxcnode71 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
  2. wsrep_cluster_address=gcomm://192.168.4.71,192.168.4.72,192.168.4.73//成员列表
  3. wsrep_node_address=192.168.4.71 //本机ip
  4. wsrep_cluster_name=pxc-cluster //集群名
  5. wsrep_node_name=pxcnode71 //本机主机名
  6. wsrep_sst_auth="sstuser:123qqq...A" //SST数据同步授权用户及密码
  7. :wq

修改服务器192.168.4.72

  1. [root@pxcnode72 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
  2. wsrep_cluster_address=gcomm://192.168.4.71,192.168.4.72,192.168.4.73//成员列表
  3. wsrep_node_address=192.168.4.72 //本机ip
  4. wsrep_cluster_name=pxc-cluster //集群名
  5. wsrep_node_name=pxcnode72 //本机主机名
  6. wsrep_sst_auth="sstuser:123qqq...A" //SST数据同步授权用户及密码
  7. :wq

修改服务器192.168.4.73

  1. [root@pxcnode73 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
  2. wsrep_cluster_address=gcomm://192.168.4.71,192.168.4.72,192.168.4.73//成员列表
  3. wsrep_node_address=192.168.4.73 //本机ip
  4. wsrep_cluster_name=pxc-cluster //集群名
  5. wsrep_node_name=pxcnode73 //本机主机名
  6. wsrep_sst_auth="sstuser:123qqq...A" //SST数据同步授权用户及密码
  7. :wq

步骤四:启动服务

1)启动集群服务

注意:在1台服务器上执即可(192.168.4.71),首次启动服务时间比较长

  1. [root@pxcnode71 ~]# ]# systemctl start mysql@bootstrap.service //启动集群服务
  2. [root@pxcnode71 ~]# grep pass /var/log/mysqld.log     //查看数据库管理员初始登录密码
  3. 2019-06-20T12:29:42.489377Z 1 [Note] A temporary password is generated for root@localhost: W.HiOb8(ok)_
  4.  
  5. [root@pxcnode71 ~]#mysql –uroot –p’ W.HiOb8(ok)_’ //使用初始密码登录
  6. Mysql> alter user root@”localhost” identified by “123456;//修改登录密码
  7. MySQL> exit;
  8. [root@pxcnode71 ~]#mysql –uroot –p123456 //使用修改后的密码登录
  9. Mysql> garnt reload, lock tables,replication client,process on *.* to
  10. sstuser@"localhost” identified by “123qqq…A”; //添加授权用户

2)启动数据库服务

启动主机pxcnode72的数据库服务,会自动同步pxcnode71主机的root初始密码和授权用户sstuser

  1. [root@pxcnode72 ~]# systemctl start mysql //启动数据库服务
  2. [root@pxcnode72 ~]#
  3. [root@pxcnode72 ~]# netstat -utnlp | grep :3306
  4. tcp6 0 0 :::3306 :::* LISTEN 12794/mysqld
  5. [root@pxcnode72 ~]# netstat -utnlp | grep :4567
  6. tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN 12794/mysqld
  7. [root@host72 ~]#

启动主机pxcnode73的数据库服务,会自动同步pxcnode71主机的root初始密码和授权用户sstuser

  1. [root@pxcnode73 ~]# systemctl start mysql //启动数据库服务
  2. [root@pxcnode73 ~]#
  3. [root@pxcnode73 ~]# netstat -utnlp | grep :3306
  4. tcp6 0 0 :::3306 :::* LISTEN 12794/mysqld
  5. [root@pxcnode73 ~]# netstat -utnlp | grep :4567
  6. tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN 12794/mysqld
  7. [root@host73 ~]#

3 案例3:测试配置

3.1 问题

3.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:查看集群信息

1)启动数据库服务

在任意一台数据查看都可以。

  1. [root@pxcnode71 ~]# mysql -uroot -p123456
  2. wsrep_incoming_addresses 192.168.4.71:3306,192.168.4.72:3306,192.168.4.73:3306 //集群成员列表
  3. wsrep_cluster_size 3 //集群服务器台数
  4. wsrep_cluster_status Primary //主机状态
  5. wsrep_connected ON            //连接状态
  6. wsrep_ready ON //服务状态

步骤二:访问集群,存取数据

1)添加访问数据的连接用户 (在任意一台服务器上添加都可以,另外的2台服务器会自动同步授权用户)

  1. [root@pxcnode72 ~]# mysql -uroot -p123456
  2. mysql> grant all on gamedb.* to yaya@"%" identified by "123456"; //添加授权用户
  3. Query OK, 0 rows affected, 1 warning (0.18 sec)
  4.  
  5. [root@pxcnode71 ~]# mysql -uroot -p123456 -e 'show grants for yaya@"%" ' //查看
  6. mysql: [Warning] Using a password on the command line interface can be insecure.
  7. +--------------------------------------------------+
  8. | Grants for yaya@% |
  9. +--------------------------------------------------+
  10. | GRANT USAGE ON *.* TO 'yaya'@'%' |
  11. | GRANT ALL PRIVILEGES ON `gamedb`.* TO 'yaya'@'%' |
  12. +--------------------------------------------------+
  13. [root@pxcnode71 ~]#
  14.  
  15. [root@pxcnode73 ~]# mysql -uroot -p123456 -e 'show grants for yaya@"%" ' //查看
  16. mysql: [Warning] Using a password on the command line interface can be insecure.
  17. +--------------------------------------------------+
  18. | Grants for yaya@% |
  19. +--------------------------------------------------+
  20. | GRANT USAGE ON *.* TO 'yaya'@'%' |
  21. | GRANT ALL PRIVILEGES ON `gamedb`.* TO 'yaya'@'%' |
  22. +--------------------------------------------------+
  23. [root@pxcnode73 ~]#

2)客户端连接集群存取数据 (连接任意一台数据库服务器的ip地址都可以)

连接数据服务器主机73

  1. client50 ~]# mysql -h192.168.4.73 -uyaya -p123456 //连接服务器73
  2. mysql>
  3. mysql> create database gamedb; //建库
  4. Query OK, 1 row affected (0.19 sec)
  5.  
  6. mysql> create table gamedb.a(id int primary key auto_increment,name char(10));//建表
  7. Query OK, 0 rows affected (1.02 sec)
  8.  
  9. mysql> insert into gamedb.a(name)values("bob"),("tom"); //插入记录
  10. Query OK, 2 rows affected (0.20 sec)
  11. Records: 2 Duplicates: 0 Warnings: 0

3)在另外2台数据库服务器查看数据,客户端连接数据库服务器71主机查看数据。

  1. client50 ~]# mysql -h192.168.4.71 -uyaya -p123456 //连接服务器71
  2. mysql> select * from gamedb.a; //查看记录
  3. +----+-------+
  4. | id | name |
  5. +----+-------+
  6. | 2 | bob |
  7. | 5 | tom |

4)客户端连接数据库服务器73主机查看数据

  1. client50 ~]# mysql -h192.168.4.73 -uyaya -p123456 //连接服务器73
  2. mysql> select * from gamedb.a; //查看记录
  3. +----+-------+
  4. | id | name |
  5. +----+-------+
  6. | 2 | bob |
  7. | 5 | tom |

步骤三:测试故障自动恢复

1)停止数据库服务

停止3台服务器的任意一台主机的数据库服务都不会影响数据的存取。

  1. [root@pxcnode71 ~]# systemctl stop mysql //停止71主机的数据库服务
  2.  
  3.  
  4. Client50 ~]# client50 ~]# mysql -h192.168.4.72 -uyaya -p123456 //连接服务器72
  5. mysql> insert into gamedb.a(name)values("bob2"),("tom2");
  6. mysql> insert into gamedb.a(name)values("jerry"),("jack");
  7. Query OK, 2 rows affected (0.20 sec)
  8. Records: 2 Duplicates: 0 Warnings: 0

客户端50,连接数据库主机73,查看数据

  1. client50 ~]# mysql -h192.168.4.73 -uyaya -p123456 //连接服务器73
  2. mysql> select * from gamedb.a;
  3. +----+-------+
  4. | id | name |
  5. +----+-------+
  6. | 2 | bob |
  7. | 5 | tom |
  8. | 7 | bob2 |
  9. | 9 | tom2 |
  10. | 11 | jerry |
  11. | 13 | jack |
  12. +----+-------+
  13. 6 rows in set (0.00 sec)

3)启动71主机的数据库服务

数据库服务运行后,会自动同步宕机期间的数据。

  1. client50 ~]# mysql -h192.168.4.71 -uyaya -p123456 //连接服务器71
  2. mysql> select * from gamedb.a;
  3. +----+-------+
  4. | id | name |
  5. +----+-------+
  6. | 2 | bob |
  7. | 5 | tom |
  8. | 7 | bob2 |
  9. | 9 | tom2 |
  10. | 11 | jerry |
  11. | 13 | jack |
  12. +----+-------+
  13. rows in set (0.00 sec)

4 案例4:MySQL存储引擎的配置

4.1 问题

本案例要求MySQL数据存储引擎的使用,完成以下任务操作:

4.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:查看服务支持的存储引擎

登入MySQL服务器,查看当前支持哪些存储引擎。

使用mysql命令连接,以root用户登入:

  1. [root@dbsvr1 ~]# mysql -u root –p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 9
  5. Server version: 5.7.17 MySQL Community Server (GPL)
  6.  
  7. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  8.  
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12.  
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14.  
  15. mysql>

执行SHOW ENGINES\G指令可列表查看,MySQL 5.6可用的存储引擎有9种(除最后的FEDERATED以外,其他8种都支持),其中默认采用的存储引擎为InnoDB:

  1. mysql> SHOW ENGINES\G
  2. *************************** 1. row ***************************
  3. Engine: InnoDB
  4. Support: DEFAULT //此存储引擎为默认
  5. Comment: Supports transactions, row-level locking, and foreign keys
  6. Transactions: YES
  7. XA: YES
  8. Savepoints: YES
  9. *************************** 2. row ***************************
  10. Engine: MRG_MYISAM
  11. Support: YES
  12. Comment: Collection of identical MyISAM tables
  13. Transactions: NO
  14. XA: NO
  15. Savepoints: NO
  16. *************************** 3. row ***************************
  17. Engine: MEMORY
  18. Support: YES
  19. Comment: Hash based, stored in memory, useful for temporary tables
  20. Transactions: NO
  21. XA: NO
  22. Savepoints: NO
  23. *************************** 4. row ***************************
  24. Engine: BLACKHOLE
  25. Support: YES
  26. Comment: /dev/null storage engine (anything you write to it disappears)
  27. Transactions: NO
  28. XA: NO
  29. Savepoints: NO
  30. *************************** 5. row ***************************
  31. Engine: MyISAM
  32. Support: YES
  33. Comment: MyISAM storage engine
  34. Transactions: NO
  35. XA: NO
  36. Savepoints: NO
  37. *************************** 6. row ***************************
  38. Engine: CSV
  39. Support: YES
  40. Comment: CSV storage engine
  41. Transactions: NO
  42. XA: NO
  43. Savepoints: NO
  44. *************************** 7. row ***************************
  45. Engine: ARCHIVE
  46. Support: YES
  47. Comment: Archive storage engine
  48. Transactions: NO
  49. XA: NO
  50. Savepoints: NO
  51. *************************** 8. row ***************************
  52. Engine: PERFORMANCE_SCHEMA
  53. Support: YES
  54. Comment: Performance Schema
  55. Transactions: NO
  56. XA: NO
  57. Savepoints: NO
  58. *************************** 9. row ***************************
  59. Engine: FEDERATED
  60. Support: NO //此引擎不被支持
  61. Comment: Federated MySQL storage engine
  62. Transactions: NULL
  63. XA: NULL
  64. Savepoints: NULL
  65. 9 rows in set (0.01 sec)

步骤二:修改服务默认使用的存储引擎

在 mysql> 环境中,可以直接通过SET指令更改默认的存储引擎(只在本次连接会话过程中有效,退出重进即失效) 。比如临时修改为MyISAM,可执行下列操作:

  1. mysql> SET default_storage_engine=MyISAM;             //改用MyISAM引擎
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql> SHOW VARIABLES LIKE 'default_storage_engine';         //确认结果
  5. +------------------------+--------+
  6. | Variable_name | Value |
  7. +------------------------+--------+
  8. | default_storage_engine | MyISAM |
  9. +------------------------+--------+
  10. 1 row in set (0.00 sec)

若希望直接修改MySQL服务程序所采用的默认存储引擎,应将相关设置写入配置文件/etc/my.cnf,并重启服务后生效。比如:

  1. [root@dbsvr1 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. .. ..
  4. default_storage_engine=myisam                             //改用myisam引擎
  5.  
  6. [root@dbsvr1 ~]# systemctl restart mysqld.service         //重启服务

重新登入 mysql> 确认修改结果:

  1. [root@dbsvr1 ~]# mysql -u root -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 3
  5. Server version: 5.7.17 MySQL Community Server (GPL)
  6.  
  7. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  8.  
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12.  
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14.  
  15.  
  16. mysql> SHOW VARIABLES LIKE 'default_storage_engine';
  17. +------------------------+--------+
  18. | Variable_name | Value |
  19. +------------------------+--------+
  20. | default_storage_engine | MYISAM |                 //默认引擎已修改
  21. +------------------------+--------+
  22. 1 row in set (0.00 sec)
  23.  
  24. mysql> exit
  25. Bye

步骤三:查看表使用的存储引擎

登入MySQL服务器查看。

  1. mysql> show create table user \G; //查看建表命令
  2. *************************** 1. row ***************************
  3. Table: user
  4. Create Table: CREATE TABLE `user` (
  5. `id` int(11) NOT NULL AUTO_INCREMENT,
  6. `name` char(50) DEFAULT NULL,
  7. `age` tinyint(3) unsigned DEFAULT '19',
  8. `password` char(1) DEFAULT NULL,
  9. `uid` int(11) DEFAULT NULL,
  10. `gid` int(11) DEFAULT NULL,
  11. `comment` char(150) DEFAULT NULL,
  12. `homedir` char(50) DEFAULT NULL,
  13. `shell` char(50) DEFAULT NULL,
  14. PRIMARY KEY (`id`)
  15. ) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=latin1 //存储引擎是InnoDB
  16. 1 row in set (0.00 sec)
  17.  
  18. ERROR:
  19. No query specified
  20.  
  21. mysql>

步骤四:设置表使用的存储引擎

登入MySQL服务器设置。

  1. mysql> create table stuinfo( name char(10), age int )engine=memory;//设置
  2. Query OK, 0 rows affected (0.12 sec)
  3.  
  4. mysql>
  5. mysql> show create table stuinfo\G; //查看
  6. *************************** 1. row ***************************
  7. Table: stuinfo
  8. Create Table: CREATE TABLE `stuinfo` (
  9. `name` char(10) DEFAULT NULL,
  10. `age` int(11) DEFAULT NULL
  11. ) ENGINE=MEMORY DEFAULT CHARSET=latin1 //存储引擎名
  12. 1 row in set (0.00 sec)
  13.  
  14. ERROR:
  15. No query specified
  16.  
  17. mysql>

步骤五:修改表存储引擎

登入MySQL服务器修改。

  1. mysql> alter table stuinfo engine=innodb; //修改
  2. Query OK, 0 rows affected (0.54 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4.  
  5. mysql>
  6. mysql> show create table stuinfo\G; //查看
  7. *************************** 1. row ***************************
  8. Table: stuinfo
  9. Create Table: CREATE TABLE `stuinfo` (
  10. `name` char(10) DEFAULT NULL,
  11. `age` int(11) DEFAULT NULL
  12. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 //当前存储引擎名
  13. 1 row in set (0.00 sec)
  14.  
  15. ERROR:
  16. No query specified
  17.  
  18. mysql>

5 案例5:事务特性

5.1 问题

具体操作如下:

5.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:创建innodb存储引擎的表

1)数据库管理员root登录,创建新库、新表。

  1. [root@host50 ~]# mysql -uroot -p123456 //访问服务
  2. mysql> create database db10; //建库
  3. Query OK, 1 row affected (0.05 sec)
  4.  
  5. mysql> use db10; //切换库
  6. Database changed
  7. mysql>
  8. mysql> create table a(id int)engine=innodb; //建表并指定存储引擎
  9. Query OK, 0 rows affected (0.52 sec)

步骤二:关闭服务的自动提交功能

1)数据库管理员root登录,关闭服务的自动提交功能。

  1. mysql> show variables like "%commit%"; //查看所有包涵commit 字样的变量
  2. +-----------------------------------------+-------+
  3. | Variable_name | Value |
  4. +-----------------------------------------+-------+
  5. | autocommit | ON |     //自动提交功能开启
  6. | binlog_group_commit_sync_delay | 0 |
  7. | binlog_group_commit_sync_no_delay_count | 0 |
  8. | binlog_order_commits | ON |
  9. | innodb_api_bk_commit_interval | 5 |
  10. | innodb_commit_concurrency | 0 |
  11. | innodb_flush_log_at_trx_commit | 1 |
  12. | slave_preserve_commit_order | OFF |
  13. +-----------------------------------------+-------+
  14. 8 rows in set (0.01 sec)
  15.  
  16. mysql> set autocommit=off ; 关闭自动提交
  17. Query OK, 0 rows affected (0.00 sec)
  18.  
  19. mysql> show variables like "autocommit"; //查看
  20. +---------------+-------+
  21. | Variable_name | Value |
  22. +---------------+-------+
  23. | autocommit | OFF |            //已处于关闭状态
  24. +---------------+-------+
  25. 1 row in set (0.00 sec)
  26. mysql>

步骤三:测试事务特性

1)插入新记录,不执行提交命令commit

  1. mysql> insert into db10.a values(101); //插入记录
  2. Query OK, 1 row affected (0.00 sec)
  3.  
  4. mysql> select * from db10.a ;//查看记录
  5. +------+
  6. | id |
  7. +------+
  8. | 101 |
  9. +------+
  10. 1 row in set (0.00 sec)
  11.  
  12. mysql>

2)打开新终端访问数据服务,查看不到插入的数据

注:此处打开的终端称为终端2 , 此处之前终端被称为终端1

  1. [root@host50 ~]# mysql -uroot -p123456
  2. mysql> select * from db10.a; //没有记录
  3. Empty set (0.00 sec)
  4.  
  5. mysql>

3)终端1 执行提交命令commit

  1. mysql> select * from db10.a ;
  2. +------+
  3. | id |
  4. +------+
  5. | 101 |
  6. +------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql> commit ; 执行提交命令
  10. Query OK, 0 rows affected (0.08 sec)

4)第终端2执行查看命令

  1. mysql> select * from db10.a;
  2. Empty set (0.00 sec)
  3.  
  4. mysql> select * from db10.a; //查看到数据
  5. +------+
  6. | id |
  7. +------+
  8. | 101 |
  9. +------+
  10. 1 row in set (0.00 sec)
  11.  
  12. mysql>

5)在终端1删除记录 ,不执行提交命令commit

将/etc/passwd文件复制到/var/lib/mysql-files/目录下,

  1. mysql> select * from db10.a ; //删除前查看
  2. +------+
  3. | id |
  4. +------+
  5. | 101 |
  6. +------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql> delete from db10.a ;//删除表记录
  10. Query OK, 1 row affected (0.00 sec)
  11.  
  12. mysql>
  13. mysql> select * from db10.a ; //查看不到记录
  14. Empty set (0.00 sec)

6)在终端2 依然可以查看到记录

  1. mysql> select * from db10.a;
  2. +------+
  3. | id |
  4. +------+
  5. | 101 |
  6. +------+
  7. 1 row in set (0.00 sec)
  8.  
  9. mysql>

7)在终端1,执行回滚命令

  1. mysql> select * from db10.a ; //回滚前查看
  2. Empty set (0.00 sec)
  3.  
  4. mysql>
  5. mysql> rollback ; //数据回滚
  6. Query OK, 0 rows affected (0.03 sec)
  7.  
  8. mysql>
  9. mysql> select * from db10.a ; //回滚后查看
  10. +------+
  11. | id |
  12. +------+
  13. | 101 |
  14. +------+
  15. 1 row in set (0.00 sec)
  16.  
  17. mysql>
  18.  
  19. mysql> delete from db10.a ; //删除记录
  20. Query OK, 1 row affected (0.00 sec)
  21.  
  22. mysql> commit ; //提交
  23. Query OK, 0 rows affected (0.08 sec)
  24.  
  25. mysql>
  26. mysql> rollback ; //数据回滚
  27. Query OK, 0 rows affected (0.00 sec)
  28.  
  29. mysql> select * from db10.a ; //查看不到记录
  30. Empty set (0.00 sec)
  31.  
  32. mysql>

8)在终端2 也查看不到记录

  1. mysql> select * from db10.a;
  2. Empty set (0.00 sec)