Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
Linux上安装Mysql,希望能够帮助你!!!。
以上几种方式便捷性依次增加,但通用性依次下降,比如直接下载压缩包进行解压,这种方式一般需要自己做一些额外的配置工作,但只要掌握了方法,各个平台基本都适用,YUM虽然简单,但是平台受限,网络受限,必要的时候还需要增加一些特定YUM源。
几种安装方式最好都能掌握,原则上能用简单的就用简单的:YUM>RPM>tar.gz>源码
[root@localhost ~]# rpm -qa|grep mysql #无输出说明没有安装
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.8.2003 (Core)
3. 点击如下图红框所示
4. 复制下载文件的全名:mysql80-community-release-el7-3.noarch.rpm
5. 下载和安装mysql源
[root@localhost ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
如果显示wget未找到命令,则需要先安装wget:
yum -y install wget
然后再次执行上面命令
[root@localhost ~]# sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
检查是否安装成功:
执行成功后会在/etc/yum.repos.d/目录下生成两个repo文件mysql-community.repo及 mysql-community-source.repo
[root@localhost ~]# cd /etc/yum.repos.d/ [root@localhost yum.repos.d]# ls -l 总用量 12 -rw-r--r--. 1 root root 2523 6月 16 2018 CentOS-Base.repo -rw-r--r--. 1 root root 2076 4月 25 2019 mysql-community.repo -rw-r--r--. 1 root root 2108 4月 25 2019 mysql-community-source.repo [root@localhost yum.repos.d]#
查看mysql相关资源
yum repolist enabled | grep "mysql.*-community.*"
[root@localhost ~] sudo yum-config-manager --disable mysql80-community [root@localhost ~] sudo yum-config-manager --enable mysql57-community
[root@localhost ~]# sudo yum install mysql-community-server
该命令会安装MySQL服务器 (mysql-community-server) 及其所需的依赖、相关组件,包括mysql-community-client、mysql-community-common、mysql-community-libs等。安装时间会比较长
[root@localhost ~]# systemctl start mysqld.service [root@localhost ~]# systemctl status mysqld.service
启动成功
[root@localhost ~]# systemctl stop mysqld.service
[root@localhost ~]# systemctl restart mysqld.service
初始密码
MySQL第一次启动后会创建超级管理员账号root@localhost,初始密码存储在日志文件中:
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log 2020-12-06T07:32:41.Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Nd<-p!Fxt6A8
修改默认密码
# 先进行登录 [root@localhost ~]# mysql -uroot -p Enter password: mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements #密码太弱
解决方法如下:
mysql> SHOW VARIABLES LIKE 'character%';
[mysqld] character_set_server=utf8 init-connect='SET NAMES utf8'
[root@localhost ~]# systemctl enable mysqld [root@localhost ~]# systemctl daemon-reload
允许远程登录
# firewall防火墙 systemctl status firewalld # 查看转态 firewall-cmd --state # 开启 service firewalld start # 重启 service firewalld restart # 关闭 service firewalld stop #注意分清楚linux的版本 命令会有所不同 #查看防火墙规则 firewall-cmd --list-all
[root@localhost ~]# rpm -qa|grep mariadb
如无则跳过下面一条
[root@localhost ~]# rpm -e --nodeps mariadb-server [root@localhost ~]# rpm -e --nodeps mariadb [root@localhost ~]# rpm -e --nodeps mariadb-libs
[root@localhost ~]# rpm -qa|grep mysql mysql-community-client-8.0.22-1.el7.x86_64 mysql-community-common-8.0.22-1.el7.x86_64 mysql-community-client-plugins-8.0.22-1.el7.x86_64 mysql-community-libs-compat-8.0.22-1.el7.x86_64 mysql80-community-release-el7-3.noarch mysql-community-libs-8.0.22-1.el7.x86_64 mysql-community-server-8.0.22-1.el7.x86_64 [root@localhost ~]#
[root@localhost ~]# rpm -e --nodeps xxx
1.删除旧的,操作如上
2.下载mysql对应安装包
官网下载:历史版本下载
下载好后通过xftp上传至虚拟机
或者直接在虚拟机上使用命令进行下载:
[root@localhost ~]# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.15-linux-glibc2.12-i686.tar
3.安装和配置
MySQL依赖libaio库
[root@localhost ~]# yum install libaio
4.创建mysql用户
不需要登录的一个系统账号,启动MySQL服务时会使用该账号
[root@localhost mysql]# groupadd mysql [root@localhost mysql]# useradd -r -g mysql -s /bin/false mysql
5.解压并创建链接
[root@localhost mysql]# tar zxvf /root/mysql/mysql-8.0.15-linux-glibc2.12-i686.tar.gz 或者 [root@localhost mysql]# tar -xvf /root/mysql/mysql-8.0.15-linux-glibc2.12-i686.tar 或者 [root@localhost mysql]# tar -xvJf /root/mysql/mysql-8.0.15-linux-glibc2.12-i686.tar.xz 修改文件名 [root@localhost mysql]# mv mysql-8.0.15-linux-glibc2.12-i686 mysql 将文件移动到/usr/local/ [root@localhost mysql]# cp mysql-8.0.15-linux-glibc2.12-i686.tar.xz /usr/local/
6.修改/usr/local/mysql 目录为mysql用户
cd /usr/local/mysql //进入/usr/local/mysql 目录
[root@localhost mysql]# cd /usr/local/mysql //进入/usr/local/mysql 目录 [root@localhost mysql]# chown -R mysql:mysql ./
7.mysql初始化操作
[root@localhost mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data ./bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory # 初始化报错,请执行以下命令,查看是否安装齐全 yum search libaio yum install libaio yum -y install numactl yum install libnuma yum install ld-linux.so.2 yum install libaio.so.1 yum install libnuma.so.1 yum install libstdc++.so.6 yum install libtinfo.so.5
8.启动服务
[root@localhost mysql]# cd support-files [root@localhost support-files]# ./mysql.server start Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'. .. SUCCESS!
success表示已经启动成功
9.配置mysql环境
[root@localhost ~]# vi /etc/profile
xport MYSQL_HOME=/usr/local/mysql export MYSQL_PATH=${MYSQL_HOME}/bin:${MYSQL_HOME}/lib export PATH=$PATH:/usr/local/mysql/bin
shutdown -r now 重启下服务器或者 source /etc/profile 均可使环境变量生效
10.系统配置
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql //拷贝mysql.server chmod +x /etc/init.d/mysql //添加可执行权限。 chkconfig --add mysql // 注册启动服务 chkconfig --list //查看是否添加成功
至此mysql就安装成功了
11.修改初始密码
[root@localhost ~]# mysql -uroot -p Enter password: //这里输入刚刚初始化操作时的初始密码 mysql> alter user 'root'@'localhost' identified by '你的新密码'; Query OK, 0 rows affected (0.02 sec) #设置允许远程登录 mysql> update user set user.Host='%' where user.User='root';
12.关闭防火墙
允许远程登录
# firewall防火墙 systemctl status firewalld # 查看转态 firewall-cmd --state # 开启 service firewalld start # 重启 service firewalld restart # 关闭 service firewalld stop #注意分清楚linux的版本 命令会有所不同 #查看防火墙规则 firewall-cmd --list-all
远程连接时报错
原因
mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password
解决
#更改加密方式 mysql> ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVEER; Query OK, 0 rows affected (0.01 sec) #更新用户密码 mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
已是最后文章
下一篇
已是最新文章