PHP學習-4 安裝MySql

1 MySql Yum Repository

MySql Yum Repository

rpm -Uvh http://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
Retrieving http://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
warning: /var/tmp/rpm-tmp.EgJlCT: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql80-community-release-el8-1  ################################# [100%]

2 MySql版本查看

yum repolist all | grep mysql
MySQL 8.0 Community Server                      1.6 MB/s | 543 kB     00:00
MySQL Connectors Community                      112 kB/s |  19 kB     00:00
MySQL Tools Community                           293 kB/s |  62 kB     00:00
mysql-cluster-8.0-community        MySQL Cluster 8.0 Community    disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community -  disabled
mysql-connectors-community         MySQL Connectors Community     enabled:    42
mysql-connectors-community-source  MySQL Connectors Community - S disabled
mysql-tools-community              MySQL Tools Community          enabled:    19
mysql-tools-community-source       MySQL Tools Community - Source disabled
mysql-tools-preview                MySQL Tools Preview            disabled
mysql-tools-preview-source         MySQL Tools Preview - Source   disabled
mysql80-community                  MySQL 8.0 Community Server     enabled:    31
mysql80-community-source           MySQL 8.0 Community Server - S disabled

如果我們想要使用的版本沒有啓用,我們可以修改源庫文件,將enabled=0改爲enabled=1,已經啓用的版本改爲enabled=0。

/etc/yum.repos.d/mysql-community.repo
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/8/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  • 查看啓用版本
yum repolist enabled | grep mysql
Last metadata expiration check: 0:12:37 ago on Wed 11 Mar 2020 01:00:34 PM EDT.
mysql-connectors-community           MySQL Connectors Community              42
mysql-tools-community                MySQL Tools Community                   19
mysql80-community                    MySQL 8.0 Community Server              31

3 MySql安裝

  • 安裝MySql
yum -y install mysql-server

在這裏插入圖片描述

  • 啓動MySql
service mysqld start
  • 查看啓動狀態
service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-03-11 13:31:44 EDT; 1min 15s ago
  Process: 3010 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 2880 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
  Process: 2856 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 2967 (mysqld)
   Status: "Server is operational"
    Tasks: 38 (limit: 11499)
   Memory: 536.5M
   CGroup: /system.slice/mysqld.service
           └─2967 /usr/libexec/mysqld --basedir=/usr

Mar 11 13:31:31 localhost.localdomain systemd[1]: Starting MySQL 8.0 database server...
Mar 11 13:31:31 localhost.localdomain mysql-prepare-db-dir[2880]: Initializing MySQL database
Mar 11 13:31:44 localhost.localdomain systemd[1]: Started MySQL 8.0 database server.

4 開機啓動

systemctl enable mysqld
systemctl daemon-reload
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.

5 登錄密碼

MySql安裝完成之後會生成一個臨時密碼,並寫入Log文件(/var/log/mysql/mysqld.log)

grep 'temporary password' /var/log/mysql/mysqld.log

我們發現竟然沒有查到這個密碼,我們進入這個log文件看一下

2020-03-11T17:31:31.540900Z 0 [System] [MY-013169] [Server] /usr/libexec/mysqld (mysqld 8.0.17) initializing of server in progress as process 2917
2020-03-11T17:31:37.183614Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2020-03-11T17:31:40.206593Z 0 [System] [MY-013170] [Server] /usr/libexec/mysqld (mysqld 8.0.17) initializing of server has completed
2020-03-11T17:31:41.613861Z 0 [System] [MY-010116] [Server] /usr/libexec/mysqld (mysqld 8.0.17) starting as process 2967
2020-03-11T17:31:43.907048Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-03-11T17:31:43.947394Z 0 [System] [MY-010931] [Server] /usr/libexec/mysqld: ready for connections. Version: '8.0.17'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  Source distribution.
2020-03-11T17:31:44.138559Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/lib/mysql/mysqlx.sock' bind-address: '::' port: 33060

我們看到Log中說設置的是空密碼,那MySql自動生成密碼可能是5.X版本的事情吧。

6 鏈接MySql

mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.17 Source distribution

Copyright (c) 2000, 2019, 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>ALTER USER 'root'@'localhost' IDENTIFIED BY 'new psd';
  • 修改MySql默認編碼
    編輯 /etc/my.cnf.d/mysql-server.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
  • 重啓MySql使修改生效
systemctl restart mysqld

7 遠程鏈接用戶添加

  • 添加
mysql> CREATE USER 'remote_usr'@'%' IDENTIFIED BY 'remote_psd';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'remote_usr'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
  • 查看
mysql> select host,user from mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | remote_usr       |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章