1 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 登录密码
安装过MySql5.x版本的同行们肯定知道,MySql安装完成之后会生成一个临时密码,并写入Log文件(/var/log/mysqld.log)。我们使用grep命令找一个这个临时密码。
grep 'temporary password' /var/log/mysqld.log
grep: /var/log/mysqld.log: No such file or directory
发信系统提示没有这个log文件。于是我到/etc/log目录下查看了一下,原来在/etc/log/目录下还有一个mysql的文件夹。所以log文件的正确路径应是 /etc/log/mysql/mysqld.log
grep 'temporary password' /var/log/mysql/mysqld.log
但是执行完命令之后还是没有查到任何的内容,于是我打开mysqld.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
在MySql8中并没有给我们设置初始密码。
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)
添加远程登录用户之后,我们就可以使用mysql连接客户端或者终端连接数据库了。
※能连接成功还有一个前提是系统的mysql服务是对外开放的。参照PHP学习-3 端口开放开放MySql服务的端口或者开放MySql服务。