MySql教程

linux系统 MYSQL的安装

本文主要是介绍linux系统 MYSQL的安装,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

安装mysql

#这里是安装mysql的源
yum install -y https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm

检查MySQL源是否安装成功

yum repolist enabled

部分结果:
mysql-connectors-community/x86_64                   MySQL Connectors Community           
mysql-tools-community/x86_64                        MySQL Tools Community                 
mysql80-community/x86_64                            MySQL 8.0 Community Server    

源安装完以后 安装mysql服务

yum install mysql-community-server


Transaction test succeeded
Running transaction
  Installing : mysql-community-common-8.0.21-1.el7.x86_64                                                               1/7
  Installing : mysql-community-libs-8.0.21-1.el7.x86_64                                                                 2/7
  Installing : mysql-community-client-8.0.21-1.el7.x86_64                                                               3/7
  Installing : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                4/7
  Installing : mysql-community-server-8.0.21-1.el7.x86_64                                                               5/7
  Installing : mysql-community-libs-compat-8.0.21-1.el7.x86_64                                                          6/7
  Erasing    : 1:mariadb-libs-5.5.65-1.el7.x86_64                                                                       7/7
  Verifying  : mysql-community-server-8.0.21-1.el7.x86_64                                                               1/7
  Verifying  : mysql-community-libs-8.0.21-1.el7.x86_64                                                                 2/7
  Verifying  : mysql-community-client-8.0.21-1.el7.x86_64                                                               3/7
  Verifying  : mysql-community-libs-compat-8.0.21-1.el7.x86_64                                                          4/7
  Verifying  : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                5/7
  Verifying  : mysql-community-common-8.0.21-1.el7.x86_64                                                               6/7
  Verifying  : 1:mariadb-libs-5.5.65-1.el7.x86_64                                                                       7/7

Installed:
  mysql-community-libs.x86_64 0:8.0.21-1.el7                 mysql-community-libs-compat.x86_64 0:8.0.21-1.el7
  mysql-community-server.x86_64 0:8.0.21-1.el7

Dependency Installed:
  mysql-community-client.x86_64 0:8.0.21-1.el7                 mysql-community-common.x86_64 0:8.0.21-1.el7
  net-tools.x86_64 0:2.0-0.25.20131004git.el7

Replaced:
  mariadb-libs.x86_64 1:5.5.65-1.el7

需要安装

Total download size: 553 M 

mysql服务操作

# 启动服务
systemctl start mysqld

# 查看服务
systemctl status mysqld

mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-08-01 16:06:52 EDT; 12s ago

修改mysql中root本地密码

先在日志中查找 生成的密码

grep 'temporary password' /var/log/mysqld.log
[root@bogon ~]# grep 'temporary password' /var/log/mysqld.log
2020-08-01T20:06:49.105603Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: J<wGLsguq9?n

使用密码root登录

mysql -u root -p


不过进行其他操作的时候,会发现 提示你用alert 语句去修改密码,这个密码只是临时密码。

mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

修改密码

# ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';

密码组成:

xxxxxxxxxx mysql> show global 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_dictionary_file :验证密码的字典文件,与之相同的不可使用​validate_password_length :密码最少长度​validate_password_number_count :最少数字字符数​validate_password_mixed_case_count :最少大写和小写字符数(同时有大写和小写)​validate_password_special_char_count :最少特殊字符数​validate_password_policy :密码安全策略    LOW: 只限制长度    MEDIUM: 限制长度、数字、字母、特殊字符    STRONG: 限制长度、数字、字母、特殊字符、字典​​#默认是中等,密码必须由   大写字母、小写字母、数字、特殊字符组成   且长度为8位

在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略

选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件

validate_password_policy=0

如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:

validate_password = off

重新启动mysql服务使配置生效:

systemctl restart mysqld

允许远程登录

MySQL默认只允许root帐户在本地登录,想要远程连接MySQL,必须开启root用户允许远程连接,或者添加一个允许远程连接的帐户。

首先查看mysql的 user表

select * from user \G

*************************** 4. row ***************************
                    Host: localhost
                    User: root
             Select_priv: Y
             Insert_priv: Y
             Update_priv: Y
             Delete_priv: Y
             Create_priv: Y
               Drop_priv: Y
             Reload_priv: Y
           Shutdown_priv: Y
            Process_priv: Y
               File_priv: Y
              Grant_priv: Y
         References_priv: Y
              Index_priv: Y
              Alter_priv: Y
            Show_db_priv: Y
              Super_priv: Y
   Create_tmp_table_priv: Y
        Lock_tables_priv: Y
            Execute_priv: Y
         Repl_slave_priv: Y
        Repl_client_priv: Y
        Create_view_priv: Y
          Show_view_priv: Y
     Create_routine_priv: Y
      Alter_routine_priv: Y
        Create_user_priv: Y
              Event_priv: Y
            Trigger_priv: Y
  Create_tablespace_priv: Y
                ssl_type:
              ssl_cipher: 0x
             x509_issuer: 0x
            x509_subject: 0x
           max_questions: 0
             max_updates: 0
         max_connections: 0
    max_user_connections: 0
                  plugin: caching_sha2_password
   authentication_string: $A$005$QVf`
hi@? _:w.FDDatpHPPpgl.7j830gDg9X.HbKF05PzcdmXptHuD
        password_expired: N
   password_last_changed: 2020-08-03 20:01:07
       password_lifetime: NULL
          account_locked: N
        Create_role_priv: Y
          Drop_role_priv: Y
  Password_reuse_history: NULL
     Password_reuse_time: NULL
Password_require_current: NULL
         User_attributes: NULL
4 rows in set (0.01 sec)

可以看到 root用户默认只能访问localhost 也就是只能本地登录,不能远程登录。

查询mysql具体版本

SELECT @@VERSION

mysql> select @@VERSION;
+-----------+
| @@VERSION |
+-----------+
| 8.0.21    |
+-----------+

此版的的mysql版本把将创建账户和赋予权限分开了

早期可以合二为一

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;

创建可以远程访问的root用户

create user 'root'@'%' identified by  '密码';

给远程root用户赋予权限

grant all privileges on *.* to 'root'@'%' with grant option;

操作后要执行刷新,远程用户才能有效

flush privileges;

查看数据库使用的字符集

show variables like '%character%%'

MySQL忘记root用户密码,解决方案

在 /etc/my.cnf文件[mysqld]下面加如下命令

[mysqld]
skip-grant-tables  #设置为跳过授权认证 不验证密码
mysql  -u root

#update mysql.user set authentication_string=password('newPass') where user='root' and #Host = 'localhost';

flush privileges

ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';

flush privileges
这篇关于linux系统 MYSQL的安装的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!