目标

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

过程

  1. # 修改主机名
  2. [root@localhost ~]# hostnamectl set-hostname server
  3. [root@localhost ~]# su -
  4. 上一次登录:二 9 14 08:47:17 CST 2021tty1
  5. [root@server ~]#
  6. # 上传安装包并解压
  7. [root@server ~]# ls
  8. anaconda-ks.cfg mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
  9. [root@server ~]#
  10. [root@server ~]# tar -zxvf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
  11. # 移动到指定目录
  12. [root@server ~]# mv mysql-5.6.47-linux-glibc2.12-x86_64 /usr/local/mysql/
  13. # 创建MySQL启动用户,不可登录
  14. [root@server ~]# useradd -s /sbin/nologin mysql
  15. # 创建存放数据目录
  16. [root@server ~]# mkdir -p /data/mysql
  17. # 更改所属组,所属用户权限
  18. [root@server ~]# chown -R mysql:mysql /data/mysql/
  19. [root@server ~]# ls -la /data/mysql/
  20. 总用量 0
  21. drwxr-xr-x. 2 mysql mysql 6 9 14 08:38 .
  22. drwxr-xr-x. 3 root root 19 9 14 08:38 ..
  23. [root@server ~]# cd /usr/local/mysql/
  24. # 安装依赖
  25. [root@localhost mysql]# yum install -y perl-Module-Install
  26. [root@server mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/
  27. [root@server mysql]# echo $?
  28. 0
  29. [root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf
  30. cp:是否覆盖"/etc/my.cnf" y
  31. [root@localhost mysql]# vim /etc/my.cnf
  32. basedir = /usr/local/mysql
  33. datadir = /data/mysql
  34. port = 3306
  35. server_id = 29
  36. socket = /tmp/mysql.sock
  37. [root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
  38. [root@localhost mysql]# chmod 777 /etc/init.d/mysqld
  39. [root@localhost mysql]# vim /etc/init.d/mysqld
  40. basedir=/usr/local/mysql
  41. datadir=/data/mysql
  42. [root@localhost mysql]# chkconfig --add mysqld
  43. [root@localhost mysql]# chkconfig mysqld on
  44. [root@localhost mysql]# service mysqld start
  45. Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'.
  46. SUCCESS!
  47. [root@localhost mysql]# netstat -ntlp
  48. Active Internet connections (only servers)
  49. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  50. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 941/sshd
  51. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1189/master
  52. tcp6 0 0 :::22 :::* LISTEN 941/sshd
  53. tcp6 0 0 ::1:25 :::* LISTEN 1189/master
  54. tcp6 0 0 :::3306 :::* LISTEN 2210/mysqld
  55. # 配置环境变量
  56. [root@server mysql]# vim /etc/profile
  57. # 最后一行添加
  58. export PATH=$PATH:/usr/local/mysql/bin
  59. [root@server mysql]# source /etc/profile
  60. [root@server mysql]# mysqladmin -uroot password '123456'

如果出错先设置密码

  1. # 在知道或者是没有密码的情况下配置密码
  2. # 二进制免编译安装的MySQL是没有密码的,首先设置一个密码
  3. [root@lnmp ~]# mysqladmin -uroot password '123456'
  4. # 在不知道密码的情况下重置密码
  5. [root@lnmp ~]# vim /etc/my.cnf
  6. # 在mysqld下添加skip-grant这一行
  7. [mysqld]
  8. skip-grant # 忽略用户认证
  9. # 重启服务生效
  10. [root@lnmp ~]# service mysqld restart
  11. Shutting down MySQL.. SUCCESS!
  12. Starting MySQL. SUCCESS!
  13. # 免密登录
  14. [root@server ~]# mysql -uroot
  15. Welcome to the MySQL monitor. Commands end with ; or \g.
  16. Your MySQL connection id is 1
  17. Server version: 5.6.47 MySQL Community Server (GPL)
  18. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  19. Oracle is a registered trademark of Oracle Corporation and/or its
  20. affiliates. Other names may be trademarks of their respective
  21. owners.
  22. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  23. mysql> mysql> show databases;
  24. +--------------------+
  25. | Database |
  26. +--------------------+
  27. | information_schema |
  28. | mysql |
  29. | performance_schema |
  30. | test |
  31. +--------------------+
  32. 4 rows in set (0.00 sec)
  33. mysql> use mysql;
  34. Reading table information for completion of table and column names
  35. You can turn off this feature to get a quicker startup with -A
  36. Database changed
  37. mysql>
  38. # 更新密码
  39. mysql> update user set password=password('123456') where user='root';
  40. Query OK, 4 rows affected (0.00 sec)
  41. Rows matched: 4 Changed: 4 Warnings: 0
  42. mysql> Ctrl-C -- exit!
  43. Aborted
  44. [root@server ~]# vim /etc/my.cnf
  45. # 去掉skip-grant字段
  46. # 重启生效
  47. [root@lnmp ~]# service mysqld restart
  48. Shutting down MySQL.. SUCCESS!
  49. Starting MySQL. SUCCESS!
  50. # 使用新密码登录
  51. [root@server ~]# mysql -uroot -p123456
  52. Warning: Using a password on the command line interface can be insecure.
  53. Welcome to the MySQL monitor. Commands end with ; or \g.
  54. Your MySQL connection id is 1
  55. Server version: 5.6.47 MySQL Community Server (GPL)
  56. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  57. Oracle is a registered trademark of Oracle Corporation and/or its
  58. affiliates. Other names may be trademarks of their respective
  59. owners.
  60. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  61. mysql>

到这里二进制免编译MySQL安装配置完成。