转载连接 http://blog.itpub.net/12679300/viewspace-2144619/

【前言】测试环境中经常需要多台mysql数据库来进行各种环境和场景的模拟,由于测试环境中的资源一般都是比较有限的,也就需要在一台服务器上面搭建多个MysqL数据库来完成测试的需求。本文档介绍在Centos6.4的环境中安装多个MysqL 5.7数据库的操作方法

【1】MysqL数据库软件的安装

MysqL数据库软件官方提供了三种的安装文件方法,如下:

  • RPM方式安装

  • 二进制方式安装

  • 源码编译安装

虽然源码安装的方式比较麻烦,但是个人还是习惯用源码的方案安装,MysqL5.7的安装跟早起5.5的安装方式和步骤也几乎一样所以这里就不再说明了。


【2】创建多个数据库

软件的安装完成其实都是一样的,就是创建数据库的时候有点不一样。

2.1 进行数据文件目录的规划,本环境安装了5个数据库,在/data下面创建5个文件夹,并用端口号区分


点击( 此处 )折叠或打开

  1. [ root@db01 data ] #  pwd

  2. /data

  3. [ root@db01 data ] #  ll

  4. drwxr - xr - x .  6 MysqL MysqL 4096 7月 8 05 : 00 MysqL3306

  5. drwxr - xr - x .  6 MysqL MysqL 4096 7月 20 21 : 46 MysqL3307

  6. drwxr - xr - x .  7 MysqL MysqL 4096 7月 20 21 : 46 MysqL3308

  7. drwxr - xr - x .  6 MysqL MysqL 4096 7月 20 21 : 46 MysqL3309

  8. drwxr - xr - x .  5 MysqL MysqL 4096 7月 20 21 : 46 MysqL3310

2.2 进行参数文件的配置


点击( 此处 )折叠或打开

  1. [ root@db01 data ] #  vi /etc/my . cnf


  2. [ MysqLd_multi ]

  3. MysqLd  =  /usr/ local / MysqL/bin/MysqLd_safe

  4. #basedir  =  /usr/ local / MysqL

  5. MysqLadmin  =  /usr/ local / MysqL/bin/MysqLadmin

  6. user  =  MysqL

  7. pass  =  MysqL

  8. #password  =  MysqL

  9. #bindir  =  /usr/ local / MysqL/bin


  10. [ MysqLd7 ]

  11. port  =  3307

  12. socket  =  /tmp/MysqL . sock7

  13. pid - file  =  /data/MysqL3307/hostname . pid7

  14. datadir  =  /data/MysqL3307

  15. user  =  MysqL

  16. basedir  =  /usr/ local / MysqL

  17. log - bin  =  /data/MysqL3307/bin - log

  18. server_id  =  7

  19. gtid_mode  =  ON

  20. enforce - gtid - consistency = TRUE

  21. log_slave_updates =  ON

  22. #skip - grant - tables


  23. [ MysqLd8 ]

  24. socket  =  /tmp/MysqL . sock8

  25. port  =  3308

  26. pid - file  =  /data/MysqL3308/hostname . pid8

  27. datadir  =  /data/MysqL3308

  28. user  =  MysqL

  29. basedir  =  /usr/ local / MysqL

  30. #skip - grant - tables

  31. log - bin  =  /data/MysqL3308/bin - log

  32. server_id = 8

  33. gtid_mode = ON

  34. enforce - gtid - consistency = TRUE

  35. log_slave_updates =  ON


  36. [ MysqLd9 ]

  37. socket  =  /tmp/MysqL . sock9

  38. port  =  3309

  39. pid - file  =  /data/MysqL3309/hostname . pid9

  40. datadir  =  /data/MysqL3309

  41. user  =  MysqL

  42. basedir  =  /usr/ local / MysqL

  43. #skip - grant - tables

  44. log - bin  =  /data/MysqL3309/bin - log

  45. server_id =  9

  46. gtid_mode = ON

  47. enforce - gtid - consistency = TRUE

  48. log_slave_updates =  ON


  49. [ MysqLd10 ]

  50. socket  =  /tmp/MysqL . sock10

  51. port  =  3310

  52. pid - file  =  /data/MysqL3310/hostname . pid10

  53. datadir  =  /data/MysqL3310

  54. user  =  MysqL

  55. basedir  =  /usr/ local / MysqL

  56. #skip - grant - tables

  57. log - bin  =  /data/MysqL3310/bin - log

  58. server_id =  10

  59. gtid_mode = ON

  60. enforce - gtid - consistency = TRUE

  61. log_slave_updates =  ON


2.3 创建数据库,创建的过程中需要记录数据库的初始密码

依次创建其他数据库,记录认的随机密码

点击( 此处 )折叠或打开

  1. [ root@db01 data ] #  /usr/ local / MysqL/bin/MysqLd  - - initialize  - - datadir = / data/MysqL3307

  2. 2017 - 07 - 20T14 : 31 : 01 . 890314Z 0  [ Warning ]  TIMESTAMP with implicit DEFAULT value is deprecated .  Please use  - - explicit_defaults_for_timestamp server  option   ( see documentation  for  more details ) .

  3. 2017 - 07 - 20T14 : 31 : 09 . 081679Z 0  [ Warning ]  InnoDB :  New log files created ,  LSN = 45790

  4. 2017 - 07 - 20T14 : 31 : 09 . 626403Z 0  [ Warning ]  InnoDB :  Creating foreign key constraint system tables .

  5. 2017 - 07 - 20T14 : 31 : 09 . 867983Z 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 :  12d65efe - 6d58 - 11e7 - 9d39 - 000c29a755d3 .

  6. 2017 - 07 - 20T14 : 31 : 09 . 873982Z 0  [ Warning ]  Gtid table is  not  ready to be used .  Table  'MysqL.gtid_executed'  cannot be opened .

  7. 2017 - 07 - 20T14 : 31 : 09 . 923044Z 1  [ Note ]  A temporary password is generated  for  root@localhost :  wAQ * p > . O : 4 , p

  1. [ root@db01 MysqL3307 ] #  cat auto . cnf

  2. [ auto ]

  3. server - uuid = 12d65efe - 6d58 - 11e7 - 9d39 - 000c29a755d3

  4. [ root@db01 MysqL3307 ] #  /usr/ local / MysqL/bin/MysqLd  - - initialize  - - datadir = / data/MysqL3308

  5. 2017 - 07 - 20T14 : 32 : 36 . 027225Z 0  [ Warning ]  TIMESTAMP with implicit DEFAULT value is deprecated .  Please use  - - explicit_defaults_for_timestamp server  option   ( see documentation  for  more details ) .

  6. 2017 - 07 - 20T14 : 32 : 38 . 601806Z 0  [ Warning ]  InnoDB :  New log files created ,  LSN = 45790

  7. 2017 - 07 - 20T14 : 32 : 39 . 071963Z 0  [ Warning ]  InnoDB :  Creating foreign key constraint system tables .

  8. 2017 - 07 - 20T14 : 32 : 39 . 167438Z 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 :  48106897 - 6d58 - 11e7 - a1b9 - 000c29a755d3 .

  9. 2017 - 07 - 20T14 : 32 : 39 . 172770Z 0  [ Warning ]  Gtid table is  not  ready to be used .  Table  'MysqL.gtid_executed'  cannot be opened .

  10. 2017 - 07 - 20T14 : 32 : 39 . 212540Z 1  [ Note ]  A temporary password is generated  for  root@localhost :  Ak3XwQpb = ta0

  1. [ root@db01 MysqL3307 ] #  /usr/ local / MysqL/bin/MysqLd  - - initialize  - - datadir = / data/MysqL3309

  2. 2017 - 07 - 20T14 : 33 : 32 . 801680Z 0  [ Warning ]  TIMESTAMP with implicit DEFAULT value is deprecated .  Please use  - - explicit_defaults_for_timestamp server  option   ( see documentation  for  more details ) .

  3. 2017 - 07 - 20T14 : 33 : 35 . 102950Z 0  [ Warning ]  InnoDB :  New log files created ,  LSN = 45790

  4. 2017 - 07 - 20T14 : 33 : 35 . 443411Z 0  [ Warning ]  InnoDB :  Creating foreign key constraint system tables .

  5. 2017 - 07 - 20T14 : 33 : 35 . 557451Z 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 :  69acd736 - 6d58 - 11e7 - a436 - 000c29a755d3 .

  6. 2017 - 07 - 20T14 : 33 : 35 . 562713Z 0  [ Warning ]  Gtid table is  not  ready to be used .  Table  'MysqL.gtid_executed'  cannot be opened .

  7. 2017 - 07 - 20T14 : 33 : 35 . 607109Z 1  [ Note ]  A temporary password is generated  for  root@localhost :  sDXL5hh71I > R

点击( 此处 )折叠或打开

  1. [ root@db01 MysqL3307 ] #  /usr/ local / MysqL/bin/MysqLd  - - initialize  - - datadir = / data/MysqL3310

  2. 2017 - 07 - 20T14 : 34 : 14 . 881243Z 0  [ Warning ]  TIMESTAMP with implicit DEFAULT value is deprecated .  Please use  - - explicit_defaults_for_timestamp server  option   ( see documentation  for  more details ) .

  3. 2017 - 07 - 20T14 : 34 : 17 . 227399Z 0  [ Warning ]  InnoDB :  New log files created ,  LSN = 45790

  4. 2017 - 07 - 20T14 : 34 : 17 . 744012Z 0  [ Warning ]  InnoDB :  Creating foreign key constraint system tables .

  5. 2017 - 07 - 20T14 : 34 : 17 . 904000Z 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 :  82ea694b - 6d58 - 11e7 - a566 - 000c29a755d3 .

  6. 2017 - 07 - 20T14 : 34 : 17 . 908498Z 0  [ Warning ]  Gtid table is  not  ready to be used .  Table  'MysqL.gtid_executed'  cannot be opened .

  7. 2017 - 07 - 20T14 : 34 : 17 . 923365Z 1  [ Note ]  A temporary password is generated  for  root@localhost :  Rq4 * Teq#l ; Ve

【3】 修改数据库的初始密码

先启动数据库


  1. [ root@db01 MysqL3307 ] #  /usr/ local / MysqL/bin/MysqLd_multi start

  2. 修改认密码

  3. [ root@db01 MysqL3307 ] #  MysqLadmin  - u root  - p  - P 3307  - S /tmp/MysqL . sock7 password

  4. Enter  password :  输入认密码

  5. New password :

  6. Confirm new password :

  7. Warning :  Since password will be sent to server  in  plain text ,  use ssl connection to ensure password safety .


登录数据库

[root@db01 MysqL3307]# MysqL -P 3307 -S /tmp/MysqL.sock7 -u root  – p


认的密码登录会有以下的提示信息:

点击( 此处 )折叠或打开

  1. MysqL >  show databases ;

  2. ERROR 1820  ( HY000 ) :  You must reset your password using ALTER USER statement before executing  this  statement .


【4】设置MysqLd_multi stop的关闭权限

认的情况下,不能通过MysqLd_multi关闭数据库,需要进行额外的设置,步骤如下:

创建MysqL关闭用户
点击( 此处 )折叠或打开

  1. [ root@db01 MysqL3307 ] #  MysqL  - u root  - p  - P 3310  - S /tmp/MysqL . sock10

  2. Enter  password :

  3. MysqL >  grant shutdown on  * . *  to  'MysqL' @ 'localhost'  identified by  'MysqL' ;

  4. MysqL >  flush privileges

设置参数文件的账户
点击( 此处 )折叠或打开
  1. [ root@db01 MysqL3307 ] #  cat /etc/my . cnf

  2. [ MysqLd_multi ]

  3. MysqLd  =  /usr/ local / MysqL/bin/MysqLd_safe

  4. #basedir  =  /usr/ local / MysqL

  5. MysqLadmin  =  /usr/ local / MysqL/bin/MysqLadmin

  6. user  =  MysqL

  7. pass  =  MysqL

通过以上的操作,便完成了在单台服务器上面安装多个MysqL数据库的操作;

附加:常用的操作语句

/usr/local/MysqL/bin/MysqLd_multi start #启动所有的数据库

/usr/local/MysqL/bin/MysqLd_multi start 7 #启动单台数据库

/usr/local/MysqL/bin/MysqLd_multi stop #关闭所有的数据库

/usr/local/MysqL/bin/MysqLd_multi stop 7 #关闭单台数据库


相关文章

这篇文章主要介绍“hive和mysql的区别是什么”,在日常操作中...
这篇“MySQL数据库如何改名”文章的知识点大部分人都不太理解...
这篇文章主要介绍“mysql版本查询命令是什么”的相关知识,小...
本篇内容介绍了“mysql怎么修改字段的内容”的有关知识,在实...
这篇文章主要讲解了“mysql怎么删除unique约束”,文中的讲解...
今天小编给大家分享一下mysql怎么查询不为空的字段的相关知识...