在Linux 系统 Centos 7 与 CentOS 6 上手工安装mysql服务器V5.7.28及以上版本之详细步骤
本文适用于 CentOS 7 与 CentOS 6 上安装Mysql服务器。本文完整描述了msyql服务器手工安装过程与服务器的初始化事项。适合于手工安装mysql服务器的同学参考。
新购买了一个云服务器,上面没有安装mysql服务,今天从0开始安装mysql服务。
由于中间遇到不顺利的情节,所以把他记录下来,便于以后参考。
本文适用于 CentOS 7 与 CentOS 6 上安装Mysql服务器。
一、要下载的Mysql安装相关文件(共4个):
1)mysql-community-libs-5.7.28-1.el7.x86_64.rpm
https://downloads.mysql.com/archives/get/p/23/file/mysql-community-libs-5.7.28-1.el7.x86_64.rpm
2)mysql-community-common-5.7.28-1.el7.x86_64.rpm
https://downloads.mysql.com/archives/get/p/23/file/mysql-community-common-5.7.28-1.el7.x86_64.rpm
3)mysql-community-client-5.7.28-1.el7.x86_64.rpm
https://downloads.mysql.com/archives/get/p/23/file/mysql-community-client-5.7.28-1.el7.x86_64.rpm
4)mysql-community-server-5.7.28-1.el7.x86_64.rpm
https://downloads.mysql.com/archives/get/p/23/file/mysql-community-server-5.7.28-1.el7.x86_64.rpm
以上这些文件我已经添加了URL,不必访问Oracle网站,直接点击就可下载。
进入linux系统,执行如下命令进行下载:
cd /
mkdir mysql_install
cd mysql_install
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-libs-5.7.28-1.el7.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-common-5.7.28-1.el7.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-client-5.7.28-1.el7.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-server-5.7.28-1.el7.x86_64.rpm
注意需要按顺序安装以上RPM包,安装顺序如下:
rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
出现如下错误:
error: Failed dependencies:
libncurses.so.5()(64bit) is needed by mysql-community-client-5.7.28-1.el7.x86_64
libtinfo.so.5()(64bit) is needed by mysql-community-client-5.7.28-1.el7.x86_64
yum install libncurses*
在安装 执行 rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm 时,出现如下错误:
error: Failed dependencies:
libncurses.so.5()(64bit) is needed by mysql-community-client-5.7.28-1.el7.x86_64
libtinfo.so.5()(64bit) is needed by mysql-community-client-5.7.28-1.el7.x86_64
执行如下命令安装 libncurses
yum install libncurses*
从 rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm 重新执行,结果如下:
rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
warning: mysql-community-common-5.7.28-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
package mysql-community-common-5.7.28-1.el7.x86_64 is already installed
rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
warning: mysql-community-libs-5.7.28-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
package mysql-community-libs-5.7.28-1.el7.x86_64 is already installed
rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
warning: mysql-community-client-5.7.28-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-client-5.7.28-1.e################################# [100%]
rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.28-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-server-5.7.28-1.e################################# [100%]
至此,mysql所用到的文件安装成功!
安装完成后,mysql安装程序会创建mysql这个系统帐号,如果不存在,请用如下命令创建mysql用户:
groupadd mysql
useradd -r -g mysql mysql
二、修改配置文件
mysql的服务安装成功后,
服务器程序文件位置在 : /usr/sbin/mysqld,
同时配置文件位置在: /etc/my.cnf,
客户端程序位置在: /usr/bin/mysql
修改配置文件,输入如下命令:
vi /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
#
# 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
[client]
socket=/home/mysql-5.7.28/mysql.sock
[mysqld]
explicit_defaults_for_timestamp=true
datadir=/home/mysql-5.7.28/data
socket=/home/mysql-5.7.28/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/home/mysql-5.7.28/log/mysqld.log
pid-file=/home/mysql-5.7.28/mysqld.pid
my.cnf文件原来有内容,我将配置文件当中的目录一律改为 /home/mysql-5.7.28,这样便于以后升级或安装多个版本的mysql服务。注意,要设置好mysql客户端的socket,如下:
[client]
socket=/home/mysql-5.7.28/mysql.sock
如果未添加以上配置,则用mysql客户端时会提示找不到socket文件。
然后创建/home/mysql-5.7.28目录,命令如下:
cd /home
mkdir mysql-5.7.28
mkdir mysql-5.7.28/data
mkdir mysql-5.7.28/log
chown -R mysql:mysql mysql-5.7.28
三、首次启动mysqld服务,执行初始化与修改root密码
在命令好键入如下命令(不能用systemctl命令,CentOS7以上才支持,只能用service命令):
service mysqld start
这个命令将执行初始化数据库操作,创建初始数据库。
执行完成,如果没有错误,会创建一个root用户,并设置一个临时口令,必须修改这个口令才能对mysql数据库进行下一步操作,请修改root用户的口令,用如下命令查看这个临时口令:
cat /home/mysql-5.7.28/log/mysqld.log | grep password
查看的结果如下:
2019-10-30T11:29:10.750559Z 0 [Note] Shutting down plugin 'sha256_password'
2019-10-30T11:29:10.750562Z 0 [Note] Shutting down plugin 'mysql_native_password'
2019-10-30T11:46:55.703659Z 1 [Note] A temporary password is generated for root@localhost: ftW61Ogr=IRM
2019-10-30T11:47:59.235604Z 2 [Note] Access denied for user 'UNKNOWN_MYSQL_USER'@'localhost' (using password: NO)
2019-10-30T11:48:05.209678Z 0 [Note] Shutting down plugin 'validate_password'
2019-10-30T11:48:06.523413Z 0 [Note] Shutting down plugin 'sha256_password'
2019-10-30T11:48:06.523416Z 0 [Note] Shutting down plugin 'mysql_native_password'
字符串:ftW61Ogr=IRM 就是临时口令。
在命令行里键入如下mysql客户端:
mysql -u root -p
输入如上临时口令;成功后将进入mysql命令环境。如下所示,即表示安装mysql成功。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.28
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命令行环境里执行: show databases; 将会看到如下提示:
You must reset your password using ALTER USER statement before executing this statement.
必须重置mysql的root用户口令,才能进一步操作。
由于mysql安装完成后初始化对命令的校验级别比较高,修改命令不会成功的,因此用如下命令修改命令的校验级别:
set global validate_password_policy=LOW;
然后用如下命令修改mysql的root口令:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'lyh@26670927';
root用户的口令修改为 lyh@26670927,提示如下:
Query OK, 0 rows affected (0.00 sec)
再次敲入:show databases; 出现如下结果:
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
至此,mysql数据库全部安装成功,就可以用PHP,java来访问了。
更多推荐
所有评论(0)