美文网首页
CentOS7下安装mysql

CentOS7下安装mysql

作者: _Irving | 来源:发表于2023-02-19 11:41 被阅读0次

使用yum方式安装

  • 由于centos7默认安装了mariaDB,需要先将其卸载(yum方式可不用卸载,会被自动替代)
[wanchao@localhost ~]$ rpm -qa|grep maridb 
[wanchao@localhost ~]$ rpm -e --nodeps maridb-libs -5.5.64-1.el7.x86_64
  • 下载mysql仓库并安装
#https://repo.mysql.com//可以使用此地址在浏览器查看最新的,否则安装后可能会存在版本冲突问题
[wanchao@localhost ~]$wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
[root@localhost ~]# yum install -y mysql80-community-release-el7-3.noarch.rpm
  • 如果安装过程中出现如下错误无法安装,是因为mysql的GPG升级了,需要重新获取
Failing package is:mysql-community-client-plugins-8.0.29-1.el7.x86_64
GPG Keys are configured as file://etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

执行以下命令后再安装即可

[root@localhost ~]# rpm -import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
  • 安装mysql数据库
[root@localhost ~]#  yum install -y  mysql-community-server
  • 开启mysql服务
[root@localhost ~]# systemctl start mysqld.service
#systemctl status mysqld#查看状态
#systemctl restart mysqld.service  #重启服务
#systemctl stop mysqld  #停止服务

修改初始密码

  • 查看mysql默认密码并登录
[root@localhost ~]# cat /var/log/mysqld.log |grep password
2023-02-17T08:07:22.492625Z 6 [Note] [MY-010454] [Server] A temporary password is 
generated for root@localhost: uh_QTy2h9ufj
[root@localhost ~]# mysql -u root -p
#输入上面的默认密码登录
  • 如果执行sql语句报如下错,则需要修改初始密码
mysql> show databases;
#下面这个提示需要修改初始密码
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by '123456';
#提示密码强度不够,设置一个复杂度更高的密码通过即可
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
  • 设置弱密码
#下面能看到密码长度要求为8位,密码policy是medium
mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| validate_password.dictionary_file    |        |
| validate_password.length             | 8      |
| validate_password.mixed_case_count   | 1      |
| validate_password.number_count       | 1      |
| validate_password.policy             | MEDIUM |
| validate_password.special_char_count | 1      |
| validate_password_check_user_name    | ON     |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
14 rows in set (1.47 sec)
#修改密码policy是低等级
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.11 sec)
#修改密码长度要求为4
mysql> set global validate_password.length=4;
Query OK, 0 rows affected (0.09 sec)
#查看已经修改
mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| validate_password.dictionary_file    |        |
| validate_password.length             | 4      |
| validate_password.mixed_case_count   | 1      |
| validate_password.number_count       | 1      |
| validate_password.policy             | LOW    |
| validate_password.special_char_count | 1      |
| validate_password_check_user_name    | ON     |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
14 rows in set (0.33 sec)
#修改为123456
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
  • 设置远程连接
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.11 sec)
#将localhost改成%,允许任意地址访问即可
mysql> update user set host='%' where user = 'root';
Query OK, 1 row affected (0.56 sec)
Rows matched: 1  Changed: 1  Warnings: 0
#注意:改了host之后,重新修改root的密码
mysql> alter user 'root'@'%' identified by '123456';

mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
  • 注:如果使用客户端连接提示了plugin caching_sha2_password错误,这是因为MySQL8.0的密码策略默认为caching_sha2_password(MySQL5.7无此问题)
mysql> update user set plugin='mysql_native_password' where user='root';
Query OK, 1 row affected (1.34 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.37 sec)
mysql> select user,host,plugin from user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| root             | %         | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
4 rows in set (0.00 sec)

忘记密码处理

  • 在/etc/my.cnf下添加skip-grant-tables,重启登录
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables   #在[mysqld]下添加这一行
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
[root@localhost ~]# mysql   #重启mysql后直接mysql命令进入去修改登录密码即可,完了记住删除skip-grant-tables重启
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

相关文章

网友评论

      本文标题:CentOS7下安装mysql

      本文链接:https://www.haomeiwen.com/subject/ljsikdtx.html