CentOS7位安装MySql教程

Posted wwtao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7位安装MySql教程相关的知识,希望对你有一定的参考价值。

1.先检查系统是否装有mysql

rpm -qa | grep mysql

2.下载mysql的repo源

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

 

3.安装 mysql-community-release-el7-5.noarch.rpm包

 

sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

 

4.安装MySQL

 

sudo yum install mysql-server

 

5.重置MySQL密码

 

mysql -u root

 

如果报错:

 

ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2)

 

原因:原因是/var/lib/mysql的访问权限问题。

 

chown root /var/lib/mysql/

 

重启MySQL服务

 

service mysqld restart

 

接着登陆设置密码

 

mysql -u root

 

use mysql;

 

update user set password=password(‘123456‘) where user=‘root‘;

 

exit;

 

6.重启MySQL服务

 

service mysqld restart

 

接着设置Root账户远程连接密码,账户和密码都是 root

 

 mysql -u root -p

 

GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root"; 

 

重启服务器 

 

service mysqld restart

 

7.使用外网工具连接MySQL

 

连接mysql的用户名和密码都是 root

 

关闭防火墙

 

systemctl stop firewalld.service

 

 

 

 

下面为另一种办法,但是,测试没有成功

连接MySQL服务器,修改密码

 

1)查看初始密码

 

  1. [root@host50 ~]#grep –i ‘password‘ /var/log/mysqld.log
  2. 2017-04-01T18:10:42.948679Z 1 [Note] A temporary password is generated for root@localhost: mtoa>Av<p6Yk //随机生成的管理密码为mtoa>Av<p6Yk

 

2)使用初始密码连接mysql服务

 

  1. [root@host50 ~]# 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 11
  5. Server version: 5.7.17
  6. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type ‘help;‘ or \hfor help. Type \c‘ to clear the current input statement.
  11. mysql>                                     //登录成功后,进入SQL操作环境

 

3)重置数据库管理员roo本机登录密码

 

  1. mysql> show databases;
  2. ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement //提示必须修改密码
  3. mysql> alter user root@”localhost” identified by "123qqq…A"; //修改登陆密码
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql> exit //断开连接
  6. [root@host50 ~]#

 

4)修改密码策略

 

  1. [root@host50 ~]# mysql -uroot –p123qqq…A
  2. mysql>
  3. mysql>set global validate_password_policy=0; //只验证长度
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql>set global validate_password_length=6; //修改密码长度,默认值是8个字符
  6. Query OK, 0 rows affected (0.00 sec)
  7. mysql> alter user root@”localhost” identified by "tarena"; //修改登陆密码
  8. Query OK, 0 rows affected (0.00 sec)
  9. mysql>exit

 

5)使用修改后的密码登录

 

  1. [root@host50 ~]# mysql -uroot -ptarena        //登录
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 15
  4. Server version: 5.7.17 MySQL Community Server (GPL)
  5. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. mysql> show databases; //查看数据库
  10. +--------------------+
  11. | Database |
  12. +--------------------+
  13. | information_schema |
  14. | mysql |
  15. | performance_schema |
  16. | sys                |
  17. +--------------------+
  18. 4 rows in set (0.00 sec)
  19. mysql>

 

 

 

以上是关于CentOS7位安装MySql教程的主要内容,如果未能解决你的问题,请参考以下文章

CentOS7/64位环境安装Mysql 5.7.19二进制包教程

CentOS7 安装 MySQL5.7

CentOS7 64位 安装MySQL5.7

Centos7.4安装MySQL

Centos7安装并配置mysql5.6完美教程

Centos7安装并配置mysql5.6完美教程