MySQL菜鸟,接触不久,如有错误之处,请指正,谢谢。
mysql创建多个实例,其实就是把DATA文件,SOCK,PORT指向不同的文件和端口。
mysql的多实例有两种方式可以实现,两种方式各有利弊。
第一种是使用多个配置文件启动不同的进程来实现多实例,这种方式的优势逻辑简单,配置简单,缺点是管理起来不太方便。
第二种是通过官方自带的mysqld_multi使用单独的配置文件来实现多实例,这种方式定制每个实例的配置不太方面,优点是管理起来很方便,集中管理。
本实验多实例采用mysqld_multi管理。
MySQL多实例环境搭建过程。
1.下载编译后的MySQL程序文件,解压缩。MySQL软件安装路径为/usr/local/mysql。
[root@lvs01 tmp]# [root@lvs01 tmp]# ll total 182656 -rw-r--r--. 1 root root 41700 Sep 2 10:12 ipvsadm-1.26.tar.gz -rw-r--r--. 1 root root 241437 Sep 2 10:12 keepalived-1.1.19.tar.gz -rw-r--r--. 1 root root 186722932 Sep 2 10:50 mysql-5.5.32-linux2.6-x86_64.tar.gz -rw-r--r--. 1 root root 21972 Sep 5 20:08 popt-static-1.13-7.el6.x86_64.rpm -rw-------. 1 root root 0 Sep 6 18:30 yum.log [root@lvs01 tmp]# [root@lvs01 tmp]# [root@lvs01 tmp]# [root@lvs01 tmp]# tar zxf mysql-5.5.32-linux2.6-x86_64.tar.gz [root@lvs01 tmp]# ll total 182660 -rw-r--r--. 1 root root 41700 Sep 2 10:12 ipvsadm-1.26.tar.gz -rw-r--r--. 1 root root 241437 Sep 2 10:12 keepalived-1.1.19.tar.gz drwxr-xr-x. 13 root root 4096 Sep 23 09:36 mysql-5.5.32-linux2.6-x86_64 -rw-r--r--. 1 root root 186722932 Sep 2 10:50 mysql-5.5.32-linux2.6-x86_64.tar.gz -rw-r--r--. 1 root root 21972 Sep 5 20:08 popt-static-1.13-7.el6.x86_64.rpm -rw-------. 1 root root 0 Sep 6 18:30 yum.log [root@lvs01 tmp]# [root@lvs01 tmp]# [root@lvs01 tmp]# [root@lvs01 tmp]# mv mysql-5.5.32-linux2.6-x86_64 /usr/local/mysql [root@lvs01 tmp]# cd /usr/local/mysql/ [root@lvs01 mysql]# ll total 76 drwxr-xr-x. 2 root root 4096 Sep 23 09:36 bin -rw-r--r--. 1 7161 wheel 17987 Jun 19 2013 COPYING drwxr-xr-x. 3 root root 4096 Sep 23 09:36 data drwxr-xr-x. 2 root root 4096 Sep 23 09:36 docs drwxr-xr-x. 3 root root 4096 Sep 23 09:36 include -rw-r--r--. 1 7161 wheel 7470 Jun 19 2013 INSTALL-BINARY drwxr-xr-x. 3 root root 4096 Sep 23 09:36 lib drwxr-xr-x. 4 root root 4096 Sep 23 09:36 man drwxr-xr-x. 10 root root 4096 Sep 23 09:36 mysql-test -rw-r--r--. 1 7161 wheel 2496 Jun 19 2013 README drwxr-xr-x. 2 root root 4096 Sep 23 09:36 scripts drwxr-xr-x. 27 root root 4096 Sep 23 09:36 share drwxr-xr-x. 4 root root 4096 Sep 23 09:36 sql-bench drwxr-xr-x. 3 root root 4096 Sep 23 09:36 support-files [root@lvs01 mysql]# [root@lvs01 mysql]# [root@lvs01 mysql]# [root@lvs01 mysql]# [root@lvs01 mysql]# [root@lvs01 mysql]# cd data [root@lvs01 data]# ls test [root@lvs01 data]# cd test/ [root@lvs01 test]# ls db.opt [root@lvs01 test]# [root@lvs01 test]#
2。添加mysql用户,初始化三个MySQL数据库。数据文件放在不同路径。
初始化完成之后,数据目录下面会有三个数据库。
[root@lvs01 test]# [root@lvs01 test]# [root@lvs01 test]# cd [root@lvs01 ~]# useradd mysql -s /sbin/nologin -M [root@lvs01 ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/dbdata_3306 --user=mysql WARNING: The host 'lvs01' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK Filling help tables... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/local/mysql/bin/mysqladmin -u root password 'new-password' /usr/local/mysql/bin/mysqladmin -u root -h lvs01 password 'new-password' Alternatively you can run: /usr/local/mysql/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/local/mysql/scripts/mysqlbug script! [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/dbdata_3307 --user=mysql WARNING: The host 'lvs01' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK Filling help tables... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/local/mysql/bin/mysqladmin -u root password 'new-password' /usr/local/mysql/bin/mysqladmin -u root -h lvs01 password 'new-password' Alternatively you can run: /usr/local/mysql/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/local/mysql/scripts/mysqlbug script! [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/dbdata_3308 --user=mysql WARNING: The host 'lvs01' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK Filling help tables... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/local/mysql/bin/mysqladmin -u root password 'new-password' /usr/local/mysql/bin/mysqladmin -u root -h lvs01 password 'new-password' Alternatively you can run: /usr/local/mysql/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/local/mysql/scripts/mysqlbug script! [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# [root@lvs01 ~]# cd /data/ [root@lvs01 data]# ll total 12 drwx------. 5 mysql root 4096 Sep 23 09:40 dbdata_3306 drwx------. 5 mysql root 4096 Sep 23 09:41 dbdata_3307 drwx------. 5 mysql root 4096 Sep 23 09:41 dbdata_3308 [root@lvs01 data]# cd dbdata_3306/ [root@lvs01 dbdata_3306]# ll total 12 drwx------. 2 mysql root 4096 Sep 23 09:40 mysql drwx------. 2 mysql mysql 4096 Sep 23 09:40 performance_schema drwx------. 2 mysql root 4096 Sep 23 09:40 test [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]#
3。编辑MySQL配置文件my.cnf。mysqld_multi命令启动三个MySQL实例。
注:三个实例,数据目录、socket文件、端口号各不相同。
mysqld_multi命令需要配置好环境变量。
export PATH=/usr/local/mysql/bin:$PATH
[root@lvs01 etc]# cat my.cnf [mysqld_multi] mysqld = /usr/local/mysql/bin/mysqld_safe mysqladmin = /usr/local/mysql/bin/mysqladmin user = admin password = password [mysqld1] socket = /data/dbdata_3306/mysql.sock port = 3306 #pid-file = /data/dbdata_3306/3306.pid datadir = /data/dbdata_3306 user = mysql [mysqld2] socket = /data/dbdata_3307/mysql.sock port = 3307 pid-file = /data/dbdata_3307/3307.pid datadir = /data/dbdata_3307 user = mysql [mysqld3] socket = /data/dbdata_3308/mysql.sock port = 3308 #pid-file = /data/dbdata_3308/3308.pid datadir = /data/dbdata_3308 user = mysql [root@lvs01 etc]# [root@lvs01 etc]# [root@lvs01 etc]# [root@lvs01 etc]# [root@lvs01 etc]# [root@lvs01 etc]# ps -ef | grep mysql root 3422 1457 0 10:35 pts/0 00:00:00 grep mysql [root@lvs01 etc]# [root@lvs01 etc]# [root@lvs01 etc]# [root@lvs01 etc]# [root@lvs01 etc]# /usr/local/mysql/bin/mysqld_multi start 1 WARNING: my_print_defaults command not found. Please make sure you have this command available and in your path. The command is available from the latest MySQL distribution. ABORT: Can't find command 'my_print_defaults'. This command is available from the latest MySQL distribution. Please make sure you have the command in your PATH. [root@lvs01 etc]# [root@lvs01 etc]# export PATH=/usr/local/mysql/bin:$PATH [root@lvs01 etc]# cd /data/dbdata_3306 [root@lvs01 dbdata_3306]# ll total 12 drwx------. 2 mysql root 4096 Sep 23 09:40 mysql drwx------. 2 mysql mysql 4096 Sep 23 09:40 performance_schema drwx------. 2 mysql root 4096 Sep 23 09:40 test [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysqld_multi start 1 [root@lvs01 dbdata_3306]# ps -ef | grep mysql root 3435 1 0 10:36 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --socket=/data/dbdata_3306/mysql.sock --port=3306 --datadir=/data/dbdata_3306 --user=mysql mysql 3557 3435 1 10:36 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/dbdata_3306 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/dbdata_3306/lvs01.err --pid-file=/data/dbdata_3306/lvs01.pid --socket=/data/dbdata_3306/mysql.sock --port=3306 root 3575 1457 0 10:36 pts/0 00:00:00 grep mysql [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# ll total 28692 -rw-rw----. 1 mysql mysql 18874368 Sep 23 10:36 ibdata1 -rw-rw----. 1 mysql mysql 5242880 Sep 23 10:36 ib_logfile0 -rw-rw----. 1 mysql mysql 5242880 Sep 23 10:36 ib_logfile1 -rw-r-----. 1 mysql root 1902 Sep 23 10:36 lvs01.err -rw-rw----. 1 mysql mysql 5 Sep 23 10:36 lvs01.pid drwx------. 2 mysql root 4096 Sep 23 09:40 mysql srwxrwxrwx. 1 mysql mysql 0 Sep 23 10:36 mysql.sock drwx------. 2 mysql mysql 4096 Sep 23 09:40 performance_schema drwx------. 2 mysql root 4096 Sep 23 09:40 test [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysqld_multi start 2 [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysqld_multi start 3 [root@lvs01 dbdata_3306]# ps -ef | grep mysql root 3435 1 0 10:36 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --socket=/data/dbdata_3306/mysql.sock --port=3306 --datadir=/data/dbdata_3306 --user=mysql mysql 3557 3435 0 10:36 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/dbdata_3306 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/dbdata_3306/lvs01.err --pid-file=/data/dbdata_3306/lvs01.pid --socket=/data/dbdata_3306/mysql.sock --port=3306 root 3587 1 0 10:38 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --socket=/data/dbdata_3307/mysql.sock --port=3307 --pid-file=/data/dbdata_3307/3307.pid --datadir=/data/dbdata_3307 --user=mysql mysql 3720 3587 0 10:38 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/dbdata_3307 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/dbdata_3307/lvs01.err --pid-file=/data/dbdata_3307/3307.pid --socket=/data/dbdata_3307/mysql.sock --port=3307 root 3745 1 0 10:38 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --socket=/data/dbdata_3308/mysql.sock --port=3308 --datadir=/data/dbdata_3308 --user=mysql mysql 3867 3745 1 10:38 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/dbdata_3308 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/dbdata_3308/lvs01.err --pid-file=/data/dbdata_3308/lvs01.pid --socket=/data/dbdata_3308/mysql.sock --port=3308 root 3885 1457 0 10:38 pts/0 00:00:00 grep mysql [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# netstat -ntlp | grep mysql tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 3720/mysqld tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 3867/mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3557/mysqld [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld1 is running MySQL server from group: mysqld2 is running MySQL server from group: mysqld3 is running [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]#
4。在数据库中设置mysqld_multi相关用户和密码,赋予权限。mysqld_multi命令关闭实例。
如果没有创建用户并赋予权限,命令执行失败。error日志暂时不知道什么意思。
[root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysqld_multi stop 2 [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld1 is running MySQL server from group: mysqld2 is running MySQL server from group: mysqld3 is running [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysqladmin -uroot password '3307' -S /data/dbdata_3307/mysql.sock [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysqld_safe stop 2 150923 10:43:09 mysqld_safe Logging to '/usr/local/mysql/data/lvs01.err'. 150923 10:43:09 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data 150923 10:43:09 mysqld_safe mysqld from pid file /usr/local/mysql/data/lvs01.pid ended [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# cat /usr/local/mysql/data/lvs01.err 150923 10:43:09 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data 150923 10:43:09 [Note] Plugin 'FEDERATED' is disabled. /usr/local/mysql/bin/mysqld: Table 'mysql.plugin' doesn't exist 150923 10:43:09 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it. 150923 10:43:09 InnoDB: The InnoDB memory heap is disabled 150923 10:43:09 InnoDB: Mutexes and rw_locks use GCC atomic builtins 150923 10:43:09 InnoDB: Compressed tables use zlib 1.2.3 150923 10:43:09 InnoDB: Using Linux native AIO 150923 10:43:09 InnoDB: Initializing buffer pool, size = 128.0M 150923 10:43:09 InnoDB: Completed initialization of buffer pool 150923 10:43:09 InnoDB: Operating system error number 13 in a file operation. InnoDB: The error means mysqld does not have the access rights to InnoDB: the directory. InnoDB: File name ./ibdata1 InnoDB: File operation call: 'create'. InnoDB: Cannot continue operation. 150923 10:43:09 mysqld_safe mysqld from pid file /usr/local/mysql/data/lvs01.pid ended [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld1 is running MySQL server from group: mysqld2 is running MySQL server from group: mysqld3 is running [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# cat /etc/my.cnf [mysqld_multi] mysqld = /usr/local/mysql/bin/mysqld_safe mysqladmin = /usr/local/mysql/bin/mysqladmin user = admin password = password [mysqld1] socket = /data/dbdata_3306/mysql.sock port = 3306 #pid-file = /data/dbdata_3306/3306.pid datadir = /data/dbdata_3306 user = mysql [mysqld2] socket = /data/dbdata_3307/mysql.sock port = 3307 pid-file = /data/dbdata_3307/3307.pid datadir = /data/dbdata_3307 user = mysql [mysqld3] socket = /data/dbdata_3308/mysql.sock port = 3308 #pid-file = /data/dbdata_3308/3308.pid datadir = /data/dbdata_3308 user = mysql [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysql -uroot -p3307 -S /data/dbdata_3307/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.5.32 MySQL Community Server (GPL) Copyright (c) 2000, 2013, 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> GRANT SHUTDOWN ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysqld_multi stop 2 [root@lvs01 dbdata_3306]# /usr/local/mysql/bin/mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld1 is running MySQL server from group: mysqld2 is not running MySQL server from group: mysqld3 is running [root@lvs01 dbdata_3306]#