Mysql 通过 Mysql_install_db 创建多实例
当MySQL的系统库(mysql系统库)发生故障或需要新加一个mysql实例时,需要初始化mysql数据库。
需要使用的命令:/mysql/bin/mysql_install_db
#/mysql/bin/mysql_install_db --help
Usage: /usr/local/mysql/bin/mysql_install_db [OPTIONS]
--basedir=path The path to the MySQL installation directory.
--cross-bootstrap For internal use. Used when building the MySQL system
tables on a different host than the target.
--datadir=path The path to the MySQL data directory.
--force Causes mysql_install_db to run even if DNS does not
work. In that case, grant table entries that normally
use hostnames will use IP addresses.
--ldata=path The path to the MySQL data directory.
--rpm For internal use. This option is used by RPM files
during the MySQL installation process.
--skip-name-resolve Use IP addresses rather than hostnames when creating
grant table entries. This option can be useful if
your DNS does not work.
--srcdir=path For internal use. The directory under which
mysql_install_db looks for support files such as the
error message file and the file for popoulating the
help tables.
--user=user_name The login username to use for running mysqld. Files
and directories created by mysqld will be owned by this
user. You must be root to use this option. By default
mysqld runs using your current login name and files and
directories that it creates will be owned by you.
All other options are passed to the mysqld program
除了支持以上的参数,还支持mysqld的参数。
举例:
本文以新加一个mysql实例为例。例如服务器上已经安装了3306端口的mysql服务,需要再启一个3308端口的mysql服务。
假设mysql安装在/mysql-5.6.22路径下,创建3308目录,把3308端口的mysql的数据保存在/3308/data下
#mkdir /mysql5.6.22/mysql_3308
#mkdir /mysql5.6.22/mysql_3308/data
#chown -R mysql:mysql /mysql5.6.22/mysql_3308
复制一个mysql配置文件my.cnf到/mysql5.6.22/mysql_3308目录下
#vi /mysql5.6.22/mysql_3308/my.cnf
修改配置文件,将端口和相关目录的都改为新的设置,如下:
[client]
character-set-server = utf8
port = 3308
socket = /mysql5.6.22/mysql_3308/tmp/mysql_3308.sock
[mysqld]
user = mysql
port = 3308
socket = /mysql5.6.22/mysql_3308/tmp/mysql_3308.sock
basedir = /mysql5.6.22/mysql_3308
datadir = /mysql5.6.22/mysql_3308/data
log-error = /mysql5.6.22/mysql_3308/mysql_error.log
pid-file = /mysql5.6.22/mysql_3308/mysql.pid
......其他略
确保配置文件无误。
运行下面命令进行数据库的初始化:
[root@mysqlsource mysql5.6.22]# ./scripts/mysql_install_db --defaults-file=/mysql5.6.22/mysql_3308/my.cnf --datadir=/mysql5.6.22/mysql_3308/data
FATAL ERROR: Neither host 'mysqlsource' nor 'localhost' could be looked up with
/mysql5.6.22/mysql_3308/bin/resolveip
Please configure the 'hostname' command to return a correct
hostname.
If you want to solve this at a later stage, restart this script
with the --force option
[root@mysqlsource mysql5.6.22]# ./scripts/mysql_install_db --defaults-file=/mysql5.6.22/mysql_3308/my.cnf --datadir=/mysql5.6.22/mysql_3308/data --force option
Installing MySQL system tables...2015-09-09 10:10:27 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
[root@mysqlsource mysql5.6.22]#
[root@mysqlsource mysql5.6.22]# ./scripts/mysql_install_db --defaults-file=/mysql5.6.22/mysql_3308/my.cnf --datadir=/mysql5.6.22/mysql_3308/data --force option --explicit_defaults_for_timestamp
Installing MySQL system tables...[root@mysqlsource mysql5.6.22]#
[root@mysqlsource mysql5.6.22]#
[root@mysqlsource mysql5.6.22]#
[root@mysqlsource mysql5.6.22]# ./scripts/mysql_install_db --defaults-file=/mysql5.6.22/mysql_3308/my.cnf --datadir=/mysql5.6.22/mysql_3308/data
Installing MySQL system tables...2015-09-09 10:33:00 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
OK
Filling help tables...2015-09-09 10:33:02 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
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:
/mysql5.6.22//bin/mysqladmin -u root password 'new-password'
/mysql5.6.22//bin/mysqladmin -u root -h mysqlsource password 'new-password'
Alternatively you can run:
/mysql5.6.22//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 . ; /mysql5.6.22//bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
WARNING: Found existing config file ./my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as ./my-new.cnf,
please compare it with your file and take the changes you need.
[root@mysqlsource mysql5.6.22]#
完成后新的3308数据库就初始化好了,如果有报错,则按照报错的提示查看报错日志,一般情况下都是my.cnf配置文件的问题,修正后即可。
启动新mysql
启动3308端口的mysql服务
[root@mysqlsource mysql5.6.22]# ./bin/mysqld_safe --defaults-file=/mysql5.6.22/mysql_3308/my.cnf --user=mysql &
检查是否启动
[root@mysqlsource mysql]# ps -ef | grep mysql
root 5672 498 0 10:21 pts/5 00:00:00 /bin/sh ./bin/mysqld_safe --defaults-file=/mysql5.6.22/etc/my.cnf
mysql 6144 5672 0 10:21 pts/5 00:00:02 /mysql5.6.22/sbin/mysqld --defaults-file=/mysql5.6.22/etc/my.cnf --basedir=/mysql5.6.22 --datadir=/mysql5.6.22/data/ --plugin-dir=/mysql5.6.22/lib64/mysql/plugin --user=mysql --log-error=/mysql5.6.22/mysql-error.log --pid-file=/mysql5.6.22/mysqld.pid --socket=/mysql5.6.22/tmp/mysql.sock --port=3306
root 8103 20753 0 10:32 pts/0 00:00:00 tail -f mysql_3308/mysql_error.log
root 8227 498 0 10:34 pts/5 00:00:00 /bin/sh ./bin/mysqld_safe --defaults-file=/mysql5.6.22/mysql_3308/my.cnf --user=mysql
mysql 8400 8227 4 10:34 pts/5 00:00:00 /mysql5.6.22/sbin/mysqld --defaults-file=/mysql5.6.22/mysql_3308/my.cnf --basedir=/mysql5.6.22/ --datadir=/mysql5.6.22/mysql_3308/data --plugin-dir=/mysql5.6.22/lib64/mysql/plugin --user=mysql --log-error=/mysql5.6.22/mysql_3308/mysql_error.log --pid-file=/mysql5.6.22/mysql_3308/mysql.pid --socket=/mysql5.6.22/mysql_3308/tmp/mysql_3308.sock --port=3308
root 8430 3735 0 10:34 pts/1 00:00:00 grep mysql
root 9348 1 0 Aug24 ? 00:17:00 /usr/bin/Xvnc :1 -desktop mysqlsource:1 (root) -auth /root/.Xauthority -geometry 1280x900 -rfbwait 30000 -rfbauth /root/.vnc/passwd -rfbport 5901 -fp catalogue:/etc/X11/fontpath.d -pn
root 24137 24079 0 Aug28 ? 00:00:00 [mysqld] <defunct>
root 24277 24263 0 Aug28 ? 00:00:00 [mysqld] <defunct>
[root@mysqlsource mysql]#
如果有3308字样说明已经启动成功
可将启动命令加入/etc/rc.local随服务器启动
新加的mysql没有设置root密码,可以通过下面命令设置root密码:
#/usr/local/mysql/bin/mysqladmin -S /mysql-5.6.22/mysql_3308/tmp/mysql_3308.sock -u root password 'new-password'
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29500582/viewspace-1795124/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29500582/viewspace-1795124/