[关闭]
@yanglt7 2018-10-21T15:56:37.000000Z 字数 7077 阅读 739

【Web 集群实战】16_安装并配置多实例 MySQL 数据库

Web集群实战


1. 安装 MySQL 多实例

可参考文章【Web 集群实战】12_MySQL 的安装与配置

2. 创建 MySQL 多实例的数据文件目录

  1. [root@ylt001 ~]# mkdir -p /data/{3306,3307}/data
  2. [root@ylt001 ~]# tree /data/
  3. /data
  4. |-- 3306
  5. | `-- data
  6. `-- 3307
  7. `-- data
  8. 4 directories, 0 files

3. 创建 MySQL 多实例的配置文件

  1. [root@ylt001 3306]# cat my.cnf
  2. [client]
  3. port = 3306
  4. socket = /data/3306/mysql.sock
  5. [mysqld]
  6. port = 3306
  7. socket = /data/3306/mysql.sock
  8. basedir = /application/mysql
  9. datadir = /data/3306/data
  10. pid-file = /data/3306/mysql.pid
  11. relay-log = /data/3306/relay-bin
  12. relay-log-info-file = /data/3306/relay-log.info
  13. skip-external-locking
  14. key_buffer_size = 16K
  15. max_allowed_packet = 1M
  16. table_open_cache = 4
  17. sort_buffer_size = 64K
  18. read_buffer_size = 256K
  19. read_rnd_buffer_size = 256K
  20. net_buffer_length = 2K
  21. thread_stack = 128K
  22. server-id = 1
  23. [mysqldump]
  24. quick
  25. max_allowed_packet = 16M
  26. [mysql]
  27. no-auto-rehash
  28. # Remove the next comment character if you are not familiar with SQL
  29. #safe-updates
  30. [myisamchk]
  31. key_buffer_size = 8M
  32. sort_buffer_size = 8M
  33. [mysqlhotcopy]
  34. interactive-timeout
  35. [mysqld_safe]
  36. log-error=/data/3306/mysql_ylt3306.err
  37. pid-file=/data/3306/mysqld.pid
  1. [root@ylt001 3306]# cp /data/3306/my.cnf /data/3307/
  2. [root@ylt001 3306]# sed -i 's/3306/3307/g' /data/3307/my.cnf
  3. [root@ylt001 3306]# sed -i 's/server-id = 1/sever-id = 3/g' /data/3307/my.cnf
  4. [root@ylt001 3306]# touch /data/{3306/mysql_ylt3306.err,3307/mysql_ylt3307.err}
  5. [root@ylt001 3306]# tree /data
  6. /data
  7. |-- 3306
  8. | |-- data
  9. | |-- my.cnf
  10. | `-- mysql_ylt3306.err
  11. `-- 3307
  12. |-- data
  13. |-- my.cnf
  14. `-- mysql_ylt3307.err
  15. 4 directories, 4 files

4. 创建 MySQL 多实例的启动文件

  1. [root@ylt001 3306]# cat mysql
  2. #!/bin/sh
  3. ############
  4. #this scripts is created by ylt at 2018-09-30
  5. #init
  6. port=3306
  7. mysql_user="root"
  8. mysql_pwd="password"
  9. CmdPath="/application/mysql/bin"
  10. mysql_sock="/data/${port}/mysql.sock"
  11. #startup function
  12. function_start_mysql()
  13. {
  14. if [ ! -e "$mysql_sock" ];then
  15. printf "Starting MySQL...\n"
  16. /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
  17. else
  18. printf "MySQL is running...\n"
  19. exit
  20. fi
  21. }
  22. #stop function
  23. function_stop_mysql()
  24. {
  25. if [ ! -e "$mysql_sock" ];then
  26. printf "MySQL is stopped...\n"
  27. exit
  28. else
  29. printf "Stopping MySQL...\n"
  30. ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
  31. fi
  32. }
  33. #restart function
  34. function_restart_mysql()
  35. {
  36. printf "Restarting MySQL...\n"
  37. function_stop_mysql
  38. sleep 2
  39. function_start_mysql
  40. }
  41. case $1 in
  42. start)
  43. function_start_mysql
  44. ;;
  45. stop)
  46. function_stop_mysql
  47. ;;
  48. restart)
  49. function_restart_mysql
  50. ;;
  51. *)
  52. printf "Usage: /data/${port}/mysql{start|stop|restart}\n"
  53. esac
  1. [root@ylt001 3306]# cp /data/3306/mysql /data/3307/
  2. [root@ylt001 3306]# sed -i 's/3306/3307/g' /data/3307/mysql
  3. [root@ylt001 3306]# tree /data
  4. /data
  5. |-- 3306
  6. | |-- data
  7. | |-- my.cnf
  8. | |-- mysql_ylt3306.err
  9. | `-- mysql
  10. `-- 3307
  11. |-- data
  12. |-- my.cnf
  13. |-- mysql_ylt3307.err
  14. `-- mysql
  15. 4 directories, 6 files

5. 配置 MySQL 多实例的文件权限

  1. [root@ylt001 3306]# chown -R mysql:mysql /data
  2. [root@ylt001 3306]# find /data -name mysql|xargs ls -l
  3. -rw-r--r-- 1 mysql mysql 1012 Sep 30 13:38 /data/3306/mysql
  4. -rw-r--r-- 1 mysql mysql 1012 Sep 30 13:38 /data/3307/mysql
  1. [root@ylt001 3306]# find /data -name mysql|xargs chmod 700
  1. [root@ylt001 3306]# find /data -name mysql -exec ls -l {} \;
  2. -rwx------ 1 mysql mysql 1012 Sep 30 13:38 /data/3307/mysql
  3. -rwx------ 1 mysql mysql 1012 Sep 30 13:38 /data/3306/mysql

6. MySQL 相关命令加入全局路径的配置

  1. [root@ylt001 3306]# ls /application/mysql/bin/mysql
  2. /application/mysql/bin/mysql
  1. [root@ylt001 3306]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
  2. [root@ylt001 3306]# tail -1 /etc/profile
  3. export PATH=/application/mysql/bin:$PATH
  4. [root@ylt001 3306]# source /etc/profile
  5. [root@ylt001 3306]# echo $PATH
  6. /application/mysql/bin:/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
  1. [root@ylt001 3306]# ln -s /application/mysql/bin/* /usr/local/sbin/

7. 初始化 MySQL 多实例的数据库文件

  1. [root@ylt001 3306]# cd /application/mysql/scripts/
  2. [root@ylt001 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
  3. Installing MySQL system tables...
  4. 180930 13:46:31 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
  5. 180930 13:46:31 [Note] /application/mysql/bin/mysqld (mysqld 5.5.61) starting as process 102410 ...
  6. OK
  7. Filling help tables...
  8. 180930 13:46:31 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
  9. 180930 13:46:31 [Note] /application/mysql/bin/mysqld (mysqld 5.5.61) starting as process 102417 ...
  10. OK
  11. ...
  12. [root@ylt001 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
  13. Installing MySQL system tables...
  14. 180930 13:47:48 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
  15. 180930 13:47:48 [Note] /application/mysql/bin/mysqld (mysqld 5.5.61) starting as process 102521 ...
  16. OK
  17. Filling help tables...
  18. 180930 13:47:48 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
  19. 180930 13:47:48 [Note] /application/mysql/bin/mysqld (mysqld 5.5.61) starting as process 102528 ...
  20. OK
  21. ...
  1. [root@ylt001 scripts]# tree /data
  2. /data
  3. |-- 3306
  4. | |-- data
  5. | | |-- mysql
  6. | | | |-- columns_priv.MYD
  7. | | | |-- columns_priv.MYI
  8. | | | |-- columns_priv.frm
  9. | | | |-- db.MYD
  10. | | | |-- db.MYI
  11. | | | |-- db.frm
  12. ...

8. 启动 MySQL 多实例的数据库

  1. [root@ylt001 3306]# /data/3306/mysql start
  2. Starting MySQL...
  3. [root@ylt001 3306]# /data/3307/mysql start
  4. Starting MySQL...
  5. [root@ylt001 3306]# netstat -lntup|grep 330
  6. tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 106726/mysqld
  7. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 107676/mysqld
  1. [root@ylt001 3306]# /data/3306/mysql stop
  2. Stopping MySQL...
  1. [root@ylt001 ~]# /data/3306/mysql restart
  2. Restarting MySQL...
  3. Stopping MySQL...
  4. Starting MySQL...

9. 配置 MySQL 多实例数据库开机自启动

  1. [root@ylt001 3306]# echo "#mysql multi instances" >> /etc/rc.local
  2. [root@ylt001 3306]# echo "/data/3306/mysql start" >> /etc/rc.local
  3. [root@ylt001 3306]# echo "/data/3307/mysql start" >> /etc/rc.local
  4. [root@ylt001 3306]# tail -3 /etc/rc.local
  5. #mysql multi instances
  6. /data/3306/mysql start
  7. /data/3307/mysql start

10. 登录 MySQL 测试

  1. [root@ylt001 ~]# mysql -S /data/3306/mysql.sock
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 2
  4. Server version: 5.5.61 MySQL Community Server (GPL)
  5. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> show databases;
  11. +--------------------+
  12. | Database |
  13. +--------------------+
  14. | information_schema |
  15. | mysql |
  16. | performance_schema |
  17. | test |
  18. +--------------------+
  19. 4 rows in set (0.00 sec)
  20. mysql> select user();
  21. +----------------+
  22. | user() |
  23. +----------------+
  24. | root@localhost |
  25. +----------------+
  26. 1 row in set (0.00 sec)
  27. mysql>

MySQL 安全配置

  1. [root@ylt001 ~]# mysqladmin -u root -S /data/3306/mysql.sock password 'password'
  2. # 为 mysql 设置密码
  3. [root@ylt001 ~]# mysql -uroot -p -S /data/3306/mysql.sock
  4. Enter password:
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 2
  7. Server version: 5.5.61 MySQL Community Server (GPL)
  8. ...
  9. mysql>
  1. [root@ylt001 3306]# find /data -type f -name "mysql" -exec chmod 700 {} \;
  2. [root@ylt001 3306]# find /data -type f -name "mysql" -exec chown root.root {} \;
  3. [root@ylt001 3306]# find /data -type f -name "mysql" -exec ls -l {} \;
  4. -rwx------ 1 root root 1003 Sep 30 14:20 /data/3307/mysql
  5. -rwx------ 1 root root 1003 Sep 30 14:21 /data/3306/mysql
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注