mysql安装
下载并安装MySQL官方的 Yum Repository
在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。
下载并安装MySQL官方的 Yum Repository
[root@zheng ~]# yum install wget -y
[root@zheng ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@zheng ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
安装mysql服务
[root@zheng ~]# yum install -y mysql-conmmunity-server
可以看到mariadb数据库被替换掉了,还有client客户端和common作为依赖被安装了
启动mysql服务
[root@zheng ~]# systemctl start mysqld
[root@zheng ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2020-07-02 00:40:54 EDT; 6s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3226 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 3177 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3230 (mysqld)
CGroup: /system.slice/mysqld.service
└─3230 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld...
Jul 02 00:40:50 zheng systemd[1]: Starting MySQL Server...
Jul 02 00:40:54 zheng systemd[1]: Started MySQL Server.
这里注意下,mysql5.6的版本是可以直接空密码登录,5.7就需要密码登录了!
可以用查日志的方式查到密码, =fI)hBp*0+f) 这个就是密码
[root@zheng ~]# grep "password" /var/log/mysqld.log
2020-07-02T04:40:51.555632Z 1 [Note] A temporary password is generated for root@localhost: =fI)hBp*0+f)
我们不用这个,直接改配置文件,把他设置成跳过密码验证
[root@zheng ~]# vi /etc/my.cnf
在【mysqld】文本段里任意行数添加 skip-grant-tables 然后保存配置文件退出。
重启mysql
[root@zheng ~]# systemctl restart mysqld
进入mysql
[root@zheng ~]# mysql -uroot -p
Enter password: ***##这里直接回车就可以了***
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30
Copyright (c) 2000, 2020, 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> show databases; ## 查询数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql; ## 进入mysql数据库
Database changed
mysql> update user set authentication_string=password("123") where user='root';
**##(“123”)这个是修改的密码, user=‘root’ 是用户**
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
更改报错的话按下面步骤来一下
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> SET PASSWORD FOR root@localhost = '123456';
Query OK, 0 rows affected (0.00 sec)
修改完数据库密码以后修改配置文件
[root@zheng ~]# vi /etc/my.cnf
把之前添加的这一行跳过密码验证删除掉
重启mysql以后重新进入数据库
[root@zheng ~]# systemctl restart mysqld
[root@zheng ~]# mysql -uroot -p
Enter password: ## 可以看到这里直接回车报错了
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
接下来用修改的密码123登录一下试试
[root@zheng ~]# mysql -uroot -p123 成功了
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30
Copyright (c) 2000, 2020, 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> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
提示要用ALTER USER修改密码才可以
但是如果想要设置非常简单的密码,比如123456会提示密码不符合要求
mysql> alter user 'root'@'localhost' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
/ / 但是修改这个参数会报错,没有这个系统变量
mysql> set global validate_password_policy=0;
ERROR 1193 (HY000): Unknown system variable 'validate_password_policy'
添加参数
这个时候就得去修改一下mysql的配置文件
[root@zheng ~]# vi /etc/my.cnf
[mysqld] ## 在mysqld文本段里添加这两行
#添加密码验证插件
plugin-load-add=validate_password.so
#服务器在启动时加载插件,并防止在服务器运行时删除插件
validate-password=FORCE_PLUS_PERMANENT
"/etc/my.cnf" 34L, 1167C written
[root@zheng ~]# systemctl restart mysqld / / 修改完重启mysql
[root@zheng ~]# mysql -uroot -p
Enter password: 密码123
修改参数值
修改validate_password_policy参数的值
validate_password_length(密码长度)参数默认为8,我们修改为1
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql安装成功
mysql> show databases; / / 这里就可以正常使用了
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
这里说一下,我这用的是5.7版本的,mysql8.0是不支持这么修改的
mysql> status
--------------
mysql Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper
Connection id: 5
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.30
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 1 hour 58 min 34 sec
Threads: 1 Questions: 26 Slow queries: 0 Opens: 106 Flush tables: 1 Open tables: 99 Queries per second avg: 0.003
--------------
8.0mysql版本修改参数设置
mysql> set global validate_password.policy=0;
mysql> set global validate_password.length=1;