MySQL多实例安装

 

1.安装MySQL需要的依赖的包和编译软件
 
(1)安装MySQL需要的依赖包
安装MySQL之前,最好先安装MySQL需要的依赖包,不然后面会出现报错,还得回来安装MySQL的依赖包。
  1. [root@Mysql-server tools]# yum install -y ncurses-devel libaio-devel
  2. [root@Mysql-server tools]# rpm -qa ncurses-devel libaio-devel
  3. ncurses-devel-5.7-4.20090207.el6.x86_64
  4. libaio-devel-0.3.107-10.el6.x86_64
 
(2)安装编译MySQL需要的软件
要安装cmake编译软件:
  1. [root@Mysql-server tools]# tar xf cmake-2.8.8.tar.gz
  2. [root@Mysql-server tools]# cd cmake-2.8.8
  3. [root@Mysql-server cmake-2.8.8]#./configure
  4. [root@mysql cmake-2.8.8]# gmake
  5. [root@mysql cmake-2.8.8]# gmake install
2.开始安装MySQL
2.1 创建用户mysql
  1. [root@mysql tools]# useradd mysql -s /sbin/nologin -M
  2. [root@mysql tools]# id mysql uid=501(mysql) gid=501(mysql) 组=501(mysql) [root@mysql tools]#
2.2 解压编译MySQL
  1. tar zxf mysql-5.5.32.tar.gz cd mysql-5.5.32
  2. cmake .-DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \
  3. -DMYSQL_DATADIR=/application/mysql-5.5.32/data \
  4. -DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock \
  5. -DDEFAULT_CHARSET=utf8 \
  6. -DDEFAULT_COLLATION=utf8_general_ci \
  7. -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
  8. -DENABLED_LOCAL_INFILE=ON \
  9. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  10. -DWITH_FEDERATED_STORAGE_ENGINE=1 \
  11. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  12. -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
  13. -DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
  14. -DWITH_FAST_MUTEXES=1 \
  15. -DWITH_ZLIB=bundled \
  16. -DENABLED_LOCAL_INFILE=1 \
  17. -DWITH_READLINE=1 \
  18. -DWITH_EMBEDDED_SERVER=1 \
  19. -DWITH_DEBUG=0
  20. #-- Build files have been written to: /home/oldboy/tools/mysql-5.5.32
  21. 提示,编译时可配置的选项很多,具体可参考结尾附录或官方文档:
  22. make
  23. #[100%] Built target my_safe_process
  24. make install
  25. ln -s /application/mysql-5.5.32//application/mysql
  26. 如果上述操作未出现错误,则MySQL5.5.32软件cmake方式的安装就算成功了。
编译过程中出现如下报错,解决方法为:
  1. [root@mysql mysql-5.5.32]# rm CMakeCache.txt ###删除这个文件,重新cmake
 
 
  1. [root@mysql mysql-5.5.32]# mkdir -p /data/{3306,3307}/data
  2. [root@mysql mysql-5.5.32]# tree /data/
  3. /data/
  4. ├──3306
  5. │??└── data ##实例3306的数据文件
  6. ├──3307
  7. │??└── data ##实例3307的数据文件
  8. └── test
2.3 接着创建配置文件,将data.zip文件上传到/(根)下,并且使用unzip命令解压
  1. [root@mysql /]# unzip data.zip ##上传到根下,加压会替换上面创建的/data目录
  2. Archive: data.zip
  3. inflating: data/3306/my.cnf
  4. inflating: data/3306/mysql
  5. inflating: data/3307/my.cnf
  6. inflating: data/3307/mysql
  7. [root@mysql /]# tree /data
  8. data/ data.zip
  9. [root@mysql /]# tree data
  10. data/ data.zip
  11. [root@mysql /]# tree data/
  12. data/
  13. ├──3306
  14. │??├── data
  15. │??├── my.cnf ##实例的配置文件
  16. │??└── mysql ##实例的启动文件
  17. ├──3307
  18. │??├── data
  19. │??├── my.cnf ##实例的配置文件
  20. │??└── mysql ##实例的启动文件
  21. └── test
/data/3306/my.cnf配置文件信息
  1. [root@mysql /]# cat /data/3306/my.cnf
  2. [client]
  3. port =3306
  4. socket =/data/3306/mysql.sock
  5. [mysql]
  6. no-auto-rehash
  7. [mysqld]
  8. user = mysql
  9. port =3306
  10. socket =/data/3306/mysql.sock
  11. basedir =/application/mysql
  12. datadir =/data/3306/data
  13. open_files_limit =1024
  14. back_log =600
  15. max_connections =800
  16. max_connect_errors =3000
  17. table_cache =614
  18. external-locking = FALSE
  19. max_allowed_packet =8M
  20. sort_buffer_size =1M
  21. join_buffer_size =1M
  22. thread_cache_size =100
  23. thread_concurrency =2
  24. query_cache_size =2M
  25. query_cache_limit =1M
  26. query_cache_min_res_unit =2k
  27. #default_table_type = InnoDB
  28. thread_stack =192K
  29. #transaction_isolation = READ-COMMITTED
  30. tmp_table_size =2M
  31. max_heap_table_size =2M
  32. long_query_time =1
  33. #log_long_format
  34. #log-error = /data/3306/error.log
  35. #log-slow-queries = /data/3306/slow.log
  36. pid-file =/data/3306/mysql.pid
  37. log-bin =/data/3306/mysql-bin
  38. relay-log =/data/3306/relay-bin
  39. relay-log-info-file =/data/3306/relay-log.info
  40. binlog_cache_size =1M
  41. max_binlog_cache_size =1M
  42. max_binlog_size =2M
  43. expire_logs_days =7
  44. key_buffer_size =16M
  45. read_buffer_size =1M
  46. read_rnd_buffer_size =1M
  47. bulk_insert_buffer_size =1M
  48. #myisam_sort_buffer_size = 1M
  49. #myisam_max_sort_file_size = 10G
  50. #myisam_max_extra_sort_file_size = 10G
  51. #myisam_repair_threads = 1
  52. #myisam_recover
  53. lower_case_table_names =1
  54. skip-name-resolve
  55. slave-skip-errors =1032,1062
  56. replicate-ignore-db=mysql
  57. server-id =1
  58. innodb_additional_mem_pool_size =4M
  59. innodb_buffer_pool_size =32M
  60. innodb_data_file_path = ibdata1:128M:autoextend
  61. innodb_file_io_threads =4
  62. innodb_thread_concurrency =8
  63. innodb_flush_log_at_trx_commit =2
  64. innodb_log_buffer_size =2M
  65. innodb_log_file_size =4M
  66. innodb_log_files_in_group =3
  67. innodb_max_dirty_pages_pct =90
  68. innodb_lock_wait_timeout =120
  69. innodb_file_per_table =0
  70. [mysqldump]
  71. quick
  72. max_allowed_packet =2M
  73. [mysqld_safe]
  74. log-error=/data/3306/mysql_oldboy3306.err
  75. pid-file=/data/3306/mysqld.pid
/data/3307/my.cnf配置文件参数
  1. [root@mysql /]# cat /data/3307/my.cnf
  2. [client]
  3. port =3307
  4. socket =/data/3307/mysql.sock
  5. [mysql]
  6. no-auto-rehash
  7. [mysqld]
  8. user = mysql
  9. port =3307
  10. socket =/data/3307/mysql.sock
  11. basedir =/application/mysql
  12. datadir =/data/3307/data
  13. open_files_limit =1024
  14. back_log =600
  15. max_connections =800
  16. max_connect_errors =3000
  17. table_cache =614
  18. external-locking = FALSE
  19. max_allowed_packet =8M
  20. sort_buffer_size =1M
  21. join_buffer_size =1M
  22. thread_cache_size =100
  23. thread_concurrency =2
  24. query_cache_size =2M
  25. query_cache_limit =1M
  26. query_cache_min_res_unit =2k
  27. #default_table_type = InnoDB
  28. thread_stack =192K
  29. #transaction_isolation = READ-COMMITTED
  30. tmp_table_size =2M
  31. max_heap_table_size =2M
  32. #long_query_time = 1
  33. #log_long_format
  34. #log-error = /data/3307/error.log
  35. #log-slow-queries = /data/3307/slow.log
  36. pid-file =/data/3307/mysql.pid
  37. #log-bin = /data/3307/mysql-bin
  38. relay-log =/data/3307/relay-bin
  39. relay-log-info-file =/data/3307/relay-log.info
  40. binlog_cache_size =1M
  41. max_binlog_cache_size =1M
  42. max_binlog_size =2M
  43. expire_logs_days =7
  44. key_buffer_size =16M
  45. read_buffer_size =1M
  46. read_rnd_buffer_size =1M
  47. bulk_insert_buffer_size =1M
  48. #myisam_sort_buffer_size = 1M
  49. #myisam_max_sort_file_size = 10G
  50. #myisam_max_extra_sort_file_size = 10G
  51. #myisam_repair_threads = 1
  52. #myisam_recover
  53. lower_case_table_names =1
  54. skip-name-resolve
  55. slave-skip-errors =1032,1062
  56. replicate-ignore-db=mysql
  57. server-id =3
  58. innodb_additional_mem_pool_size =4M
  59. innodb_buffer_pool_size =32M
  60. innodb_data_file_path = ibdata1:128M:autoextend
  61. innodb_file_io_threads =4
  62. innodb_thread_concurrency =8
  63. innodb_flush_log_at_trx_commit =2
  64. innodb_log_buffer_size =2M
  65. innodb_log_file_size =4M
  66. innodb_log_files_in_group =3
  67. innodb_max_dirty_pages_pct =90
  68. innodb_lock_wait_timeout =120
  69. innodb_file_per_table =0
  70. [mysqldump]
  71. quick
  72. max_allowed_packet =2M
  73. [mysqld_safe]
  74. log-error=/data/3307/mysql_oldboy3307.err
  75. pid-file=/data/3307/mysqld.pid
 
特别说明:
本文多实例启动文件的启动mysql服务实质命令:
mysqld_safe --defaults-file=/data /3306/my.cnf 2>&1 /dev/null &
mysqld_safe --defaults-file=/data /3307/my.cnf 2>&1 /dev/null &
 
本文多实例启动文件的停止mysql服务实质命令:
mysqladmin -u root -p123456 -S /data/3306/mysql.sock shutdown
mysqladmin -u root -p123456 -S /data/3307/mysql.sock shutdown
 
2.4 修改数据文件/data 权限
  1. [root@mysql /]# chown -R mysql.mysql /data
  2. [root@mysql /]# find /data -type f -name "my.cnf"|xargs ls -l ##将my.cnf文件权限设置成644
  3. -rw-r--r--.1 mysql mysql 189910292013/data/3306/my.cnf
  4. -rw-r--r--.1 mysql mysql 190110292013/data/3307/my.cnf
  5. [root@mysql /]# find /data -type f -name "mysql"|xargs ls -l ##由于mysql脚本中保存了登录数据库的密码,需要将文件权限设置成700,只允许root,mysql访问
  6. -rw-r--r--.1 mysql mysql 13077152013/data/3306/mysql
  7. -rw-r--r--.1 mysql mysql 13077212013/data/3307/mysql
  8. [root@mysql /]# find /data -type f -name "mysql"|xargs chmod 700
  9. [root@mysql /]# find /data -type f -name "mysql"|xargs ls -l
  10. -rwx------.1 mysql mysql 13077152013/data/3306/mysql
  11. -rwx------.1 mysql mysql 13077212013/data/3307/mysql
2.5 配置MySQL命令全局使用路径
     如果不为MySQL的命令配置全局路径,就无法直接在命令行输入mysql,这样只能用全路径/application/msyq/bin/mysql 这样带着路径输出会比较麻烦
 
2.5.1 配置MySQL全局路径方法
1.确认mysql命令所在路径
  1. [root@mysql /]# ll /application/mysql/bin/mysql
  2. -rwxr-xr-x.1 root root 53453564816:05/application/mysql/bin/mysql
2. 修改PATH路径
  1. [root@mysql /]# tail -1/etc/profile
  2. export PATH=/application/mysql/bin:$PATH ##添加如下行
  3. [root@mysql /]# source /etc/profile ##让其生效
3. 最后检查PATH
  1. [root@mysql /]# echo $PATH
  2. /application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
2.6 初始化数据库
  1. cd /application/mysql/scripts/##注意和MySQL 5.1的路径不同,MySQL 5.1 不在mysql/bin下
  2. ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data/--user=mysql
  3. ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data/--user=mysql
注意:
当初始化的过程中出现两个OK时,表示初始化成功

3. 最后启动mysql
  1. [root@mysql scripts]#/data/3306/mysql start
  2. StartingMySQL...
  3. [root@mysql scripts]#/data/3307/mysql start
  4. StartingMySQL...
  5. 查看是否启动成功
  6. [root@mysql scripts]# netstat -lntup|grep mysql
  7. tcp 000.0.0.0:33060.0.0.0:* LISTEN 4614/mysqld
  8. tcp 000.0.0.0:33070.0.0.0:* LISTEN 5412/mysqld
4. 登录mysql
mysql -uroot -p123456 -S /data/3306/mysql.sock
5. 多实例修改mysql 密码
  1. [root@mysql scripts]# mysqladmin -uroot password 123456 -S /data/3306/mysql.sock
  2. [root@mysql scripts]# mysqladmin -uroot password 123456 -S /data/3307/mysql.sock
6. 修改配置文件中的mysql连接密码,如果不修改否则在停库的时候会报错
  1. [root@mysql scripts]# sed -i '13 s#oldboy#123456#g'/data/3306/mysql
  2. [root@mysql scripts]# sed -n '13p'/data/3306/mysql
  3. mysql_pwd="123456"
  4. [root@mysql scripts]# sed -i '13 s#oldboy#123456#g' /data/3307/mysql [root@mysql scripts]# sed -n '13p' /data/3307/mysql mysql_pwd="123456" [root@mysql scripts]#
7.停止mysql数据库
  1. [root@mysql scripts]#/data/3306/mysql stop
  2. StopingMySQL...
  3. [root@mysql scripts]#/data/3307/mysql stop
  4. StopingMySQL...
  5. [root@mysql scripts]#
 
新增一个多实例库3308,操作命令如下
 





附件列表

 

posted @ 2016-04-08 21:50  Simon92  阅读(2101)  评论(0编辑  收藏  举报