MySQL快速入门——MySQL在CentOS7环境下的安装

MySQL快速入门——MySQL在CentOS7环境下的安装

  • 0. 前言
  • 1. 卸载不要的环境
  • 2. 获取mysql官方yum源
  • 3. 安装MySQL yum源,对比前后yum源
  • 4. 安装MySQL服务
  • 5. 启动并查看服务
  • 6. 登入MySQL
  • 7. 配置my.cnf
  • 8. MySQL开机自启动


0. 前言


说明:

  • 安装与卸载过程中,用户全部切换成为root。⼀旦安装,普通用户也是能使用的;
  • 初期练习,mysql不进行用户管理,全部使用root进行,尽快适应mysql语句,后面学了用户管理,再考虑新建普通用户。

1. 卸载不要的环境


1. 检查本机有无mysql启动,有的话停止该服务:

[root@localhost ~]# ps axj | grep mysql
     1   2799   2798   2798 ?            -1 Sl      27   0:26 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
  9488   9720   9719   2886 pts/0      9719 R+       0   0:00 grep --color=auto mysql
[root@localhost ~]# ps axj | grep mariadb
  9488   9739   9738   2886 pts/0      9738 R+       0   0:00 grep --color=auto mariadb
  • 可以发现,我的电脑上是安装了mysql的,并且该服务正在启动;
  • 可以使用命令systemctl stop mysqld停止该服务;
  • 如果mariadb服务也在启动,还需要使用指令systemctl stop mariadb.service终止mariadb服务(我的电脑上这个服务没有启动)。
[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# ps axj | grep mysql
  9488   9818   9817   2886 pts/0      9817 S+       0   0:00 grep --color=auto mysql

2. 检查系统安装包,并批量化卸载:

  • 使用rpm -qa | grep mysql检查系统安装包。
[root@localhost ~]# rpm -qa | grep mysql
mysql-community-libs-compat-5.7.28-1.el7.x86_64
mysql57-community-release-el7-10.noarch
mysql-community-common-5.7.28-1.el7.x86_64
mysql-community-server-5.7.28-1.el7.x86_64
mysql-community-libs-5.7.28-1.el7.x86_64
mysql-community-client-5.7.28-1.el7.x86_64
  • 使用rpm -qa | grep mysql | xargs yum -y remove,批量化卸载。
[root@localhost ~]# rpm -qa | grep mysql | xargs yum -y remove
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.28-1.el7 will be erased
---> Package mysql-community-common.x86_64 0:5.7.28-1.el7 will be erased
---> Package mysql-community-libs.x86_64 0:5.7.28-1.el7 will be erased
---> Package mysql-community-libs-compat.x86_64 0:5.7.28-1.el7 will be erased
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-7.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-7.el7.x86_64
---> Package mysql-community-server.x86_64 0:5.7.28-1.el7 will be erased
---> Package mysql57-community-release.noarch 0:el7-10 will be erased
--> Running transaction check
---> Package postfix.x86_64 2:2.10.1-7.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================
 Package                                  Arch                Version                      Repository                       Size
=================================================================================================================================
Removing:
 mysql-community-client                   x86_64              5.7.28-1.el7                 @mysql57-community              175 M
 mysql-community-common                   x86_64              5.7.28-1.el7                 @mysql57-community              2.8 M
 mysql-community-libs                     x86_64              5.7.28-1.el7                 @mysql57-community               17 M
 mysql-community-libs-compat              x86_64              5.7.28-1.el7                 @mysql57-community              6.2 M
 mysql-community-server                   x86_64              5.7.28-1.el7                 @mysql57-community              868 M
 mysql57-community-release                noarch              el7-10                       installed                        30 k
Removing for dependencies:
 postfix                                  x86_64              2:2.10.1-7.el7               @base                            12 M

Transaction Summary
=================================================================================================================================
Remove  6 Packages (+1 Dependent package)

Installed size: 1.1 G
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : mysql-community-server-5.7.28-1.el7.x86_64                                                                    1/7 
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave
  Erasing    : mysql-community-client-5.7.28-1.el7.x86_64                                                                    2/7 
  Erasing    : 2:postfix-2.10.1-7.el7.x86_64                                                                                 3/7 
  Erasing    : mysql57-community-release-el7-10.noarch                                                                       4/7 
  Erasing    : mysql-community-libs-compat-5.7.28-1.el7.x86_64                                                               5/7 
  Erasing    : mysql-community-libs-5.7.28-1.el7.x86_64                                                                      6/7 
  Erasing    : mysql-community-common-5.7.28-1.el7.x86_64                                                                    7/7 
  Verifying  : 2:postfix-2.10.1-7.el7.x86_64                                                                                 1/7 
  Verifying  : mysql57-community-release-el7-10.noarch                                                                       2/7 
  Verifying  : mysql-community-common-5.7.28-1.el7.x86_64                                                                    3/7 
  Verifying  : mysql-community-libs-compat-5.7.28-1.el7.x86_64                                                               4/7 
  Verifying  : mysql-community-libs-5.7.28-1.el7.x86_64                                                                      5/7 
  Verifying  : mysql-community-client-5.7.28-1.el7.x86_64                                                                    6/7 
  Verifying  : mysql-community-server-5.7.28-1.el7.x86_64                                                                    7/7 

Removed:
  mysql-community-client.x86_64 0:5.7.28-1.el7                 mysql-community-common.x86_64 0:5.7.28-1.el7                     
  mysql-community-libs.x86_64 0:5.7.28-1.el7                   mysql-community-libs-compat.x86_64 0:5.7.28-1.el7                
  mysql-community-server.x86_64 0:5.7.28-1.el7                 mysql57-community-release.noarch 0:el7-10                        

Dependency Removed:
  postfix.x86_64 2:2.10.1-7.el7                                                                                                  

Complete!
  • 再检查一下是否卸干净了:
    • 检查rpm安装包还在不在;
    • 检查是否还有mysql进程在运行;
    • 检查配置文件my.cnf是否还在,还在就将其卸载(卸载之前可以重命名,备份一下)。
[root@localhost ~]# rpm -qa | grep mysql
[root@localhost ~]# ps axj | grep mysql
  9488   9971   9970   2886 pts/0      9970 R+       0   0:00 grep --color=auto mysql
[root@localhost ~]# ls /etc/my.cnf
ls: cannot access /etc/my.cnf: No such file or directory

2. 获取mysql官方yum源


1. 查看自己CentOS7的版本:

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 

2. 在官方网站,下载对应的mysql到本地:

  • 官方yum源:http://repo.mysql.com/
  • 我所下载的mysql版本如下:

MySQL快速入门——MySQL在CentOS7环境下的安装_第1张图片

3. 上传至Linux服务器:

  • 建议在root家目录下新建一个文件夹,将rpm安装包放在这个文件夹中,方便管理。
[root@localhost MySQL]# pwd
/root/MySQL
[root@localhost MySQL]# rz

[root@localhost MySQL]# ll
total 28
-rw-r--r--. 1 root root 25680 Jul 18 07:40 mysql57-community-release-el7.rpm

3. 安装MySQL yum源,对比前后yum源


1. 查看未安装MySQL yum源时的yum源:

  • 大家的yum源可能跟我的有出入,这都不重要。
[root@localhost MySQL]# ls /etc/yum.repos.d/ -l
total 44
-rw-r--r--. 1 root root 2523 Nov 30  2022 CentOS-Base.repo
-rw-r--r--. 1 root root 1664 Nov 30  2022 CentOS-Base.repo.bak
-rw-r--r--. 1 root root 1309 Nov 29  2016 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Nov 29  2016 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 Nov 29  2016 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Nov 29  2016 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 29  2016 CentOS-Sources.repo
-rw-r--r--. 1 root root 2893 Nov 29  2016 CentOS-Vault.repo
-rw-r--r--. 1 root root 2081 Mar  9 00:07 docker-ce.repo
-rw-r--r--. 1 root root 1358 Sep  4  2021 epel.repo
-rw-r--r--. 1 root root 1457 Sep  4  2021 epel-testing.repo

2. 安装MySQL yum源,并对比前后yum源:

  • 安装命令是:rpm -ivh mysql57-community-release-el7.rpm
[root@localhost MySQL]# rpm -ivh mysql57-community-release-el7.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-11 ################################# [100%]
[root@localhost MySQL]# ls /etc/yum.repos.d/ -l
total 52
-rw-r--r--. 1 root root 2523 Nov 30  2022 CentOS-Base.repo
-rw-r--r--. 1 root root 1664 Nov 30  2022 CentOS-Base.repo.bak
-rw-r--r--. 1 root root 1309 Nov 29  2016 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Nov 29  2016 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 Nov 29  2016 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Nov 29  2016 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 29  2016 CentOS-Sources.repo
-rw-r--r--. 1 root root 2893 Nov 29  2016 CentOS-Vault.repo
-rw-r--r--. 1 root root 2081 Mar  9 00:07 docker-ce.repo
-rw-r--r--. 1 root root 1358 Sep  4  2021 epel.repo
-rw-r--r--. 1 root root 1457 Sep  4  2021 epel-testing.repo
-rw-r--r--. 1 root root 1838 Apr 27  2017 mysql-community.repo
-rw-r--r--. 1 root root 1885 Apr 27  2017 mysql-community-source.repo

3. 检查MySQL yum源是否正常工作:

[root@localhost MySQL]# yum list |grep mysql
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
mysql57-community-release.noarch         el7-11                        installed
akonadi-mysql.x86_64                     1.9.2-4.el7                   base     
anope-mysql.x86_64                       2.1.4-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-2.el7                   epel     
dmlite-plugins-mysql.x86_64              1.15.2-15.el7                 epel     
dovecot-mysql.x86_64                     1:2.2.36-8.el7                base     
dpm-copy-server-mysql.x86_64             1.13.0-1.el7                  epel     
dpm-name-server-mysql.x86_64             1.13.0-1.el7                  epel     
dpm-server-mysql.x86_64                  1.13.0-1.el7                  epel     
dpm-srm-server-mysql.x86_64              1.13.0-1.el7                  epel     
dspam-mysql.x86_64                       3.10.2-12.el7                 epel     
exim-mysql.x86_64                        4.97.1-1.el7                  epel     
freeradius-mysql.x86_64                  3.0.13-15.el7                 base     
fts-mysql.x86_64                         3.12.0-1.el7                  epel     
gnokii-smsd-mysql.x86_64                 0.6.31-17.el7                 epel     
holland-mysql.noarch                     1.1.22-1.el7                  epel     
holland-mysqldump.noarch                 1.1.22-1.el7                  epel     
holland-mysqllvm.noarch                  1.1.22-1.el7                  epel     
lfc-server-mysql.x86_64                  1.13.0-1.el7                  epel     
libdbi-dbd-mysql.x86_64                  0.8.3-16.el7                  base     
libgda-mysql.x86_64                      1:5.2.2-7.el7                 epel     
libnss-mysql.x86_64                      1.5-19.el7                    epel     
libodb-mysql.x86_64                      2.3.0-1.el7                   epel     
libodb-mysql-devel.x86_64                2.3.0-1.el7                   epel     
lighttpd-mod_authn_mysql.x86_64          1.4.54-1.el7                  epel     
lighttpd-mod_mysql_vhost.x86_64          1.4.54-1.el7                  epel     
mysql++.x86_64                           3.1.0-12.el7                  epel     
mysql++-devel.x86_64                     3.1.0-12.el7                  epel     
mysql++-manuals.x86_64                   3.1.0-12.el7                  epel     
mysql-community-client.i686              5.7.44-1.el7                  mysql57-community
mysql-community-client.x86_64            5.7.44-1.el7                  mysql57-community
mysql-community-common.i686              5.7.44-1.el7                  mysql57-community
mysql-community-common.x86_64            5.7.44-1.el7                  mysql57-community
mysql-community-devel.i686               5.7.44-1.el7                  mysql57-community
mysql-community-devel.x86_64             5.7.44-1.el7                  mysql57-community
mysql-community-embedded.i686            5.7.44-1.el7                  mysql57-community
mysql-community-embedded.x86_64          5.7.44-1.el7                  mysql57-community
mysql-community-embedded-compat.i686     5.7.44-1.el7                  mysql57-community
mysql-community-embedded-compat.x86_64   5.7.44-1.el7                  mysql57-community
mysql-community-embedded-devel.i686      5.7.44-1.el7                  mysql57-community
mysql-community-embedded-devel.x86_64    5.7.44-1.el7                  mysql57-community
mysql-community-libs.i686                5.7.44-1.el7                  mysql57-community
mysql-community-libs.x86_64              5.7.44-1.el7                  mysql57-community
mysql-community-libs-compat.i686         5.7.44-1.el7                  mysql57-community
mysql-community-libs-compat.x86_64       5.7.44-1.el7                  mysql57-community
mysql-community-release.noarch           el7-5                         mysql-connectors-community
mysql-community-server.x86_64            5.7.44-1.el7                  mysql57-community
mysql-community-test.x86_64              5.7.44-1.el7                  mysql57-community
mysql-connector-c++.x86_64               8.4.0-1.el7                   mysql-connectors-community
mysql-connector-c++-devel.x86_64         8.4.0-1.el7                   mysql-connectors-community
mysql-connector-c++-jdbc.x86_64          8.4.0-1.el7                   mysql-connectors-community
mysql-connector-j.noarch                 1:8.4.0-1.el7                 mysql-connectors-community
mysql-connector-java.noarch              1:8.0.30-1.el7                mysql-connectors-community
mysql-connector-odbc.x86_64              8.4.0-1.el7                   mysql-connectors-community
mysql-connector-odbc-setup.x86_64        8.4.0-1.el7                   mysql-connectors-community
mysql-connector-python.noarch            2.0.4-1.el7                   mysql-connectors-community
mysql-connector-python.x86_64            8.0.23-1.el7                  mysql-connectors-community
mysql-connector-python-cext.x86_64       8.0.21-1.el7                  mysql-connectors-community
mysql-connector-python3.x86_64           8.4.0-1.el7                   mysql-connectors-community
mysql-connector-python3-cext.x86_64      8.0.21-1.el7                  mysql-connectors-community
mysql-mmm.noarch                         2.2.1-15.el7                  epel     
mysql-mmm-agent.noarch                   2.2.1-15.el7                  epel     
mysql-mmm-monitor.noarch                 2.2.1-15.el7                  epel     
mysql-mmm-tools.noarch                   2.2.1-15.el7                  epel     
mysql-proxy.x86_64                       0.8.5-2.el7                   epel     
mysql-proxy-devel.x86_64                 0.8.5-2.el7                   epel     
mysql-ref-manual-5.5-en-html-chapter.noarch
                                         1-20170320                    mysql57-community
mysql-ref-manual-5.5-en-pdf.noarch       1-20170320                    mysql57-community
mysql-ref-manual-5.7-en-html-chapter.noarch
                                         1-20220831                    mysql57-community
mysql-ref-manual-5.7-en-pdf.noarch       1-20220831                    mysql57-community
mysql-router.x86_64                      8.0.12-1.el7                  mysql-tools-community
mysql-router-community.x86_64            8.0.37-1.el7                  mysql-tools-community
mysql-shell.x86_64                       8.0.37-1.el7                  mysql-tools-community
mysql-utilities.noarch                   1.6.5-1.el7                   mysql-tools-community
mysql-utilities-extra.noarch             1.5.6-1.el7                   mysql-tools-community
mysql-workbench-community.x86_64         8.0.22-1.el7                  mysql-tools-community
mysqlreport.noarch                       3.5-11.el7                    epel     
mysqltuner.noarch                        1.8.3-1.git.1333ea9.el7       epel     
mysqlx-connector-python3.x86_64          8.4.0-1.el7                   mysql-connectors-community
nagios-plugins-mysql.x86_64              2.4.9-1.el7                   epel     
nodejs-mysql.noarch                      2.3.2-1.el7                   epel     
opendbx-mysql.x86_64                     1.4.6-6.el7                   epel     
opensips-mysql.x86_64                    1.10.5-4.el7                  epel     
pcp-pmda-mysql.x86_64                    4.3.2-13.el7_9                updates  
pdns-backend-mysql.x86_64                4.1.14-1.el7                  epel     
perdition-mysql.x86_64                   2.2-2.el7                     epel     
perl-Test-mysqld.noarch                  0.21-1.el7                    epel     
php-mysql.x86_64                         5.4.16-48.el7                 base     
php-mysqlnd.x86_64                       5.4.16-48.el7                 base     
php-pear-MDB2-Driver-mysql.noarch        1.5.0-0.8.b4.el7              epel     
php-pear-MDB2-Driver-mysqli.noarch       1.5.0-0.8.b4.el7              epel     
poco-mysql.x86_64                        1.6.1-3.el7                   epel     
preludedb-mysql.x86_64                   5.2.0-1.el7                   epel     
proftpd-mysql.x86_64                     1.3.5e-12.el7                 epel     
python36-mysql.x86_64                    1.3.14-1.el7                  epel     
python36-mysql-debug.x86_64              1.3.14-1.el7                  epel     
qt-mysql.i686                            1:4.8.7-9.el7_9               updates  
qt-mysql.x86_64                          1:4.8.7-9.el7_9               updates  
qt5-qtbase-mysql.i686                    5.9.7-5.el7_9                 updates  
qt5-qtbase-mysql.x86_64                  5.9.7-5.el7_9                 updates  
redland-mysql.x86_64                     1.0.16-6.el7                  base     
root-sql-mysql.x86_64                    6.24.08-2.el7                 epel     
rsyslog-mysql.x86_64                     8.24.0-57.el7_9.3             updates  
soci-mysql.x86_64                        4.0.0-4.el7                   epel     
soci-mysql-devel.x86_64                  4.0.0-4.el7                   epel     
tcl-mysqltcl.x86_64                      3.052-1.el7                   epel     
voms-mysql-plugin.x86_64                 3.1.7-5.el7                   epel     
wt-dbo-mysql.x86_64                      3.3.9-1.el7                   epel     
zabbix50-dbfiles-mysql.noarch            5.0.42-1.el7                  epel     
zabbix50-proxy-mysql.x86_64              5.0.42-1.el7                  epel     
zabbix50-server-mysql.x86_64             5.0.42-1.el7                  epel     
zabbix6.0-dbfiles-mysql.noarch           6.0.29-1.el7                  epel     
zabbix6.0-proxy-mysql.x86_64             6.0.29-1.el7                  epel     
zabbix6.0-server-mysql.x86_64            6.0.29-1.el7                  epel     
zabbix6.0-web-mysql.noarch               6.0.29-1.el7                  epel     
  • 完成这些,之前下载的rpm安装包就可以卸载了。

4. 安装MySQL服务


1. 安装:

  • 一定要保证登入用户是root(使用su -切换,不是su);
  • 安装命令为:yum install -y mysql-community-server
[root@localhost MySQL]# yum install -y mysql-community-server
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.44-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.44-1.el7 for package: mysql-community-server-5.7.44-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.44-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.44-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.44-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.44-1.el7 will be installed
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.7.44-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================
 Package                               Arch                  Version                      Repository                        Size
=================================================================================================================================
Installing:
 mysql-community-server                x86_64                5.7.44-1.el7                 mysql57-community                184 M
Installing for dependencies:
 mysql-community-client                x86_64                5.7.44-1.el7                 mysql57-community                 31 M
 mysql-community-common                x86_64                5.7.44-1.el7                 mysql57-community                313 k
 mysql-community-libs                  x86_64                5.7.44-1.el7                 mysql57-community                3.0 M

Transaction Summary
=================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total size: 219 M
Installed size: 930 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 5 pre-existing rpmdb problem(s), 'yum check' output follows:
containerd.io-1.6.28-3.1.el7.x86_64 has installed conflicts containerd: containerd.io-1.6.28-3.1.el7.x86_64
containerd.io-1.6.28-3.1.el7.x86_64 has installed conflicts runc: containerd.io-1.6.28-3.1.el7.x86_64
ipa-client-4.4.0-12.el7.centos.x86_64 has installed conflicts freeipa-client: ipa-client-4.4.0-12.el7.centos.x86_64
ipa-client-common-4.4.0-12.el7.centos.noarch has installed conflicts freeipa-client-common: ipa-client-common-4.4.0-12.el7.centos.noarch
ipa-common-4.4.0-12.el7.centos.noarch has installed conflicts freeipa-common: ipa-common-4.4.0-12.el7.centos.noarch
  Installing : mysql-community-common-5.7.44-1.el7.x86_64                                                                    1/4 
  Installing : mysql-community-libs-5.7.44-1.el7.x86_64                                                                      2/4 
  Installing : mysql-community-client-5.7.44-1.el7.x86_64                                                                    3/4 
  Installing : mysql-community-server-5.7.44-1.el7.x86_64                                                                    4/4 
  Verifying  : mysql-community-server-5.7.44-1.el7.x86_64                                                                    1/4 
  Verifying  : mysql-community-client-5.7.44-1.el7.x86_64                                                                    2/4 
  Verifying  : mysql-community-libs-5.7.44-1.el7.x86_64                                                                      3/4 
  Verifying  : mysql-community-common-5.7.44-1.el7.x86_64                                                                    4/4 

Installed:
  mysql-community-server.x86_64 0:5.7.44-1.el7                                                                                   

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.44-1.el7                    mysql-community-common.x86_64 0:5.7.44-1.el7                   
  mysql-community-libs.x86_64 0:5.7.44-1.el7                     

Complete!

2. 检查是否安装成功:

[root@localhost MySQL]# which mysql
/bin/mysql
[root@localhost MySQL]# which mysqld
/sbin/mysqld
[root@localhost MySQL]# ls /etc/my.cnf
/etc/my.cnf

3. 安装过程中常见的问题:

  • 安装遇到秘钥过期的问题:
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
  • 解决方案:更换秘钥,在命令行中执行rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

5. 启动并查看服务


1. 启动并查看服务:

  • 启动服务的指令:systemctl start mysqld.service
[root@localhost MySQL]# systemctl start mysqld.service
[root@localhost MySQL]# ps axj |grep mysqld
     1  11320  11319  11319 ?            -1 Sl      27   0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
 10931  11371  11370   2886 pts/0     11370 R+       0   0:00 grep --color=auto mysqld

6. 登入MySQL


1. 登入时的问题:

  • 第一次登入时,可能会出现无法登入的情况。
[root@localhost MySQL]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  • 不要担心,接下来讲解决方案。

2. 打开MySQL配置文件:

  • 这个配置文件为/etc/my.cnf
  • 在最后一行加:skip-grant-tables选项,并保存退出(这个选项表示跳过输入密码)。
    MySQL快速入门——MySQL在CentOS7环境下的安装_第2张图片

3. 重启MySQL服务,并重新登入:

  • 重启指令:systemctl restart mysqld
  • 登入指令:mysql -uroot -p,需要你输密码时,直接按回车。
[root@localhost MySQL]# systemctl restart mysqld
[root@localhost MySQL]# mysql -uroot -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)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> 

7. 配置my.cnf


可以直接将下面的内容,覆盖到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
port=3306
character-set-server=utf8
default-storage-engine=innodb

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

skip-grant-tables

主要添加的配置选项有三个:

port=3306
character-set-server=utf8
default-storage-engine=innodb
  • port是默认端口号,一般是3306。有些公司考虑到数据的安全性,会更改这个端口号。
  • 后面两个我们之后再说。

修改完配置文件后,需要重启MySQL服务,配置文件才能生效。


8. MySQL开机自启动


依次执行命令:

[root@localhost ~]# systemctl enable mysqld
[root@localhost ~]# systemctl daemon-reload

你可能感兴趣的:(MySQL,mysql,adb,android)