centos7部署mysql-5.7

2021-06-08 18:15:39 浏览数 (1)

一.环境声明

代码语言:javascript复制
[mysql-Server]    
主机名 = host-1    
系统 = centos-7.3    
地址 = 1.1.1.1    
软件 = mysql-5.7 3306

使用二进制包部署会下载比较大,大约500M左右,而源码包就几十M。但使用二进制包不用编译,部署较快,相比于yum可以自定义目录,方便维护。

二.程序部署

1.安装依赖 yum -y install autoconf libaio bison ncurses-devel

2.创建用户 groupadd mysql useradd -g mysql -s /sbin/nologin mysql

3.清理centos7 默认自带数据库,一定要清理干净 yum -y remove mariadb mariadb-server

4.下载二进制包 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz tar -xf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

移动过去,后面是存放路径 mv mysql-5.7.21-linux-glibc2.12-x86_64 /usr/local/mysql mkdir /var/log/mysql mkdir /usr/local/mysql/data

mysql即使root启动还是mysql用户运行的,所以要给权限 chown -R mysql:mysql /usr/local/mysql chown -R mysql:mysql /var/log/mysql

5.进去删除所有行,添加新的 vim /etc/my.cnf

  1. [client]
  2. port = 3306
  3. socket = /tmp/mysql.sock
  4. [mysqld]
  5. character_set_server=utf8
  6. init_connect='SET NAMES utf8'
  7. basedir=/usr/local/mysql
  8. datadir=/usr/local/mysql/data
  9. socket=/tmp/mysql.sock
  10. log-error=/var/log/mysqld.log
  11. pid-file=/usr/local/mysql/mysqld.pid

6.初始化 cd /usr/local/mysql/bin

初始化数据库,没有反应则正确,这里可以自定义目录 /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US

7.加入systemctl控制 vim /usr/lib/systemd/system/mysql.service

  1. [Unit]
  2. Description=mysql
  3. After=network.target remote-fs.target nss-lookup.target
  4. [Service]
  5. Type=forking
  6. ExecStart=/usr/local/mysql/support-files/mysql.server start
  7. ExecReload=/usr/local/mysql/support-files/mysql.server restart
  8. ExecStop=/usr/local/mysql/support-files/mysql.server stop
  9. PrivateTmp=true
  10. [Install]
  11. WantedBy=multi-user.target

8.设置环境变量 echo 'PATH=$PATH':/usr/local/mysql/bin >> /etc/profile source /etc/profile

9.启动 systemctl daemon-reload systemctl start mysql systemctl enable mysql

三.更改初始密码

1.查看密码 cat /var/log/mysqld.log 最后一行:root@localhost: 这里就是初始密码

需要进入数据库用如下命令修改密码才能让数据库正常使用 mysql -uroot -p ALTER USER USER() IDENTIFIED BY '123456';

0 人点赞