《MySQL数据库多种安装方法及企业级安装实践(下篇)》要点:
本文介绍了MySQL数据库多种安装方法及企业级安装实践(下篇),希望对您有用。如果有疑问,可以联系我们。
接《MySQL数据库多种安装方法及企业级安装实践(上篇)》
上述配置完毕后,就可以初始化数据库文件了,这个步骤其实也可以在编译安装MySQL之后就操作,只不过放到这里更合理一些.
(1)初始化MySQL数据库
初始化数据库的核心命令为:
application/mysql/scripts/mysql_install_db –basedir=/application/mysql/ –datadir=/application/mysql/data –user=mysql
提示:–basedir=/application/mysql/为MySQL的安装路径,–datadir为数据文件目录.另,注意mysql_install_db和MySQL5.1的路径不同,MySQL5.1不在MySQL bin路径下了.
整个初始化的操作过程为:
[root@oldboy mysql-5.6.34]# /application/mysql/scripts/mysql_install_db –basedir=/application/mysql/ –datadir=/application/mysql/data –user=mysql
#<==初始化mysql数据库文件,会有很多信息提示,如果没有ERROR级别的错误,有两个OK的字样,表示初始化成功,否则就要解决初始化的问题.
Installing MySQL system tables…2017-02-26 18:08:53 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2017-02-26 18:08:53 0 [Note] Ignoring –secure-file-priv value as server is running with –bootstrap.
2017-02-26 18:08:53 0 [Note] /application/mysql//bin/mysqld (mysqld 5.6.34) starting as process 47993 …
2017-02-26 18:08:53 47993 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-02-26 18:08:53 47993 [Note] InnoDB: The InnoDB memory heap is disabled
2017-02-26 18:08:53 47993 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-02-26 18:08:53 47993 [Note] InnoDB: Memory barrier is not used
2017-02-26 18:08:53 47993 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-02-26 18:08:53 47993 [Note] InnoDB: Using Linux native AIO
2017-02-26 18:08:53 47993 [Note] InnoDB: Using CPU crc32 instructions
2017-02-26 18:08:53 47993 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-02-26 18:08:53 47993 [Note] InnoDB: Completed initialization of buffer pool
2017-02-26 18:08:53 47993 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2017-02-26 18:08:53 47993 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2017-02-26 18:08:53 47993 [Note] InnoDB: Database physically writes the file full: wait…
…省略若干…
2017-02-26 18:08:54 47993 [Note] InnoDB: Waiting for purge to start
2017-02-26 18:08:54 47993 [Note] InnoDB: 5.6.34 started; log sequence number 0
2017-02-26 18:08:54 47993 [Note] Binlog end
2017-02-26 18:08:54 47993 [Note] InnoDB: FTS optimize thread exiting.
2017-02-26 18:08:54 47993 [Note] InnoDB: Starting shutdown…
2017-02-26 18:08:55 47993 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK
#<==两个OK是初始化成功的标志.
Filling help tables…2017-02-26 18:08:55 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2017-02-26 18:08:55 0 [Note] Ignoring –secure-file-priv value as server is running with –bootstrap.
2017-02-26 18:08:55 0 [Note] /application/mysql//bin/mysqld (mysqld 5.6.34) starting as process 48015 …
2017-02-26 18:08:55 48015 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-02-26 18:08:55 48015 [Note] InnoDB: The InnoDB memory heap is disabled
…省略若干…
2017-02-26 18:08:56 48015 [Note] InnoDB: FTS optimize thread exiting.
2017-02-26 18:08:56 48015 [Note] InnoDB: Starting shutdown…
2017-02-26 18:08:57 48015 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK
#<==两个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:
/application/mysql//bin/mysqladmin -u root password ‘new-password’
/application/mysql//bin/mysqladmin -u root -h oldboy password ‘new-password’
Alternatively you can run:
/application/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 . ; /application/mysql//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
###请注意如下几行英文的说明
New default config file was created as /application/mysql//my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
#从上文说明中可以指导mysql的默认配置文件已经变到了/application/mysql//my.cnf
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
–defaults-file argument to mysqld_safe when starting the server
#从上文说明中可以看到数据库启动时会读取/etc/my.cnf,因此有可能会导致无法启动,避免的方法就是使用mysqld_safe 启动服务时采用–defaults-file参数指定配置文件,前文已将/etc/my.cnf改名了,所以,就不需要指定参数了,这里是一个坑,读者要注意.
此步骤必须要初始化成功,否则,后面会出现登录不了数据库等各种问题.
(2)初始化数据库的原理及结果说明
初始化数据库的实质就是创建基础的数据库系统的库文件,例如:生成MySQL库表等.
初始化数据库后,查看数据目录,可以看到多了如下文件:
[root@oldboy mysql-5.6.34]# ls -l /application/mysql/data
total 110604
-rw-rw—-. 1 mysql mysql 12582912 Feb 26 18:08 ibdata1
-rw-rw—-. 1 mysql mysql 50331648 Feb 26 18:08 ib_logfile0
-rw-rw—-. 1 mysql mysql 50331648 Feb 26 18:08 ib_logfile1
drwx——. 2 mysql mysql 4096 Feb 26 18:08 mysql #<==用于存放管理mysql的数据.
drwx——. 2 mysql mysql 4096 Feb 26 18:08 performance_schema #<==5.5及以上增加的内部性能库.
drwxr-xr-x. 2 mysql mysql 4096 Feb 26 17:48 test #<==用于测试的test数据库.
[root@oldboy mysql-5.6.34]# tree /application/mysql/data #<==如果没有tree,可以yum install tree –y安装
/application/mysql/data
├── ibdata1
├── ib_logfile0
├── ib_logfile1
├── mysql
│ ├── columns_priv.frm
│ ├── columns_priv.MYD
│ ├── columns_priv.MYI
…省略若干…
│ ├── general_log.CSV
│ ├── general_log.frm
│ ├── help_category.frm
│ ├── procs_priv.frm
│ ├── procs_priv.MYD
│ ├── procs_priv.MYI
│ ├── proxies_priv.frm
│ ├── proxies_priv.MYD
│ ├── proxies_priv.MYI
│ ├── servers.frm
…省略若干…
│ ├── time_zone_name.MYI
│ ├── time_zone_transition.frm
│ ├── time_zone_transition.MYD
│ ├── time_zone_transition.MYI
│ ├── time_zone_transition_type.frm
│ ├── time_zone_transition_type.MYD
│ ├── time_zone_transition_type.MYI
│ ├── user.frm
│ ├── user.MYD
│ └── user.MYI
├── performance_schema
│ ├── accounts.frm
│ ├── cond_instances.frm
│ ├── db.opt
│ ├── events_stages_current.frm
│ ├── events_stages_history.frm
│ ├── events_stages_history_long.frm
│ ├── events_stages_summary_by_account_by_event_name.frm
…省略若干…
│ ├── threads.frm
│ └── users.frm
└── test
└── db.opt
3 directories, 136 files
(3)MySQL初始化故障排错集锦
本节的所有故障必须要解除,否则,后面会出现登录不了MySQL数据库等各种问题,故障集锦见本章结尾内容.
1)设置MySQL启动脚本
[root@oldboy mysql-5.6.34]# pwd
/home/oldboy/tools/mysql-5.6.34
[root@oldboy mysql-5.6.34]# cp support-files/mysql.server /etc/init.d/mysqld #<==拷贝mysql启动脚本到mysql的命令路径.
[root@oldboy mysql-5.6.34]# chmod 700 /etc/init.d/mysqld #<==使脚本可执行.
[root@oldboy mysql-5.6.34]# ls -l /etc/init.d/mysqld
-rwx——. 1 root root 10929 Feb 26 18:26 /etc/init.d/mysqld
2)启动MySQL数据库
#<==这是启动数据库规范方法之一,还可以使用/application/mysql/bin/mysqld_safe –user=mysql &启动.这个命令结尾 “&”符号作用是,在后台执行mysql服务,这条命令执行完 还需要按下回车才能进入到命令行状态.
#<==注意,如果已执行上面/etc/init.d/mysqld start启动命令,还想尝试下面mysqld_safe的命令,请先执行/etc/init.d/mysqld stop结束mysql进程.
[root@oldboy mysql-5.6.34]# cd ~
[root@oldboy mysql-5.6.34]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
提示:禁止使用pkill、kill -9、killall -9等命令强制杀死数据库,这会引起数据库无法启动等故障发生.企业中曾发生过的血的教训案例请看http://oldboy.blog.51cto.com/2561410/1431161.
3)检查MySQL数据库是否启动.
[root@oldboy ~]# netstat -lntup|grep mysql
tcp 0 0 :::3306 :::* LISTEN 48065/mysqld
如发现3306端口没起来.请使用tail -100 /application/mysql/data/机器名.err检查日志报错进行调试.经常查看服务运行日志是个很好的习惯,也是高手的习惯,你要不要成为高手?
4)查看MySQL数据库启动结果日志.
[root@oldboy ~]# tail /application/mysql/data/oldboy.err
2017-02-26 17:38:36 48065 [Note] InnoDB: Waiting for purge to start
2017-02-26 17:38:36 48065 [Note] InnoDB: 5.6.34 started; log sequence number 1625987
2017-02-26 17:38:36 48065 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 4f94404a-fc74-11e6-8112-000c292ece3f.
2017-02-26 17:38:36 48065 [Note] Server hostname (bind-address): ‘*’; port: 3306
2017-02-26 17:38:36 48065 [Note] IPv6 is available.
2017-02-26 17:38:36 48065 [Note] – ‘::’ resolves to ‘::’;
2017-02-26 17:38:36 48065 [Note] Server socket created on IP: ‘::’.
2017-02-26 17:38:36 48065 [Note] Event Scheduler: Loaded 0 events
2017-02-26 17:38:36 48065 [Note] /application/mysql-5.6.34/bin/mysqld: ready for connections.
Version: ‘5.6.34’ socket: ‘/application/mysql-5.6.34/tmp/mysql.sock’ port: 3306 Source distribution
本例查看了错误日志的命令及错误日志中的内容,这里省略了大部分日志内容,只给了默认10行,如果有错误,一般会显示error字样.
5)设置MySQL开机自启动.
[root@oldboy ~]# chkconfig –add mysqld
[root@oldboy ~]# chkconfig –list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off此外,将启动命令/etc/init.d/mysqld start放到/etc/rc.local里面实现开机自启动也可.
若MySQL安装及使用出现故障,可根据下面的分析思路进行检查.
细看所有执行命令返回的屏幕输出,不要忽略关键的输出内容.
辅助查看系统日志/var/log/messages.
如果是MySQL关联了其他服务,要同时查看相关服务的日志.
仔细阅读,重新查看操作的步骤是否正确,书写的命令及字符是不是都对.
经常查看各种服务运行日志是个很好的习惯,也是成长为高手的必经之路,你要不要成为高手?
如果不为MySQL的命令配置全局路径,就无法直接在命令行输入mysql这样的命令,只能用全路径命令(/application/mysql/bin/mysql),这种带着路径输入命令的方式很麻烦.下面来看看配置的具体方法.
1)确认mysql命令所在的路径.
root@oldboy /]# ls /application/mysql/bin/mysql
/application/mysql/bin/mysql
2)在PATH变量前面增加/application/mysql/bin路径,并追加到/etc/profile文件中.
[root@oldboy /]# echo ‘export PATH=/application/mysql/bin:$PATH’ >>/etc/profile
#<==注意,echo后是单引号呦,双引号是不行滴.
[root@oldboy /]# tail -1 /etc/profile
export PATH=/application/mysql/bin:$PATH
[root@oldboy /]# source /etc/profile
#<==执行source使上一行添加到/etc/profile中,内容直接生效
#<==以上命令的用途为,定义mysql全局路径,实现在任意路径执行mysql命令.
[root@oldboy ~]# echo $PATH
/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
#<==执行echo $PATH有/application/mysql/bin输出表示配置成功.
提示:更简单的设置方法为用下面命令做软链接:ln -s /application/mysql/bin/* /usr/local/sbin/,把mysql命令所在路径链接到全局路径/usr/local/sbin/的下面.
要特别强调的是,务必把MySQL命令路径放在PATH路径中其他路径的前面,否则,可能会导致使用的mysql等命令和编译安装的mysql命令不是同一个,进而产生错误.下面的网址中给出了因为MySQL路径配置问题导致的错误案例:http://oldboy.blog.51cto.com/2561410/1122867,该错误实际上就是因为使用yum安装的MySQL客户端命令访问了编译安装的服务端而导致的.
登录并测试的命令如下:
[root@oldboy ~]# mysql #<==直接敲mysql就进入数据库了,而且身份还是root.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.34 Source distribution
Copyright (c) 2000, 2016, 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>
提示:你还可以使用如下三种命令写法登录mysql:
mysql -uroot -p,
mysql -uroot,
mysql -uroot -p ‘密码’
若出现登录故障,可通过以下方法排查.
如果这里提示无法登录,排除了数据库启动问题后,则很可能是数据库初始化文件有问题.例如:
[root@oldboy ~]# mysql
ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)
解决办法:重新初始化数据库即可,此问题一般都是数据库初始化问题,或者数据库文件损坏,以及目录权限问题.
mysql> show databases; #<==查看当前的数据库
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
+——————–+
4 rows in set (0.00 sec)
mysql> select user(); #<==查看当前的登录用户
+—————-+
| user() |
+—————-+
| root@localhost |
+—————-+
1 row in set (0.00 sec)
mysql> #<==快捷键ctrl+d,也可以使用quit或exit等.
MySQL安装完成后,默认情况下,管理员账号root是无密码的,极不安全,必须要处理一下.
1. 为root用户设置密码
MySQL管理员的账号root密码默认为空,极不安全,可以通过mysqladmin命令为mysql不同实例的数据库设置独立的密码.
[root@oldboy ~]# mysqladmin -u root password ‘oldboy123’ #<==为root用户设置密码oldboy123.
Warning: Using a password on the command line interface can be insecure. #<==这里是一个警告,提醒用户命令行放置密码是不安全的,读者尽量不要在命令行输入密码,而是采取交互式的输入密码.
[root@oldboy ~]# mysql #<==无法直接输入命令登录了.
ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)
[root@oldboy ~]# mysql -uroot -p #<==新的登录方式,也可以直接带密码mysql -uroot -poldboy123.
Enter password: #<==输入新密码oldboy123,交互式输入密码不会记录在命令记录里,因此更安全.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.34 Source distribution
Copyright (c) 2000, 2016, 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>
读者也可以执行mysql_secure_installation命令交互式地设置系统的用户密码.
2. 清理mysql服务器内无用的用户
select user,host from mysql.user;
+——+———–+
| user | host |
+——+———–+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | oldboy |
| root | oldboy |
+——+———–+
6 rows in set (0.00 sec)
mysql> drop user root@’::1′;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user root@’oldboy’;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ”@’oldboy’;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ”@’localhost’;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+——+———–+
| user | host |
+——+———–+
| root | 127.0.0.1 |
| root | localhost |
+——+———–+
2 rows in set (0.00 sec)
提示:对于drop命令及数据库安全,后文会有详细讲解, 此处不执行也可以.
有时用drop命令可能无法删除对应用户.比如,当数据库内的host等字段为大写及特殊Linux主机名时,删除用户就会遇到问题,例如:
mysql> drop user ‘ ‘@’MySQL’;
ERROR 1396 (HY000): Operation DROP USER failed for ‘ ‘@’mysql’
可使用DML语句,并采用delete命令删除来解决此问题,具体命令如下:
mysql> delete from mysql.user where user=” and host=’MySQL’;
Query OK, 1 row affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3. 删除mysql数据库内无用的test库
mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
+——————–+
3 rows in set (0.00 sec)
有关更多的MySQL的安全措施,在后文会有更详细的讲解.
出现的错误如下:
checking for tgetent in -ltinfo… no
checking for termcap functions library… configure: error: No curses/termcap library found
原因:缺少ncurses安装包.
解决方法:使用yum -y install ncurses-devel命令.
警告是可以忽略的,如果非要解决则需修改对主机名的解析,使其和uname -n命令的结果一样,如下:
[root@oldboy ~]# grep “`uname -n`” /etc/
hosts127.0.0.1 oldboy
故障2:错误提示ERROR: 1004 Can’t create file ‘/tmp/#sql300e_1_0.frm’ (errno: 13)
在执行初始化数据库命令时可能就会遇到这样的错误,错误提示如下:
ERROR: 1004 Can’t create file ‘/tmp/#sql300e_1_0.frm’ (errno: 13)
120406 15:47:02 [ERROR] Aborting
120406 15:47:02 [Note] /application/mysql/libexec/mysqld: Shutdown complete
Installation of system tables failed! Examine the logs in
/application/mysql/data for more information.
根据提示可知,/tmp目录不能创建文件,所以解决办法为给/tmp目录增加权限,如下:
解决办法:还原/tmp目录权限,操作命令如下:
[root@oldboy ~]# chmod 1777 /tmp #<==1777是/tmp的默认权限,除非曾经改动过此目录权限才会报错.
[root@oldboy ~]# ls -ld /tmp/
drwsrwxrwt. 8 root root 4096 3月 10 18:07 /tmp/
本示例的故障必须要解除,否则,后面会出现登录不了MySQL数据库等各种问题.
解答:正文里已给出答案,不再累述.
解答:正文里已给出答案,不再累述.
编译参数 | 说明 |
-DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.34 | 设定mysql安装目录 |
-DMYSQL_DATADIR=/application/mysql-5.6.34/data | 设定mysql数据文件目录 |
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.34/tmp/mysql.sock | 设定mysql.sock路径 |
-DDEFAULT_CHARSET=utf8 | 设定默认的字符集为utf8 |
-DDEFAULT_COLLATION=utf8_general_ci | 设定默认排序规则 |
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii | 启用额外的字符集类型 |
-DENABLED_LOCAL_INFILE=ON | 启用本地数据导入支持 |
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 |
若想启用某个引擎的支持,-DWITH_<ENGINE>_STORAGE_ENGINE=1
若想禁用某个引擎的支持:-DWITHOUT_<ENGINE>_STORAGE_ENGINE=0 |
-DWITH_FAST_MUTEXES=1 | |
-DWITH_ZLIB=bundled | 启用libz库支持 |
-DENABLED_LOCAL_INFILE=1 | 这个参数重复了,启用本地数据导入支持 |
-DWITH_READLINE=1 | 启用readline库支持(提供可编辑的命令行) |
-DWITH_EMBEDDED_SERVER=1 | 编译嵌入式服务器支持 |
-DWITH_DEBUG=0 | 禁用debug(默认为禁用)
|
更多内容可以参考官方MySQL 5.6的cmake编译参数http://dev.mysql.com/doc/refman/5.6/en/source-configuration-options.html
文章来自微信公众号:老男孩教育
转载请注明本页网址:
http://www.vephp.com/jiaocheng/4237.html