mysql安装参考教程

💻前言

为了简化安装和配置过程,许多Linux发行版提供了预编译的二进制MySQL安装包,这些安装包已经经过测试和验证,可以在大多数Linux系统上正常工作。通过安装这些二进制MySQL安装包,可以省去从源代码编译和安装的繁琐过程,提高安装和配置的效率。

此外,二进制MySQL安装包也包含了一些预配置的参数和选项,这些参数和选项已经经过优化和调整,可以满足大多数用户的需求。如果需要更高级的配置或定制化的选项,可以使用源代码进行编译和安装。

💻下载Linux版Mysql

官方地址:https://www.mysql.com/

下载步骤:

 

 

通过Xftp上传到Linux:

💻解压

1.我的Linux是桥接,通过Xshell连接我的另一台电脑。

 2.执行压缩命令
tar -xvf mysql-8.0.33-linux-glibc2.12-x86_64.tar  -C /usr/local
   
   

该命令用于解压缩名为mysql-8.0.33-linux-glibc2.12-x86_64.tar的压缩文件,并将其解压缩到/usr/local目录下。-x选项表示解压缩,-v选项表示在解压缩时显示详细信息,-f选项指定要解压缩的文件名,-C选项指定解压缩后的目标目录。

3.重命名

进入解压目录对mysql-8.0.33-linux-glibc2.12-x86_64重命名为 mysql


   
   
  1. [root@xiaojian ~]# cd /usr /local
  2. [root@xiaojian local]# ll
  3. total 0
  4. drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
  5. drwxr-xr-x. 2 root root 6 Jul 25 15: 29 etc
  6. drwxr-xr-x. 2 root root 6 Apr 11 2018 games
  7. drwxr-xr-x. 2 root root 6 Apr 11 2018 include
  8. drwxr-xr-x. 8 10 143 255 Mar 29 2018 jdk 1.8.0_ 171
  9. drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
  10. drwxr-xr-x. 2 root root 6 Apr 11 2018 lib 64
  11. drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
  12. drwxr-xr-x 9 root root 129 Jul 26 10: 53 mysql- 8.0.33-linux-glibc 2.12-x 86_ 64
  13. drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
  14. drwxr-xr-x. 5 root root 49 Jul 11 09: 19 share
  15. drwxr-xr-x. 2 root root 6 Apr 11 2018 src
  16. drwxr-xr-x. 9 root root 220 Jul 25 14: 54 tomcat- 8.5.91
  17. [root@xiaojian local]# mv mysql- 8.0.33-linux-glibc 2.12-x 86_ 64 mysql
  18. [root@xiaojian local]# ll
  19. total 0
  20. drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
  21. drwxr-xr-x. 2 root root 6 Jul 25 15: 29 etc
  22. drwxr-xr-x. 2 root root 6 Apr 11 2018 games
  23. drwxr-xr-x. 2 root root 6 Apr 11 2018 include
  24. drwxr-xr-x. 8 10 143 255 Mar 29 2018 jdk 1.8.0_ 171
  25. drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
  26. drwxr-xr-x. 2 root root 6 Apr 11 2018 lib 64
  27. drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
  28. drwxr-xr-x 9 root root 129 Jul 26 10: 53 mysql
  29. drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
  30. drwxr-xr-x. 5 root root 49 Jul 11 09: 19 share
  31. drwxr-xr-x. 2 root root 6 Apr 11 2018 src
  32. drwxr-xr-x. 9 root root 220 Jul 25 14: 54 tomcat- 8.5.91
  33. [root@xiaojian local]#

💻添加用户/组


   
   
  1. #进入到安装目录
  2. [root@xiaojian local]# cd /usr/local/mysql/
  3. #创建组
  4. [root@xiaojian mysql]# groupadd mysql
  5. #创建用户
  6. [root@xiaojian mysql]# useradd -r -g mysql mysql
  7. #设置/usr/local/mysql/文件夹的拥有者
  8. [root@xiaojian mysql]# chown -R mysql:mysql ./

💻初始化Mysql生成默认随机密码


   
   
  1. #创建数据库文件存放的文件夹。这个文件夹将来存放每个数据库的库文件
  2. [root@xiaojian mysql]# mkdir data
  3. #执行命令,
  4. [root@xiaojian mysql]# bin/mysqld --initialize --user=mysql --lower_case_table_names=1 --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
  5. #参数说明
  6. #--initialize:初始化MySQL服务器,包括创建数据目录和初始化系统数据库等操作。
  7. #--user=mysql:指定运行MySQL服务器的用户,这里为mysql用户。
  8. #--lower_case_table_names=1:将数据库和表名转换为小写,方便在不同操作系统间迁移。
  9. #--basedir=/usr/local/mysql:指定MySQL服务器的安装路径。
  10. #--datadir=/usr/local/mysql/data:指定MySQL服务器的数据目录。
  11. #注意:有些centos版本缺少libaio.so.1文件,就会执行不成功,使用yum install -y libaio安装
  12. #设置数据库存储文件夹拥有者
  13. [root@xiaojian mysql]# chown -R root:root ./
  14. [root@xiaojian mysql]# chown -R mysql:mysql data

 记住这个随机生成的密码(登录Mysql必须)

💻修改配置

修改Mysql配置文件

[root@xiaojian mysql]# vi /etc/my.cnf
   
   

 将里面的命令都删除掉,添加以下命令,保存并退出


   
   
  1. [mysql]
  2. # 设置mysql客户端默认字符集
  3. default-character-set=utf8
  4. [mysqld]
  5. # 设置3306端口
  6. port=3306
  7. # 设置mysql的安装目录
  8. basedir=/usr/local/mysql
  9. # 设置mysql数据库的数据的存放目录
  10. datadir=/usr/local/mysql/data
  11. # 允许最大连接数
  12. max_connections=1500
  13. # 服务端使用的字符集默认为8比特编码的latin1字符集
  14. character-set-server=utf8
  15. # 创建新表时将使用的默认存储引擎
  16. default-storage-engine=INNODB
  17. # 默认使用"mysql_native_password"插件认证
  18. default_authentication_plugin=mysql_native_password
  19. # 区分大小写,0区分,1不区分,填写这个配置前提是在initialize初始化数据库的时候加参数--lower_case_table_names=1,否则报错
  20. lower_case_table_names=1
  21. [client]
  22. # 设置mysql客户端连接服务端时默认使用的端口
  23. default-character-set=utf8

💻启动Mysql服务


   
   
  1. [root@xiaojian mysql]# cd /usr/local/mysql/support-files
  2. [root@xiaojian support-files]# ./mysql.server start
  3. #如果执行有问题
  4. [root@xiaojian support-files]# chmod -R 777 /usr/local/mysql
  5. #注意
  6. 1.该命令会将/usr/local/mysql目录及其所有子目录和文件的权限设置为777,即所有用户都可以读取、写入和执行这些目录和文件。
  7. 2.chmod命令用于修改文件或目录的访问权限。其中,-R选项表示递归修改,即连同子目录和文件一起修改。权限数字777表示所有用户都有读、写和执行权限。
  8. 3.需要注意的是,给文件或目录设置777权限可能会存在安全风险,因为这允许所有用户对文件或目录进行操作。因此,应该根据实际需要设置适当的权限。

启动服务


   
   
  1. [root@xiaojian mysql]# cd /usr/local/mysql/support-files
  2. [root@xiaojian support-files]# ./mysql.server start
  3. Starting MySQL SUCCESS!
  4. [root@xiaojian support-files]#

 停止服务


   
   
  1. [root@xiaojian support-files]# ./mysql.server stop
  2. Shutting down MySQL.. SUCCESS!
  3. [root@xiaojian support-files]#

 如果安装包支持systemd,按下方式启动服务器。


   
   
  1. 1.systemctl {start|stop|restart|status} mysqld
  2. systemctl start mysqld:启动MySQL服务器。
  3. systemctl stop mysqld:停止MySQL服务器。
  4. systemctl restart mysqld:重新启动MySQL服务器。
  5. systemctl status mysqld:查看MySQL服务器的状态,包括是否正在运行、已经运行的进程等。
  6. 2.service mysqld {start|stop|restart|status}
  7. service mysqld start:启动MySQL服务器。
  8. service mysqld stop:停止MySQL服务器。
  9. service mysqld restart:重新启动MySQL服务器。
  10. service mysqld status:查看MySQL服务器的状态,包括是否正在运行、已经运行的进程等。
  11. service mysqld reload:重载配置

💻修改Mysql登录密码

1.重新启动Mysql后查看进程

   
   
  1. #查看 Mysql 进程
  2. ps -ef|grep mysql
  3. #查看 3306 端口
  4. netstat -ano |grep "3306"
2.登录Mysql输入初始化随机生成的密码 

   
   
  1. [root@xiaojian mysql]# mysql -u root -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 9
  5. Server version: 8.0.33
  6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>

   
   
  1. #设置Mysql新密码为 'root'
  2. mysql > alter user 'root'@ 'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
  3. Query OK, 0 rows affected ( 0.00 sec)
  4. #设置允许远程登录
  5. mysql > use mysql;
  6. Reading table information for completion of table and column names
  7. You can turn off this feature to get a quicker startup with -A
  8. Database changed
  9. #允许root用户从任何主机连接到MySQL服务器。
  10. mysql > update user set user.Host = '%' where user.User = 'root';
  11. Query OK, 1 row affected ( 0.00 sec)
  12. Rows matched: 1 Changed: 1 Warnings: 0
  13. #刷新权限
  14. mysql > flush privileges;
  15. Query OK, 0 rows affected ( 0.01 sec)
  16. #退出
  17. mysql > exit;

3.设置Mysql 开机自启

   
   
  1. #将Mysql添加到系统进程中
  2. [root @xiaojian mysql]# cp /usr / local /mysql /support -files /mysql.server /etc /init.d /mysqld
  3. [root @xiaojian mysql]# chown 777 /etc /my.cnf
  4. [root @xiaojian mysql]# chmod +x /etc /init.d /mysqld
  5. [root @xiaojian mysql]# systemctl enable mysqld
  6. #重新启动后(可测试Linux开机自启Mysql)
  7. [root @xiaojian mysql]# reboot

💻查看防火墙开放端口


   
   
  1. #使用 firewall-cmd --list-all 命令可以列出当前防火墙的所有规则和状态。这将显示公共区域、信任区域和排除区域的规则,以及每个区域中已打开的端口和接受连接的端口。
  2. [root@xiaojian mysql]# firewall-cmd --list-all
  3. #在防火墙中将3306端口开放 使用 firewall-cmd --zone=public --add-port=3306/tcp --permanent 命令想要在防火墙公共区域上永久添加3306/tcp端口。 注意:--permanent为永久生效,没有此参数 服务器重启后配置失效
  4. [root@xiaojian mysql]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
  5. #使用 firewall-cmd --reload 命令来重新加载防火墙配置。这个命令会重新读取防火墙配置文件,并将任何已更改或新添加的规则应用于防火墙。
  6. [root@xiaojian mysql]# firewall-cmd --reload

   
   
  1. #查看当前所有tcp端口
  2. netstat -lptn
  3. #查看所有3306端口使用情况
  4. netstat -lptn |grep 3306

 附属

生产环境必须开启防火墙保证服务器的数据安全,所以需要开启防火墙并且开启对应的服务端口。

使用iptables工具配置Linux防火墙的简单步骤:

1.确认已安装iptables防火墙工具。

yum install iptables
   
   

2.配置防火墙规则,例如允许或拒绝特定IP地址、端口或协议的流量。以下是一个允许IP地址为192.168.0.100的TCP流量通过端口22的示例:

iptables -A INPUT -s 192.168.0.100 -p tcp --dport 22 -j ACCEPT
   
   

3.保存防火墙规则,以便在系统重启后也生效。可以使用以下命令保存:

iptables-save > /etc/sysconfig/iptables
   
   

4.确保防火墙规则在系统启动时自动加载,可以使用以下命令:


   
   
  1. systemctl enable iptables  
  2. systemctl start iptables

以上是配置Linux防火墙的基本步骤,可以根据您的需求和配置文件来配置更复杂的规则。同时,也可以使用其他工具,如firewalld来配置和管理防火墙,它提供了更简单的界面和更快的速度。

💻防火墙配置(摘录)


   
   
  1. 1.firewall 防火墙
  2. 基本使用
  3. # 启动
  4. systemctl start firewalld.service
  5. # 停止
  6. systemctl stop firewalld.service
  7. # 重启
  8. systemctl restart firewalld.service
  9. # 查看状态
  10. systemctl status firewalld.service
  11. # 启用, 开机自启服务
  12. systemctl enable firewalld.service
  13. # 禁用, 开机自启服务
  14. systemctl disable firewalld.service
  15. # 查看是否开机启动
  16. systemctl is-enabled firewalld.service
  17. # 查看已启动的服务列表
  18. systemctl list-unit-files | grep enabled
  19. # 查看启动失败的服务列表
  20. systemctl --failed
  21. 命令后 .service 不加也可以
  22. 基本配置 firewalld-cmd
  23. # 查看版本
  24. firewall-cmd --version
  25. # 查看帮助
  26. firewall-cmd --help
  27. # 显示状态
  28. firewall-cmd --state
  29. # 开发一个端口
  30. firewall-cmd --zone=public --permanent --add-port=端口/tcp
  31. 参数:--permanent永久生效,没有此参数重启后失效 --add-port 端口
  32. # 更新防火墙规则
  33. firewall-cmd --reload
  34. # 删除端口
  35. firewall-cmd --zone=public --permanent --remove-port=端口/tcp
  36. # 查看端口是否开放
  37. firewall-cmd --zone=public --query-port=端口/tcp
  38. # 查看所有打开的端口
  39. firewall-cmd --zone=public --list-ports
  40. # 查看防火墙规则
  41. firewall-cmd --list-all
  42. # 查看区域信息
  43. firewall-cmd --get-active-zones
  44. # 查看指定接口所属区域
  45. firewall-cmd --get-zone-of-interface=eth0
  46. # 拒绝所有包
  47. firewall-cmd --panic-on
  48. # 取消拒绝状态
  49. firewall-cmd --panic-off
  50. # 查看是否拒绝
  51. firewall-cmd --query-panic
  52. 2.iptables 防火墙
  53. 安装 iptables
  54. # 检查状态
  55. systemctl status iptables.service
  56. # 停止firewall
  57. systemctl stop firewalld.service
  58. # 禁用开机启动firewall
  59. systemctl disable firewalld.service
  60. # 安装 iptables
  61. yum install -y iptables iptables-services
  62. # 启动 iptables
  63. systemctl start iptables.service
  64. # 启用开机启动 iptables
  65. systemctl enable iptables.service
  66. 设置规则
  67. 参数:
  68. -t <>:指定要操纵的表
  69. -A:向规则链中添加条目
  70. -D:从规则链中删除条目
  71. -i:向规则链中插入条目
  72. -R:替换规则链中的条目
  73. -L:显示规则链中已有的条目
  74. -F:清除规则链中已有的条目
  75. -Z:清空规则链中的数据包计算器和字节计数器
  76. -N:创建新的用户自定义规则链
  77. -P:定义规则链中的默认目标
  78. -h:显示帮助信息
  79. -p:指定要匹配的数据包协议类型
  80. -s:指定要匹配的数据包源ip地址
  81. -j <目标>:指定要跳转的目标
  82. -i <网络接口>:指定数据包进入本机的网络接口
  83. -o <网络接口>:指定数据包要离开本机所使用的网络接口
  84. # 查看iptables现有规则
  85. iptables -L -n
  86. # 先允许所有,不然有可能会杯具
  87. iptables -P INPUT ACCEPT
  88. # 清空所有默认规则
  89. iptables -F
  90. # 清空所有自定义规则
  91. iptables -X
  92. # 所有计数器归0
  93. iptables -Z
  94. # 允许来自于lo接口的数据包(本地访问)
  95. iptables -A INPUT -i lo -j ACCEPT
  96. 开放端口
  97. iptables -A INPUT -p tcp -s IP地址 --dport 端口 -j ACCEPT
  98. # 保存规则
  99. service iptables save
  100. # 允许ping
  101. iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
  102. # 允许接受本机请求之后的返回数据 RELATED,是为FTP设置的
  103. iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  104. # 其他入站一律丢弃
  105. iptables -P INPUT DROP
  106. # 所有出站一律绿灯
  107. iptables -P OUTPUT ACCEPT
  108. # 所有转发一律丢弃
  109. iptables -P FORWARD DROP
  110. # 如果要添加内网ip信任
  111. iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
  112. # 过滤所有非以上规则的请求
  113. iptables -P INPUT DROP
  114. # 要封停一个IP,使用下面这条命令:
  115. iptables -I INPUT -s IP地址 -j DROP
  116. # 要解封一个IP,使用下面这条命令:
  117. iptables -D INPUT -s IP地址 -j DROP
  118. 常用端口
  119. # http 80
  120. # https 443
  121. # mysql 3306
  122. # mongodb 27017
  123. # postgre 5432
  124. # elasticsearch 9200
  125. # redis 6379
  126. # rabbitmq 15672, 5672
  127. # consul 8500
  128. # nacos 8848
  129. # ftp 21
  130. # ssh 22
  131. # telnet 23
  132. # smtp 25
  133. firewall 实例
  134. # 开放 mysql 端口
  135. firewall-cmd --zone=public --permanent --add-port=3306/tcp
  136. # 重新载入规则
  137. firewall-cmd --reload
  138. # 删除 mysql 端口
  139. firewall-cmd --zone=public --permanent --remove-port=3306/tcp
  140. iptables 实列
  141. 开放端口
  142. iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
  143. # 保存规则
  144. service iptables save
  145. 删除端口
  146. iptables -D INPUT -p tcp --dport 3306 -j ACCEPT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值