找回密码
 立即注册
首页 业界区 安全 Llinux安装MySQL教程

Llinux安装MySQL教程

少屠 2025-6-1 20:37:28
本文环境:CentOS7、mysql-8.0.26
使用FinalShell或者XShell连接Linux服务器,然后把网上下的MySQL的tar包传进去,我用的是mysql-8.0.26-1.el7.x86_64.rpm-bundle.tar
进入服务器以后创建一个临时文件夹用来解压文件夹
  1. cd /tmp
  2. mkdir /tempData
  3. cd /tempData
  4. # 把tar包传到/tmp/tempData以后,在这里创建一个文件夹然后解压tar包
  5. mkdir mysql
  6. # 下面的tar包记得替换成自己的,这里会把tar包解压到刚创建的mysql文件夹里
  7. tar -xvf mysql-8.0.26-1.el7.x86_64.rpm-bundle.tar -C mysql
  8. cd mysql
  9. # 现在的mysql文件夹里有一堆rpm文件,这里需要按照一定的顺序安装才行
  10. rpm -ivh mysql-community-common-8.0.26-1.el7.x86_64.rpm
  11. rpm -ivh mysql-community-client-plugins-8.0.26-1.el7.x86_64.rpm
  12. # 下面这行执行如果报错,需要执行【rpm -e mariadb-libs --nodeps】命令然后再执行下面这行命令
  13. rpm -ivh mysql-community-libs-8.0.26-1.el7.x86_64.rpm
  14. rpm -ivh mysql-community-libs-compat-8.0.26-1.el7.x86_64.rpm
  15. # 执行下面的命令,安装openssl-devel,但是现在的CentOS7仓库据说是不提供支持了,可能会报错,报错信息和解决方案在文章下面,把仓库源换成阿里的就行(其他能用的仓库也可以)
  16. yum install openssl-devel
  17. rpm -ivh  mysql-community-devel-8.0.26-1.el7.x86_64.rpm
  18. rpm -ivh mysql-community-client-8.0.26-1.el7.x86_64.rpm
  19. rpm -ivh  mysql-community-server-8.0.26-1.el7.x86_64.rpm
  20. # 总结一下安装顺序
  21. common -> client-plugins -> libs -> libs-compat -> devel -> client -> server
  22. # 其中安装libs可能会报依赖错误,这时候执行一次【rpm -e mariadb-libs --nodeps】再安装libs就好
  23. # 另外安装devel之前要先安装openssl-devel
  24. # 上面的rpm安装完毕没问题了就可以启动mysql了
  25. systemctl start mysqld
  26. # 在windows安装MySQL的时候,大家可能碰到过设置密码的时候,但是linux安装rpm的时候是没有设置密码这个流程的,系统会随机生成一个文件,我们通过下列命令查找生成的随机密码
  27. grep 'temporary password' /var/log/mysqld.log
  28. # 这时候我这边显示这样的信息
  29. 2025-03-19T15:23:19.674016Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: =qx_Qtd#%3c7
  30. # 密码就是=qx_Qtd#%3c7
  31. # 知道密码以后可以连接数据库了
  32. mysql -u root -p=qx_Qtd#%3c7
  33. # 如果此时你尝试创建数据库
  34. create database test;
  35. # 你会直接收到一个错误提示:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
  36. # 解决方法很简单,就是把初始化的密码改一下,然后就可以正常使用mysql了
  37. # 但是mysql对密码有校验规则和长度限制,如果你想要使用简单的密码比如123456,那就先降低规则限制
  38. # 在mysql客户端执行下面语句
  39. # 降低规则限制
  40. set global validate_password.policy=0;
  41. # 降低长度限制
  42. set global validate_password.length=6;
  43. # 设置新密码
  44. alter user 'root'@'localhost' identified by '123456';
  45. # 然后就可以正常创建数据库和表了
  46. # 这时候只能本机连接使用mysql,可以查看user信息,都是被限制在了localhost
  47. select * from mysql.user\G;
  48. # 可以自己创建一个叫root的用户,%代表任何主机都可以访问,密码是123456
  49. create user 'root'@'%' identified by '123456';
  50. # 刚创建的用户是没有任何权限的,需要分配权限,可以把最高权限all分配给'root'@'%',前面的on *.*代表在任何数据库任何表下都是有all权限,这里可以指定具体的库和表
  51. grant all on *.* to 'root'@'%'
  52. # 如果你想要让其他机器连接数据库,那就得先把端口开放,默认端口3306是不开放的。
  53. # 配置防火墙开放端口3306
  54. sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent
  55. # 重新加载防火墙配置
  56. sudo firewall-cmd --reload
  57. # 当你想远程连接数据库的时候,使用下列命令
  58. mysql -h192.168.179.132 -uroot -p123456
  59. # -h指的是host,输入服务器ip
  60. # -u指的是user,输入用户名
  61. # -p指的是password,输入密码
  62. # 如果数据库不是运行在3306端口的,需要指定-P,这里是大写的P,指的是Port端口号
  63. # 到这步已经可以正常使用数据库了,你可以用选择你喜欢的各种可视化工具
复制代码
执行【yum install openssl-devel】发生的报错如下
  1. Loaded plugins: fastestmirror, langpacks
  2. Loading mirror speeds from cached hostfile
  3. Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
  4. 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
  5. One of the configured repositories failed (Unknown),
  6. and yum doesn't have enough cached data to continue. At this point the only
  7. safe thing yum can do is fail. There are a few ways to work "fix" this:
  8.      1. Contact the upstream for the repository and get them to fix the problem.
  9.      2. Reconfigure the baseurl/etc. for the repository, to point to a working
  10.         upstream. This is most often useful if you are using a newer
  11.         distribution release than is supported by the repository (and the
  12.         packages for the previous distribution release still work).
  13.      3. Run the command with the repository temporarily disabled
  14.             yum --disablerepo=<repoid> ...
  15.      4. Disable the repository permanently, so yum won't use it by default. Yum
  16.         will then just ignore the repository until you permanently enable it
  17.         again or use --enablerepo for temporary usage:
  18.             yum-config-manager --disable <repoid>
  19.         or
  20.             subscription-manager repos --disable=<repoid>
  21.      5. Configure the failing repository to be skipped, if it is unavailable.
  22.         Note that yum will try to contact the repo. when it runs most commands,
  23.         so will have to try and fail each time (and thus. yum will be be much
  24.         slower). If it is a very temporary problem though, this is often a nice
  25.         compromise:
  26.             yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
  27. Cannot find a valid baseurl for repo: base/7/x86_64
复制代码
解决方法如下(注意:阿里云的镜像在本文发布2025/03/20时依然能用,但是不确保以后这个仓库地址不会变动或者遗弃)
  1. # 下载阿里云的配置文件
  2. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册