CentOS7下安装MySQL

CentOS7下安装MySQL

第一步:下载MySQL

[root@tencent-centos1 opt]# 
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

第二步:解压下载到MySQL压缩包

1、解压压缩包。
命令模板

tar -zxvf 压缩包所在目录 -C 压缩后希望放置的目录

例如博主是将压缩包放在了/opt/software目录下,并解压到/opt下。

[root@VM_161_76_centos opt]# tar -zxvf software/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C /opt/

2、可以创建一个软链接

[root@VM_161_76_centos opt]# ln -s mysql-5.7.17-linux-glibc2.5-x86_64/ /opt/mysql

3、在解压后的目录下创建一个data目录

[root@VM_161_76_centos opt]# cd mysql
[root@VM_161_76_centos mysql]# mkdir data

第三步:配置相关文件

1、第一个配置文件

[root@VM_161_76_centos opt]# cp mysql/support-files/my-default.cnf /etc/my.cnf

有的support-files下没有my-default.cnf文件。那就自己创建一个。
在文件中加入以下内容

[mysql]
default-character-set=utf8

[mysqld]
basedir = /opt/mysql
datadir = /opt/mysql/data
port = 3306
default-storage-engine=INNODB
character_set_server=utf8

2、第二个配置文件

[root@VM_161_76_centos opt]# cp mysql/support-files/mysql.server /etc/init.d/mysql 

etc/init.d/mysql 文件中配置以下内容

basedir=/opt/mysql
datadir=/opt/mysql/data

3、添加用户组与用户mysql

[root@VM_161_76_centos opt]# groupadd mysql
[root@VM_161_76_centos opt]# useradd mysql -g mysql -M -s /sbin/nologin

备注:

-g:指定新用户所属组
-M:不建立根目录
-s:定义其使用的shell,/sbin/nologin表示用户不能登陆系统

以下可以查看是否创建成功:

[root@VM_161_76_centos opt]# cat /etc/group | grep mysql
mysql:x:1002:
[root@VM_161_76_centos opt]# cat /etc/passwd | grep mysql
mysql:x:995:1002::/home/mysql:/bin/bash

输入命令后出现了类似的就表示创建成功了。
创建完后,改变解压包的所属组和所属用户

[root@VM_161_76_centos opt]# chown -R mysql:mysql mysql-5.7.17-linux-glibc2.5-x86_64/

4、配置环境变量

[root@VM_161_76_centos opt]# vim /etc/profile

在打开的文件中加入以下内容保存

export MYSQL=/opt/mysql
export PATH=$MYSQL/bin:$PATH

使配置生效,执行

[root@VM_161_76_centos opt]# source /etc/profile

第四步:初始化及启动服务

1、初始化数据库

[root@VM_161_76_centos mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
2018-03-18T06:57:26.441070Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-03-18T06:57:26.441136Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2018-03-18T06:57:26.441141Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2018-03-18T06:57:27.427075Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-03-18T06:57:27.633941Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-03-18T06:57:27.728128Z 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: 9ebb3fa6-2a79-11e8-84cd-525400ccdcec.
2018-03-18T06:57:27.737320Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-03-18T06:57:27.752672Z 1 [Note] A temporary password is generated for root@localhost: 7j)Nu!EatnIb

最后出现的一个临时密码一定要保存好,例如博主的当时是这个7j)Nu!EatnIb

2、启动MySQL

[root@VM_161_76_centos mysql]# ./bin/mysqld_safe --user=mysql &

输入bg,以后台模式运行。查看是否有相应进程

[root@VM_161_76_centos opt]# ps -ef|grep mysql
root     30379 24544  0 15:16 pts/0    00:00:00 /bin/sh ./bin/mysqld_safe
mysql    30525 30379  0 15:16 pts/0    00:00:00 ./bin/mysqld --basedir=/opt/mysql --datadir=/opt/mysql/data --plugin-dir=/opt/mysql/lib/plugin --user=mysql --log-error=/opt/mysql/data/VM_161_76_centos.err --pid-file=/opt/mysql/data/VM_161_76_centos.pid --port=3306
root     32295 24544  0 15:54 pts/0    00:00:00 grep --color=auto mysql

进入MySQL:
输入密码的时候就输入之前的临时密码

[root@VM_161_76_centos opt]# mysql -uroot -p
Enter password: 

3、成功进入后,修改密码

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.17 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> set password = password('123456');

4、给出远程访问权限

mysql> grant all privileges on *.* to root@'%' identified by '你的密码';

注意:这里给出了远程访问权限,但如果配置安全组却没有开放3306端口,也是无法访问的。
至此安装完成!

小tips:
因为没有配置开机自动启动,重启机器后,需要启动MySQL,可以使用root用户执行下面命令
前提是配置了path

[root@tencent-centos1 mysql]# systemctl start mysql

备注:

中间有可能会出现一些问题,小小记录以下。
**1、**例如首次进入MySQL时,出现以下问题。网上的解决方法很多,MySQL默认是去/tmp下找mysql.sock文件。

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

解决办法:
如果my.cnf文件没有配置socket=自己指定的目录类似的值,kill掉所有关于mysql的进程,进入MySQL的安装目录,重新执行以下命令

[root@VM_161_76_centos mysql]# ./bin/mysqld_safe&

然后再进入MySQL。还是输入之前的临时密码。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值