Linux下用mysql_install_db创建数据库实例

有时候我们需要在一个服务器上安装两个mysql,一个应对GBK编码数据,一个应对UTF-8编码数据。此文内容就是应对这种情况。( 采用此命令带参数来创建数据库实例,可以减少一些不必要的过程。网络上的一些文章都用默认配置创建数据库实例,以致需要多次重启数据库。)

  首先我们来学习一下mysql_install_db的帮助信息

通过命令$>mysql_install_db --help 打印如下信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

 

下面以创建一个UTF-8编码的mysql数据库为例子来说名这个命令的一些使用:

  首先我们可以通过执行“ cp /etc/my.conf  /etc/my_utf8.conf ”复制一份配置,然后修改配置,确保端口(port),数据路径(datadir),套接字(socket),错误日志路径(log-error),pid-file原来的配置不冲突。下面的是此次修改后配置文件,并增加了一些定制配置以适应应用要求。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[mysqld]
datadir=/var/lib/mysql_utf8
socket=/var/lib/mysql/mysql_utf8.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
skip -locking
max_allowed_packet = 5M
net_buffer_length = 8K
port=3307
 
default -character -set =utf8
lower_case_table_names=1
 
max_connections=1000
query_cache_size=100M
table_cache=256
tmp_table_size=50M
thread_cache_size=8
myisam_max_sort_file_size=100G
myisam_max_extra_sort_file_size=100G
myisam_sort_buffer_size=50M
key_buffer_size=50M
read_buffer_size=1024K
read_rnd_buffer_size=4096K
sort_buffer_size=4096K
innodb_additional_mem_pool_size=10M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=5M
innodb_buffer_pool_size=100M
innodb_log_file_size=10M
innodb_thread_concurrency=8
 
 
[mysqld_safe]
log -error =/var/log/mysql_utf8.log
pid -file =/var/run/mysqld/mysql_utf8.pid

  然后执行"#>mysql_install_db --defaults-file=/etc/my_utf8.cnf --datadir=/var/lib/mysql_utf8" 命令来创建数据库。

?
1
#>mysql_install_db --defaults-file=/etc/my_utf8.cnf --datadir=/var/lib/mysql_utf8

其中我们用“--defaults-file”指定了创建数据库用到的配置文件;“--datadir”指定创建数据的路径,这里的路径保持与配置文件中的datadir保持一致。

  观察输出的内容,看看是否有错误信息,如果有的话,观察日志调整配置解决,一般情况都是my_utf8.conf文件的问题。

  然后执行一下命令来启动数据库:

?
1
#>mysqld_safe --defaults-file=/etc/my_utf8.cnf &

  通过执行下面命令来查询数据是否启动,如果显示有“mysql_utf8”字样的内容就说明数据库启动成功了。

?
1
#>ps aux | grep mysql

  如果需要数据库随服务启动而启动,将前面说的的启动命令加入“/etc/rc.local”就可以了。

  执行下面命令修改数据库root密码:

?
1
#>mysqladmin -uroot password 'newpwd' --socket=/var/lib/mysql/mysql_utf8.sock

  执行下面命令连接数据库:

?
1
#>mysql -uroot -p --socket=/var/lib/mysql/mysql_utf8.sock

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值