首先我们来学习一下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
|