• 安装MySQL-server、MySQl-client软件包
• 修改数据库用户root的密码
• 确认MySQL服务程序运行、root可控
本课程将使用64位的RHEL 7操作系统,MySQL数据库的版本是5.7.17。
访问http://dev.mysql.com/downloads/mysql/,找到MySQL Community Server下载页面,平台选择“Red Hat Enterprise Linux 7/ Oracle Linux”,然后选择64位的bundle整合包下载
注意:下载MySQL软件时需要以Oracle网站账户登录,如果没有请根据页面提示先注册一个(免费)
步骤一:准备工作
1)停止mariadb服务
1. [root@localhost ~]# systemctl stop mariadb
2)删除/etc/my.cnf配置文件
此配置文件由RHEL自带的mariadb-libs库提供:
[root@localhost ~]# rm -rf /etc/my.cnf
3)删除数据
1. [root@localhost ~]# rm -rf /var/lib/mysql/*
4)卸载软件包(没有会显示未安装软件包)
1. [root@localhost ~]# rpm -e --nodeps mariadb-server mariadb2. 警告:/var/log/mariadb/mariadb.log 已另存为/var/log/mariadb/mariadb.log.rpmsave
步骤二:安装软件包
1)安装mysql时可能会缺少某些依赖包,需提前单独安装
1. [root@localhost ~]# yum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes
2)物理机传输解压包给虚拟机192.168.4.1
1. [root@room9pc01 ~]# cd 桌面2. [root@room9pc01 桌面]# scp mysql-5.7.17.tar 192.168.4.1:/root/ //给虚拟机传包3. root@192.168.4.1\'s password:4. mysql-5.7.17.tar 100% 543MB 95.6MB/s 00:05
3)虚拟机192.168.4.1解压mysql-5.7.17.tar 整合包
1. [root@localhost ~]# tar -xvf mysql-5.7.17.tar //解压mysql整合包2. ./mysql-community-client-5.7.17-1.el7.x86_64.rpm3. ./mysql-community-common-5.7.17-1.el7.x86_64.rpm4. ./mysql-community-devel-5.7.17-1.el7.x86_64.rpm5. ./mysql-community-embedded-5.7.17-1.el7.x86_64.rpm6. ./mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm7. ./mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm8. ./mysql-community-libs-5.7.17-1.el7.x86_64.rpm9. ./mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm10. ./mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm11. ./mysql-community-server-5.7.17-1.el7.x86_64.rpm12. ./mysql-community-test-5.7.17-1.el7.x86_64.rpm
步骤三:启动MySQL数据库服务并设置开机自启
1. [root@localhost ~]# systemctl start mysqld //启动mysql服务2. [root@localhost ~]# systemctl enable mysqld //设置开机自启3. [root@localhost ~]# systemctl status mysqld //查看mysql服务状态4. ● mysqld.service - MySQL Server5. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)6. Active: active (running) since 二 2018-08-28 10:03:24 CST; 8min ago7. Docs: man:mysqld(8)8. http://dev.mysql.com/doc/refman/en/using-systemd.html9. Main PID: 4284 (mysqld)10. CGroup: /system.slice/mysqld.service11. └─4284 /usr/sbin/mysqld --daemonize --pid-file=/var/r...12.13. 8月 28 10:02:56 localhost.localdomain systemd[1]: Starting MySQ...14. 8月 28 10:03:24 localhost.localdomain systemd[1]: Started MySQL...15. Hint: Some lines were ellipsized, use -l to show in full.
步骤四:连接MySQL服务器,修改密码
查看随机生成的root管理密码
1. [root@localhost ~]#grep \'temporary password\' /var/log/mysqld.log2. 2017-04-01T18:10:42.948679Z 1 [Note] A temporary password is generated for root@localhost: mtoa>Av<p6Yk //随机生成的管理密码为mtoa>Av<p6Yk
2)使用客户端命令mysql连接到MySQL服务器
提示验证时,填入前一步获得的随机密码,验证成功后即可进入“mysql> ”环境:
1. [root@localhost ~]# mysql -u root -p\'mtoa>Av<p6Yk\'2. mysql: [Warning] Using a password on the command line interface can be insecure.3. Welcome to the MySQL monitor. Commands end with ; or \\g.4. Your MySQL connection id is 115. Server version: 5.7.176.7. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.8.9. Oracle is a registered trademark of Oracle Corporation and/or its10. affiliates. Other names may be trademarks of their respective11. owners.12.13. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.14. mysql> //登录成功后,进入SQL操作环境
用该密码登录到服务端后,必须马上修改密码,不然会报如下错误:
1. mysql> show databases;2. ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
3)执行SET PASSWORD命令修改密码
这个其实与validate_password_policy的值有关,默认为1,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。如果我们不希望密码设置的那么复杂,需要修改两个全局参数:validate_password_policy与validate_password_length。validate_password_length默认值为8,最小值为4,如果你显性指定validate_password_length的值小于4,尽管不会报错,但validate_password_length的值将设为4。
可参考下列指令:
1. mysql>set global validate_password_policy=0; //只验证长度2. Query OK, 0 rows affected (0.00 sec)3. mysql>set global validate_password_length=6; //修改密码长度,默认值是8个字符4. Query OK, 0 rows affected (0.00 sec)5. mysql> alter user user() identified by \"123456\"; //修改登陆密码6. Query OK, 0 rows affected (0.00 sec)
上述操作的结果是——更改数据库用户root从本机访问时的密码,设为123456。
退出“mysql> ”环境,重新登录验证,必须采用新的密码才能登入:
1. mysql> exit //退出 mysql> 环境2. Bye3. [root@localhost ~]# mysql -u root -p //重新登录4. Enter password: //输入新设置的密码5. Welcome to the MySQL monitor. Commands end with ; or \\g.6. Your MySQL connection id is 157. Server version: 5.7.17 MySQL Community Server (GPL)8.9. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.10.11. Oracle is a registered trademark of Oracle Corporation and/or its12. affiliates. Other names may be trademarks of their respective13. owners.14.15. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.