Mysql 通過 Mysql_install_db 建立多例項

Michael_DD發表於2015-09-09
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章