CentOS环境下安装MySQL8.0以上版本、远程授权和开机启动

3 篇文章 0 订阅
1 篇文章 0 订阅

目录

1、卸载已经安装的数据库

2、安装MySQL数据库

2.1、下载mysql软件安装包,下载地址:https://dev.mysql.com/downloads/mysql/

2.2、上传软件包到Linux系统中,并解压,执行命令:

2.3、添加软连接,执行命令:ln -s 源文件 目标文件(锦上添花的操作,可以不执行)

2.4、在当前系统中添加mysql用户,执行命令:

2.5、初始化数据库

2.6、验证安装是否成功

2.6.4 修改mysql数据库密码,执行命令:

3、授权远程连接(重点)

4、开放防火墙端口:

4.1、查看启停防火墙状态 

4.2、开放指定端口:

4.3、重启防火墙:

4.4、查看端口号

5、设置mysql开机启动

5.1、将服务文件复制一份到/etc/init.d下,并重命名为mysql或者mysqld,执行命令:

5.2、对文件赋予执行权限,执行命令:

5.3、增加mysql服务

5.4、查询mysqld服务情况

5.5、重启服务器验证:reboot


MySQL8.0以上版本安装对比低版本发生了一些细微的变化,具体细节在安装步骤中会特别说明,从安装到远程连接总共需要操作以下几个步骤:

 

1、卸载已经安装的数据库

1.1、检查linux是否安装了mariadb数据库,新安装的centos系统会自带mariadb数据库,mariadb数据库是MySQL的分支,执行命令:

yum list installed | grep mariadb

1.2、若linux中安装了mariadb数据库,先卸载掉,mariadb数据库可能与安装mysql发生冲突,执行命令:

yum -y remove mariadb*.*

(其中mariadb*.*是第1.1步搜索出来的mariadb软件包,不同机器可能不一样)

2、安装MySQL数据库

2.1、下载mysql软件安装包,下载地址:https://dev.mysql.com/downloads/mysql/

2.1.1 选择软件包,英文不好的建议浏览器安装自动翻译插件,谷歌自带的翻译插件就很好用。本人习惯用.tar格式或者.tar.xz格式的软件安装包安装软件,所以在此选择了Linux-Genetic操作系统。

2.1.2 选择合适的操作系统版本的软件压缩包

我的电脑操作系统是64位的,所以选择的是mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz。

2.2、上传软件包到Linux系统中,并解压,执行命令:

xz -d mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz

得到的是mysql-8.0.19-linux-glibc2.12-x86_64.tar软件包,这个命令是将.tar.xz格式的文件先转为.tar格式的文件。再执行命令:

tar -xvf mysql-8.0.19-linux-glibc2.12-x86_64.tar -C /usr/local

其中,-C /usr/local 是指定解压到哪个目录下去,如果不加这个参数,则解压到当前所在的目录,此时压缩格式的软件安装包完全解压。切换目录到解压后的目录再次执行命令:

mv mysql-8.0.19-linux-glibc2.12-x86_64 mysql-8.0.19

将文件夹重命名,建议保留版本号。

2.3、添加软连接,执行命令:ln -s 源文件 目标文件(锦上添花的操作,可以不执行)

ln -s /usr/local/mysql-8.0.19/ /mysql

该命令是在根目录下创建mysql指向/usr/local/mysql8.0.19/的软连接,linux中的软连接就类似于Windows中的快捷方式。

2.4、在当前系统中添加mysql用户,执行命令:

useradd mysql 或者 adduser mysql

具体是使用useradd命令还是使用adduser命令,结合操作系统根据个人喜好来选择,useradd命令和adduser命令的区别参考:https://blog.csdn.net/beitiandijun/article/details/41678251

2.5、初始化数据库

2.5.1 在mysql目录下创建data文件夹,执行命令:mkdir data

2.5.2 切换到mysql/bin目录下执行命令:

./mysqld --initialize --user=mysql --datadir=/usr/local/mysql-8.0.19/data --basedir=/usr/local/mysql-8.0.19

在执行上面命令后,会生成一个临时的mysql数据库root用户的密码,类似于以下这行内容:

[Note] A temporary password is generated for root@localhost: sol%rzWwo5H(

其中,root@localhost: 后面跟的是mysql数据库登录的临时密码,各人安装生成的临时密码不一样,请先拷贝出来记住,后续第一次登录mysql需要使用。

2.5.3 切换到mysql/bin目录下执行命令:

./mysql_ssl_rsa_setup --datadir=/usr/local/mysql-8.0.19/data

2.6、验证安装是否成功

2.6.1 启动数据库服务:切换到mysql/bin目录下执行命令./mysqld_safe &后台启动mysql服务。

2.6.2 登录数据库:切换到mysql/bin目录下执行命令./mysql -uroot -p,输入2.5.2步生成的临时密码,进入数据库。

2.6.3 、执行sql语句 show databases; 第一次使用将会提示修改mysql的root用户密码:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

2.6.4 修改mysql数据库密码,执行命令:

alter user 'root'@'localhost' identified by '123456';

密码修改之后退出数据库,执行命令exit,然后重新进入数据库,这次需要输入的密码是刚设置的123456。

3、授权远程连接(重点

此操作与低版本的有区别,低版本的只要分别执行以下命令一般就可以解决远程连接权限问题:

mysql> grant all privileges on *.* to root@'%' identified by '123456';
mysql> flush privileges; 

8.0以上的版本,需要执行以下命令:

第一步:进入mysql客户端
在mysql的bin目录下执行命令:./mysql -uroot -p
输入密码:
第二步:查看mysql默认数据库(可以不做这步)
mysql> show databases;
这步mysql会打印如下信息表:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
第三步:选择mysql,进入数据库名为mysql的数据库
mysql> use mysql;
这步mysql会打印以下信息:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
第四步:开放root用户的权限
mysql> update user set Host="%" where User="root";
第五步:刷新权限
mysql> flush privileges;
第六步:还原密码验证插件,将MySQL8的密码认证插件由caching_sha2_password更换成mysql_native_password
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
第七步:刷新权限
mysql> flush privileges;

4、开放防火墙端口:

4.1、查看启停防火墙状态 

systemctl [参数] firewalld
其中,参数可以是:    status---查看防火墙状态
                   start----启动防火墙
                   stop-----关闭防火墙

4.2、开放指定端口:

firewall-cmd --zone=public --add-port=3306/tcp --permanent
命令含义:
        --zone #作用域
        --add-port=3306/tcp  #添加端口,格式为:端口/通讯协议
        --permanent  #永久生效,没有此参数重启后失效

4.3、重启防火墙:

firewall-cmd --reload

4.4、查看端口号

netstat -ntlp   //查看当前所有tcp端口·

netstat -ntulp |grep 3306   //查看所有3306端口使用情况·

5、设置mysql开机启动

5.1、将服务文件复制一份到/etc/init.d下,并重命名为mysql或者mysqld,执行命令:

cp /mysql/support-files/mysql.server /etc/init.d/mysql

5.2、对文件赋予执行权限,执行命令:

chmod +x /etc/init.d/mysql 或 chmod 777 /etc/init.d/mysql

5.3、增加mysql服务

执行命令:chkconfig --add mysql

5.4、查询mysqld服务情况

执行命令:chkconfig --list mysql,mysql服务运行状态如下:

mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off

默认的运行级别为2,3,4,5为on,如果3,4,5 为off:则执行命令:chkconfig --level 345 mysql on

5.5、重启服务器验证:reboot

重启之后,发现服务启动了,但是mysql没有启动,经过排查,找到原因是启动文件没有修改配置,因为本人的basedir和datadir都不是mysql默认的/usr/local/mysql和/usr/local/mysql/data。根据配置文件里面的提示有四种方法可以解决,提示内容如下:

# If you install MySQL on some other places than /usr/local/mysql, then you
# have to do one of the following things for this script to work:
#
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
#   [mysqld]
#   basedir=<path-to-mysql-installation-directory>
# - Add the above to any other configuration file (for example ~/.my.ini)
#   and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
#   below.
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

方法1:- Run this script from within the MySQL installation directory。在mysql安装目录内运行。

方法2:- Create a /etc/my.cnf file with the following information:创建my.cnf文件

[mysqld]

basedir=<path-to-mysql-installation-directory>
方法3:- Add the above to any other configuration file (for example ~/.my.ini) and copy my_print_defaults to /usr/bin

将方法2中的内容复制到别的文件中(例如~/.my.ini),并且把mysql/bin目录下的my_print_defaults文件复制到 /usr/bin目录下。

方法4:- Add the path to the mysql-installation-directory to the basedir variable below.修改下面的dasedir变量,将mysql的路径赋值给下面的basedir。(此方法即下述方法)。

编辑/etc/init.d/目录下的mysql文件,将其中的basedir=和datadir=改为basedir=/mysql或者/usr/local/mysql-8.0.19和datadir=mysql/data或者/usr/local/mysql-8.0.19/data,因为我在根目录下加了mysql的软连接,所以可以直接改为/mysql或者/mysql/data,如图所示:

修改前:

修改后:

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LINUX重启MYSQL的命令 . 分类: Linux 2010-06-25 10:21 6367人阅读 评论(0) 收藏 举报 如何启动/停止/重启MySQL 一、启动方式 1、使用 service 启动:service mysqld start 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start 3、使用 safe_mysqld 启动:safe_mysqld& 二、停止 1、使用 service 启动:service mysqld stop 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld stop 3、 mysqladmin shutdown 三、重启 1、使用 service 启动:service mysqld restart 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld restart 提问 编辑摘要 如何启动/停止/重启MySQL 一、启动方式 1、使用 service 启动:service mysqld start 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start 3、使用 safe_mysqld 启动:safe_mysqld& 二、停止 1、使用 service 启动:service mysqld stop 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld stop 3、 mysqladmin shutdown 三、重启 1、使用 service 启动:service mysqld restart 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld restart 刚开始学 mysql时都是用redhat自带的。启动是什么 /rc.d/init.d/ start 这很简单,但是后来越学越多,系统自带的 mysql,有的是版本太低,有的是与 自己想要装的web服务需要的低版本mysql 后来自己学着以tar的方式安装 mysql,我的mysql装在/usr/local/mysql目录下启 动碰到过很多问题。最常见的是: ERROR 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111) 解决办法: [root@test mysql]# /usr/local/mysql/bin/mysqladmin -u root / > -S /var/lib/mysql/mysql.sock password 'your.passwd' 或者做个连接 ln -s /var/lib/mysql/mysql.sock /tmp 其实提示找不到 /tmp/mysql.sock有时也并不是/tmp目录下没这个文件,是启动 命令不对,我碰到过 常见的几种启动方式,自己也不是记得很清楚,如果你确定tmp下有mysql.sock这 个文件不妨试试另外的几个命令 /usr/local/mysql/bin/mysql -u root -p /usr/local/mysql/bin/mysqld --user=mysql& /usr/local/mysql/bin/mysqld --user=root& /usr/local/mysql/bin/mysqld_safe --user=root& /usr/local/mysql/bin/mysqld_safe --user=mysql& /usr/local/mysql/bin/safe_mysqld--uer=root&(注意 safe_mysqld与mysqld_safe是不同的,&表示mysql在后台运行)我的就会报错了 STOPPING server from pid file /usr/local/mysql/data/localhost.localdomain.pid 060304 11:46:21 mysqld ended 这是权限问题,我的mysql目录属于root用户,也属于root群组,改用 mysqld_safe启动就没问题了, 大家只要注意这几个 mysql,safe_mysqld,mysqld_safe,mysqld,mysqladmin.多试 几次 其实有时mysql已经正常启动了,查看mysql是否启动命令 ps -aux | grep mysqld 会看到如下类似内容 mysql 6394 0.0 1.5 10528 992 pts/3 S 16:16 0:00 /usr/local/mysql/ mysql 6395 0.0 1.5 10528 992 pts/3 S 16:16 0:00 /usr/local/mysql/ mysql 6396 0.0 1.5 10528 992 pts/3 S 16:16 0:00 /usr/local/mysql/ root 6422 0.0 1.1 2408 732 pts/3 S 16:20 0:00 grep mysql 查看mysql是否在监听端口命令 netstat -tl | grep mysql 会看到如下类似内容 tcp 0 0 *:mysql *:* LISTEN

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值