MySQL——Centos7下环境安装

MySQL——Centos7下环境安装_第1张图片

目录

0.说明

1. 检查环境(systemctl start/stop/restart)

2. 检查系统安装包(rpm -qa)

3. 卸载这些默认安装包(yum remove、xargs)

4. 获取mysql官方yum源

5. 安装mysql yum源,对比前后yum源(rpm -ivh)

6. 能否正常工作

7. 安装mysql服务

8. 查看配置文件和数据存储位置

9. 启动服务(systemctl start)

10. 查看启动服务

11. 登陆方法一

12. 登陆方法二

13. 登陆方式三

14. 设置开机启动

15. 配置my.cnf

16. 常见问题


0.说明

  • 安装与卸载中,⽤⼾全部切换成为root,⼀旦安装,普通⽤⼾能使⽤的
  • mysql不进⾏⽤⼾管理,全部使⽤root进⾏,掌握mysql语句,学习⽤⼾管理后,在考虑新建普通⽤⼾

1. 检查环境(systemctl start/stop/restart)

1. 查看系统当中是否存在MySQL或者mariadb

(mariadb是MySQL开源分支),存在可直接使用MySQL

[root@VM-4-10-centos /]# ps axj | grep mysql
 8577 10898 10897 17609 pts/2    10897 S+       0   0:00 grep --color=auto mysql
    1 20559 20558 20558 ?           -1 Sl      27  25:33 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
[root@VM-4-10-centos /]# ps axj | grep mariadb
 8577 10978 10977 17609 pts/2    10977 S+       0   0:00 grep --color=auto mariadb

2. 确定MySQL版本

[root@VM-4-10-centos /]# which mysql
/usr/bin/mysql
[root@VM-4-10-centos /]# mysql --version
mysql  Ver 14.14 Distrib 5.7.43, for Linux (x86_64) using  EditLine wrapper

3. 使用Linux脚本程序终止MySQL服务,systemctl stop/ restart/start,相当于暂停守护进程

[root@VM-4-10-centos /]# systemctl stop mysqld

[root@VM-4-10-centos /]# 
[root@VM-4-10-centos /]# ps axj | grep mariadb
 8577 12824 12823 17609 pts/2    12823 S+       0   0:00 grep --color=auto mariadb

2. 检查系统安装包(rpm -qa)

使用系统命令rpm检查系统所安装的MySQL安装包

[root@VM-4-10-centos /]# rpm -qa | grep mysql
mysql57-community-release-el7-9.noarch
mysql-community-libs-5.7.43-1.el7.x86_64
mysql-community-common-5.7.43-1.el7.x86_64
mysql-community-libs-compat-5.7.43-1.el7.x86_64
mysql-community-server-5.7.43-1.el7.x86_64
mysql-community-client-5.7.43-1.el7.x86_64

3. 卸载这些默认安装包(yum remove、xargs)

使用系统工具xargs将从标准输出读取内容按照行转换为参数argv,传递给yum,逐行卸载,需要加参数-y,yum卸载会询问,因此批量卸载会中断

[root@VM-4-10-centos /]# rpm -qa | grep mysql | xargs yum -y remove
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Resolving Dependencies

4. 获取mysql官方yum源

获取mysql官⽅yum源 http://repo.mysql.com/

查看Linux本地yum源:

[root@VM-4-10-centos /]# ls /etc/yum.repos.d/ -l
total 52
-rw-r--r-- 1 root root  615 Jul 20 18:00 CentOS-Base.repo
-rw-r--r-- 1 root root 1309 Nov 23  2020 CentOS-CR.repo
-rw-r--r-- 1 root root  649 Nov 23  2020 CentOS-Debuginfo.repo
-rw-r--r-- 1 root root  230 Mar 21  2023 CentOS-Epel.repo
-rw-r--r-- 1 root root  314 Nov 23  2020 CentOS-fasttrack.repo
-rw-r--r-- 1 root root  630 Nov 23  2020 CentOS-Media.repo
-rw-r--r-- 1 root root 1331 Nov 23  2020 CentOS-Sources.repo
-rw-r--r-- 1 root root 8515 Nov 23  2020 CentOS-Vault.repo
-rw-r--r-- 1 root root  616 Nov 23  2020 CentOS-x86_64-kernel.repo
-rw-r--r-- 1 root root 1358 Sep  5  2021 epel.repo
-rw-r--r-- 1 root root 1457 Sep  5  2021 epel-testing.repo

有关安装版本选择:

1.  确定自身系统版本,Centos对应安装el,Ubuntu对应安装Ubuntu,SUSE对应sles

[root@VM-4-10-centos /]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

2. 最好安装和⾃⼰系统⼀致的mysql版本,否则可能会存在软件兼容性问题,yum源中mysql后数字对应版本,本示例安装5.7版本mysql157

3. 根据系统版本7.9 选择对应的mysql157 - release -el7.9.noarch.rpm

5. 安装mysql yum源,对比前后yum源(rpm -ivh)

1. 使用系统命令rz上传yum源

[root@VM-4-10-centos 1Lesson]# ls
mysql57-community-release-el7-9.noarch.rpm
[root@VM-4-10-centos 1Lesson]# rz  

2. 使用系统命令rpm -ivh对yum源相当于解包解压

[root@VM-4-10-centos 1Lesson]# ls
mysql57-community-release-el7-9.noarch.rpm
[root@VM-4-10-centos 1Lesson]# ls /etc/yum.repos.d/ -l
total 52
-rw-r--r-- 1 root root  615 Jul 20 18:00 CentOS-Base.repo
-rw-r--r-- 1 root root 1309 Nov 23  2020 CentOS-CR.repo
-rw-r--r-- 1 root root  649 Nov 23  2020 CentOS-Debuginfo.repo
-rw-r--r-- 1 root root  230 Mar 21  2023 CentOS-Epel.repo
-rw-r--r-- 1 root root  314 Nov 23  2020 CentOS-fasttrack.repo
-rw-r--r-- 1 root root  630 Nov 23  2020 CentOS-Media.repo
-rw-r--r-- 1 root root 1331 Nov 23  2020 CentOS-Sources.repo
-rw-r--r-- 1 root root 8515 Nov 23  2020 CentOS-Vault.repo
-rw-r--r-- 1 root root  616 Nov 23  2020 CentOS-x86_64-kernel.repo
-rw-r--r-- 1 root root 1358 Sep  5  2021 epel.repo
-rw-r--r-- 1 root root 1457 Sep  5  2021 epel-testing.repo
[root@VM-4-10-centos 1Lesson]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-9  ################################# [100%]
[root@VM-4-10-centos 1Lesson]# ls /etc/yum.repos.d/ -l
total 60
-rw-r--r-- 1 root root  615 Jul 20 18:00 CentOS-Base.repo
-rw-r--r-- 1 root root 1309 Nov 23  2020 CentOS-CR.repo
-rw-r--r-- 1 root root  649 Nov 23  2020 CentOS-Debuginfo.repo
-rw-r--r-- 1 root root  230 Mar 21  2023 CentOS-Epel.repo
-rw-r--r-- 1 root root  314 Nov 23  2020 CentOS-fasttrack.repo
-rw-r--r-- 1 root root  630 Nov 23  2020 CentOS-Media.repo
-rw-r--r-- 1 root root 1331 Nov 23  2020 CentOS-Sources.repo
-rw-r--r-- 1 root root 8515 Nov 23  2020 CentOS-Vault.repo
-rw-r--r-- 1 root root  616 Nov 23  2020 CentOS-x86_64-kernel.repo
-rw-r--r-- 1 root root 1358 Sep  5  2021 epel.repo
-rw-r--r-- 1 root root 1457 Sep  5  2021 epel-testing.repo
-rw-r--r-- 1 root root 1416 Sep 12  2016 mysql-community.repo
-rw-r--r-- 1 root root 1440 Sep 12  2016 mysql-community-source.repo

3. 观察mysql-community.repo,拥有各版本MySQL,yum链接及客服端服务端,yum会根据系统找最合适的yum源

  1 [mysql-connectors-community]
  2 name=MySQL Connectors Community
  3 baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
  4 enabled=1
  5 gpgcheck=1
  6 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  7 
  8 [mysql-tools-community]
  9 name=MySQL Tools Community
 10 baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
 11 enabled=1
 12 gpgcheck=1
 13 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
 14 
 15 # Enable to use MySQL 5.5
 16 [mysql55-community]
 17 name=MySQL 5.5 Community Server
 18 baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
 19 enabled=0
 20 gpgcheck=1
 21 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
 22 
 23 # Enable to use MySQL 5.6
 24 [mysql56-community]
 25 name=MySQL 5.6 Community Server
 26 baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
 27 enabled=0
 28 gpgcheck=1
 29 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

6. 能否正常工作

使用yum查找mysql,查看能否成功工作,根据yum源在对应的网站中mysql镜像或者安装包进行获取

[root@VM-4-10-centos 1Lesson]# yum list | grep mysql
Repository epel is listed more than once in the configuration
mysql57-community-release.noarch         el7-9                         installed
akonadi-mysql.x86_64                     1.9.2-4.el7                   os       
anope-mysql.x86_64                       2.0.14-1.el7                  epel     
apr-util-mysql.x86_64                    1.5.2-6.el7_9.1               updates  
calligra-kexi-driver-mysql.x86_64        2.9.10-2.el7                  epel     
collectd-mysql.x86_64                    5.8.1-1.el7                   epel     
dmlite-plugins-mysql.x86_64              1.15.2-15.el7                 epel     
dovecot-mysql.x86_64                     1:2.2.36-8.el7                os       
dpm-copy-server-mysql.x86_64             1.13.0-1.el7                  epel     
dpm-name-server-mysql.x86_64             1.13.0-1.el7                  epel 

7. 安装mysql服务

1. 安装前是不存在mysql服务及客户端,以及配置文件的

[root@VM-4-10-centos 1Lesson]# which mysql
/usr/bin/which: no mysql in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/customer/.local/bin:/home/customer/bin)
[root@VM-4-10-centos 1Lesson]# which mysqld
/usr/bin/which: no mysqld in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/customer/.local/bin:/home/customer/bin)
[root@VM-4-10-centos 1Lesson]# ls /etc/my.cf
ls: cannot access /etc/my.cf: No such file or directory

2. 使用yum install进行安装mysql-community-server,会将服务端、客户端、开发库、公共组件等全部进行安装

[root@VM-4-10-centos 1Lesson]# yum install -y mysql-community-server
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile

3. 安装成功

[root@VM-4-10-centos 1Lesson]# which mysql
/usr/bin/mysql
[root@VM-4-10-centos 1Lesson]# which mysqld
/usr/sbin/mysqld
[root@VM-4-10-centos 1Lesson]# ls /etc/my.cnf
/etc/my.cnf

4. 安装失败跳转第16步

8. 查看配置文件和数据存储位置

查看配置文件/etc/my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

9. 启动服务(systemctl start)

启动mysql服务端mysqld

[root@VM-4-10-centos 1Lesson]# systemctl start mysqld
[root@VM-4-10-centos 1Lesson]# ps axj | grep mysqld
    1  3696  3695  3695 ?           -1 Sl      27   0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
 8577  3806  3805 17609 pts/2     3805 S+       0   0:00 grep --color=auto mysqld

10. 查看启动服务

查看mysqld服务端口号,TCP协议,应用层服务(网络服务:有自己的协议)

[root@VM-4-10-centos 1Lesson]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1164/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      1164/sshd           
tcp6       0      0 :::3306                 :::*                    LISTEN      3696/mysqld  

11. 登陆方法一

#获取临时root密码

[root@VM-4-10-centos 1Lesson]# grep 'temporary password' /var/log/mysqld.log
2022-10-17T08:56:20.292348Z 1 [Note] A temporary password is generated for root@localhost: 5xk/,!/t)5Pl

#使⽤临时密码登录

#判断修改密码时候新密码是否符合当前的策略,不满⾜报错,不让修改,关闭它 #安全强度,默认为中,即1,要求必须包含 数字、符号、⼤⼩写字⺟,⻓度⾄少为8位

mysql> set global validate_password_policy=0;

Query OK, 0 rows affected (0.00 sec)

#设置密码最⼩⻓度

mysql> set global validate_password_length=1;

Query OK, 0 rows affected (0.00 sec)

#修改本地登录密码,暂不授权远程登录

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root.888';

Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

#如果你安装的最新的mysql,没有所谓的临时密码,root默认没有密码

12. 登陆方法二

# 如果你安装的最新的mysql,没有所谓的临时密码,root默认没有密码

# 试着直接client登陆⼀下

13. 登陆方式三

1. 打开mysql配置⽂件

[root@VM-4-10-centos 1Lesson]# vim /etc/my.cnf

在[mysqld]最后⼀栏配置(不知道是什么,就放在配置⽂件最后) 加⼊: skip-grant-tables 选项, 并保存退出

2. 重启服务systemctl restart

3. 重新登陆

[root@VM-4-10-centos 1Lesson]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 MySQL Community Server (GPL)

14. 设置开机启动

#开启开机⾃启动

systemctl enable mysqld

systemctl daemon-reload

15. 配置my.cnf

#配置⼀下my.conf,主要是数据库客⼾端和服务器的编码格式

#default-character-set=utf8 ,暂不设置,mysql有bug,汉字不回显

character-set-server=utf8

default-storage-engine=innodb

# 配置完毕,重启mysql即可

16. 常见问题

1. 安装遇到秘钥过期的问题(服务器是统一环境,一部分秘钥在打包中是过期的):

Failing package is: mysql-community-client-5.7.39-1.el7.x86_64

GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

解决⽅案执行此行命令,更新秘钥(主要用于mysql安全通信,防止握手失败):

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

2. mysql 已经配置了客⼾端服务器utf8编码,但是⽆法输⼊中⽂

确保您在终端命令⾏中可以输⼊中⽂

[root@VM-0-3-centos ~]$ env | grep LANG LANG=en_US.utf8

你可能感兴趣的:(MySQL,mysql,数据库)