1. 程式人生 > 其它 >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 '密碼';

密碼組成:

xxxxxxxxxxmysql> 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