MySQL数据库多种安装方法及企业级安装实践(下篇)(3)
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关联了其他服务,要同时查看相关服务的日志. 仔细阅读,重新查看操作的步骤是否正确,书写的命令及字符是不是都对. 经常查看各种服务运行日志是个很好的习惯,也是成长为高手的必经之路,你要不要成为高手? 3.2.5?将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客户端命令访问了编译安装的服务端而导致的. 3.2.6登录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是无密码的,极不安全,必须要处理一下. 3.2.7基本的MySQL安全配置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,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的安全措施,在后文会有更详细的讲解. 3.3 ?MySQL安装FAQ问题1:在配置mysql时遇到错误.出现的错误如下: checking?for?tgetent?in?-ltinfo…?no 原因:缺少ncurses安装包. (编辑:ASP站长网) |