linux 下安装 mysql

1 相关参数

	系统	  : CentOS release 6.8 (Final)
	mysql : mysql-5.7.11

2 检查系统环境

  检查 mysql 库文件是否存在,存在则进行删除;

	[root@localhost software]# rpm -qa | grep mysql
	mysql-libs-5.1.73-7.el6.x86_64
	[root@localhost software]# rpm -e mysql-libs-5.1.73-7.el6.x86_64 --nodeps
	[root@localhost software]# ll
	total 535352
	-rw-r--r--. 1 root root 548193637 Mar 12 06:06 mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz
	[root@localhost software]# rpm -qa | grep mysql
	[root@localhost software]# 

3 准备安装包

官网下载: https://downloads.mysql.com/archives/community/
选择版本:
产品版本:5.7.11
系统	:Red Hat Enterprise Linux / Oracle Linux
系统版本:Red Hat Enterprise Linux6 / Oracle Linux 6(x86,64-bit)

在这里插入图片描述
右键点击 Download 选择复制地址, 使用命令:wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.11-1.el6.x86_64.rpm-bundle.tar

[root@localhost software]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.11-1.el6.x86_64.rpm-bundle.tar
--2020-03-12 06:37:51--  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.11-1.el6.x86_64.rpm-bundle.tar
Resolving downloads.mysql.com... 137.254.60.14
Connecting to downloads.mysql.com|137.254.60.14|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.11-1.el6.x86_64.rpm-bundle.tar [following]
--2020-03-12 06:37:56--  https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.11-1.el6.x86_64.rpm-bundle.tar
Resolving cdn.mysql.com... 23.193.25.9
Connecting to cdn.mysql.com|23.193.25.9|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 439500800 (419M) [application/x-tar]
Saving to: “mysql-5.7.11-1.el6.x86_64.rpm-bundle.tar”

 2% [===>                                                               ] 9,332,973    322K/s  eta 23m 33s 

4 安装

下载完成之后解压,命令:tar xvf mysql-5.7.11-1.el6.x86_64.rpm-bundle.tar

[root@localhost software]# tar xvf mysql-5.7.11-1.el6.x86_64.rpm-bundle.tar 
mysql-community-embedded-5.7.11-1.el6.x86_64.rpm
mysql-community-common-5.7.11-1.el6.x86_64.rpm
mysql-community-test-5.7.11-1.el6.x86_64.rpm
mysql-community-client-5.7.11-1.el6.x86_64.rpm
mysql-community-embedded-devel-5.7.11-1.el6.x86_64.rpm
mysql-community-server-5.7.11-1.el6.x86_64.rpm
mysql-community-devel-5.7.11-1.el6.x86_64.rpm
mysql-community-libs-5.7.11-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.11-1.el6.x86_64.rpm
[root@localhost software]# ll
total 1393756
-rw-r--r--. 1 root root  439500800 Feb  2  2016 mysql-5.7.11-1.el6.x86_64.rpm-bundle.tar
-rw-r--r--. 1 7155 wheel  24176228 Feb  2  2016 mysql-community-client-5.7.11-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel    333480 Feb  2  2016 mysql-community-common-5.7.11-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel   3854020 Feb  2  2016 mysql-community-devel-5.7.11-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel  38247128 Feb  2  2016 mysql-community-embedded-5.7.11-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel 130605584 Feb  2  2016 mysql-community-embedded-devel-5.7.11-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel   2237704 Feb  2  2016 mysql-community-libs-5.7.11-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel   1722544 Feb  2  2016 mysql-community-libs-compat-5.7.11-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel 141256272 Feb  2  2016 mysql-community-server-5.7.11-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel  97051252 Feb  2  2016 mysql-community-test-5.7.11-1.el6.x86_64.rpm

接着使用rpm 命令依次安装:

rpm -ivh mysql-community-libs-5.7.11-1.el6.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.11-1.el6.x86_64.rpm
rpm -ivh mysql-community-common-5.7.11-1.el6.x86_64.rpm
rpm -ivh mysql-community-embedded-5.7.11-1.el6.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.11-1.el6.x86_64.rpm
rpm -ivh mysql-community-embedded-devel-5.7.11-1.el6.x86_64.rpm
rpm -ivh mysql-community-client-5.7.11-1.el6.x86_64.rpm
rpm -ivh mysql-community-server-5.7.11-1.el6.x86_64.rpm
rpm -ivh mysql-community-test-5.7.11-1.el6.x86_64.rpm

窗口

[root@localhost software]# rpm -ivh mysql-community-libs-5.7.11-1.el6.x86_64.rpm
warning: mysql-community-libs-5.7.11-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-libs   ########################################### [100%]
[root@localhost software]# rpm -ivh mysql-community-libs-compat-5.7.11-1.el6.x86_64.rpm
warning: mysql-community-libs-compat-5.7.11-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-libs-co########################################### [100%]
[root@localhost software]# rpm -ivh mysql-community-common-5.7.11-1.el6.x86_64.rpm
warning: mysql-community-common-5.7.11-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
	package mysql-community-common-5.7.11-1.el6.x86_64 is already installed
[root@localhost software]# rpm -ivh mysql-community-embedded-5.7.11-1.el6.x86_64.rpm
warning: mysql-community-embedded-5.7.11-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-embedde########################################### [100%]
[root@localhost software]# rpm -ivh mysql-community-devel-5.7.11-1.el6.x86_64.rpm
warning: mysql-community-devel-5.7.11-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-devel  ########################################### [100%]
[root@localhost software]# rpm -ivh mysql-community-embedded-devel-5.7.11-1.el6.x86_64.rpm
warning: mysql-community-embedded-devel-5.7.11-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-embedde########################################### [100%]
[root@localhost software]# rpm -ivh mysql-community-client-5.7.11-1.el6.x86_64.rpm
warning: mysql-community-client-5.7.11-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-client ########################################### [100%]
[root@localhost software]# rpm -ivh mysql-community-server-5.7.11-1.el6.x86_64.rpm
warning: mysql-community-server-5.7.11-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
	package mysql-community-server-5.7.11-1.el6.x86_64 is already installed
[root@localhost software]# 
[root@localhost software]# rpm -ivh mysql-community-test-5.7.11-1.el6.x86_64.rpm
warning: mysql-community-test-5.7.11-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
	perl(Time::HiRes) is needed by mysql-community-test-5.7.11-1.el6.x86_64
[root@localhost software]# yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.tuna.tsinghua.edu.cn
Package libaio-0.3.107-10.el6.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package libaio-devel.x86_64 0:0.3.107-10.el6 will be installed
......
.....
Complete!

如果出现安装错误,缺少依赖的情况,根据提示安装即可;

yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes

全部安装完成以后,尝试启动 mysql 服务:service mysqld start

[root@localhost software]# service mysqld start
Initializing MySQL database:                               [  OK  ]
Installing validate password plugin:                       [  OK  ]
Starting mysqld:                                           [  OK  ]

5 密码

打开配置文件,找到日志的的目录,命令:cat /etc/my.cnf

[root@localhost local]# cat /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

即,log-error=/var/log/mysqld.log
找到日志,其中 JC*VnGZd 即为初始密码。

2020-03-12T14:31:44.591465Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-03-12T14:31:44.775049Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-03-12T14:31:44.963796Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-03-12T14:31:45.310650Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 3301033c-646e-11ea-9e94-000c29a87561.
2020-03-12T14:31:45.327760Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-03-12T14:31:45.362722Z 1 [Note] A temporary password is generated for root@localhost: JC<F0>*VnGZd

重置密码,使用安全设置, /usr/bin/mysql_secure_installation

[root@localhost local]# /usr/bin/mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: 
The existing password for the user account root has expired. Please set a new password.
New password: 
Re-enter new password: 
he 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
New password: 
Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 
[root@localhost local]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.11 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

6 设置字符集

打开 mysql 的配置文件: vim /etc/my.cnf
在 [mysqld] 下添加:

default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci

保存重启后,即可设置成为utf8字符集。

root@localhost ~]# vim /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
[client]
default-character-set = utf8

[mysql]
default-character-set = utf8

[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

default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
# 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

7 开放防火墙端口

操作命令

/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
/etc/rc.d/init.d/iptables save

第一步,先查看当前防火墙状态 service iptables status

[root@localhost sbin]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

第二步,没有找到3306端口,则添加,使用命令:
/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
/etc/rc.d/init.d/iptables save
添加完成后查看状态

[root@localhost sbin]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
[root@localhost sbin]# /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@localhost sbin]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3306 
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
3    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
6    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination       

8 设置自动重启

打开文件 /etc/rc.d/rc.local ,添加启动命令 /etc/init.d/mysqld start,操作命令: vim /etc/rc.d/rc.local,如下:

[root@localhost ~]# vim /etc/rc.d/rc.local

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/etc/init.d/mysqld start

保存后,重启系统验证是否可以随系统启动。

9 远程连接:

1 记得不要使用root用户进行远程连接,新建一个用户,并修改新用户的host值为 % 或指定IP地址。
  即在mysql 数据库下,user表中,host 字段。
2 服务器防护墙开放 mysql 服务端口 3306。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值