目标

安装二进制免编译MySQL,并设置环境变量、密码

准备

版本 IP 配置 主机名
centos7.2-1511 192.168.200.40 2G-2vcpu-40G硬盘 server
  1. [root@server src]# ls
  2. mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz php-5.6.30
  3. nginx-1.17.8 php-5.6.30.tar.gz
  4. nginx-1.17.8.tar.gz
  5. [root@server src]# ll -h mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
  6. -rw-r--r-- 1 root root 385M 7 15 23:45 mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
  7. [root@server src]# pwd
  8. /usr/local/src

过程

  1. # 修改主机名
  2. [root@localhost ~]# hostnamectl set-hostname server
  3. [root@localhost ~]# su
  4. # 准备安装包
  5. [root@server ~]# ll -h mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz -rw-r--r-- 1 root root 385M Sep 11 22:34 mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
  6. # 解压
  7. [root@server ~]# tar -zxf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
  8. # 移动到指定目录
  9. [root@server ~]# mv mysql-5.6.47-linux-glibc2.12-x86_64 /usr/local/mysql
  10. # 创建MySQL启动用户,不可登录
  11. [root@server ~]# useradd -s /sbin/nologin mysql
  12. # 创建存放数据目录
  13. [root@server ~]# mkdir -p /data/mysql
  14. # 更改所属组,所属用户权限
  15. [root@server ~]# chown -R mysql:mysql /data/mysql/
  16. [root@server ~]# ls -la /data/mysql/
  17. total 0
  18. drwxr-xr-x 2 mysql mysql 6 Sep 11 22:37 .
  19. drwxr-xr-x 3 root root 18 Sep 11 22:37 ..
  20. [root@server ~]# cd /usr/local/mysql/
  21. # 安装依赖
  22. [root@server ~]# yum install -y perl-Module-Install
  23. [root@server mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/
  24. [root@server mysql]# echo $?
  25. 0
  26. [root@lnmp mysql]# cp support-files/my-default.cnf /etc/my.cnf
  27. cp: overwrite ‘/etc/my.cnf’? y
  28. [root@lnmp mysql]# vim /etc/my.cnf
  29. basedir = /usr/local/mysql
  30. datadir = /data/mysql
  31. port = 3306
  32. server_id = 29
  33. socket = /tmp/mysql.sock
  34. # 配置启动脚本
  35. [root@lnmp mysql]# cp support-files/mysql.server /etc/init.d/mysqld
  36. [root@lnmp mysql]# chmod 777 /etc/init.d/mysqld
  37. [root@lnmp mysql]# vim /etc/init.d/mysqld
  38. basedir=/usr/local/mysql
  39. datadir=/data/mysql
  40. [root@lnmp mysql]# chkconfig --add mysqld
  41. [root@lnmp mysql]# chkconfig mysql on
  42. [root@lnmp mysql]# service mysqld start
  43. Starting MySQL. SUCCESS!
  44. [root@lnmp mysql]# netstat -ntlp
  45. Active Internet connections (only servers)
  46. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  47. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1462/sshd
  48. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2605/master
  49. tcp6 0 0 :::3306 :::* LISTEN 7891/mysqld
  50. tcp6 0 0 :::22 :::* LISTEN 1462/sshd
  51. tcp6 0 0 ::1:25 :::* LISTEN 2605/master
  52. # 配置环境变量
  53. [root@server mysql]# vim /etc/profile
  54. # 最后一行添加
  55. export PATH=$PATH:/usr/lcoal/mysql/bin
  56. [root@server mysql]# source /etc/profile
  57. [root@server mysql]# mysqladmin -uroot password '000000'
  58. Warning: Using a password on the command line interface can be insecure.
  59. [root@server mysql]# mysql -uroot -p000000
  60. Warning: Using a password on the command line interface can be insecure.
  61. Welcome to the MySQL monitor. Commands end with ; or \g.
  62. Your MySQL connection id is 2
  63. Server version: 5.6.47 MySQL Community Server (GPL)
  64. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  65. Oracle is a registered trademark of Oracle Corporation and/or its
  66. affiliates. Other names may be trademarks of their respective
  67. owners.
  68. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  69. mysql> Ctrl-C -- exit!
  70. Aborted
  71. [root@server mysql]#
  72. 到这里二进制免编译MySQL安装配置完成。