欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

二进制包安装MySQL

程序员文章站 2024-03-12 11:17:50
...

需求说明

使用二进制包安装MySQL

环境说明
下载MySQL的二进制包
点击下载

操作步骤
①.将下载好的二进制包解压

[[email protected] ~]# ls
anaconda-ks.cfg  mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[[email protected] ~]# tar -xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local
[[email protected] ~]# ls /usr/local/ | grep mysql
mysql-5.7.23-linux-glibc2.12-x86_64

②.为了方便使用,建立一个软连接,并将MySQL的路径写入到环境变量中

[[email protected] ~]# ln -s /usr/local/mysql-5.7.23-linux-glibc2.12-x86_64/ /usr/local/mysql
[[email protected] ~]# ll /usr/local/ | grep mysql
lrwxrwxrwx. 1 root root  47 Sep 25 05:22 mysql -> /usr/local/mysql-5.7.23-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Sep 25 05:20 mysql-5.7.23-linux-glibc2.12-x86_64
[[email protected] ~]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[[email protected] ~]# . /etc/profile.d/mysql.sh

③.建立mysql用户

[[email protected] ~]# groupadd -r -g 306 mysql
[[email protected] ~]# useradd -r -M -s /sbin/nologin -g 306 -u 306 mysql
[[email protected] ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)

④.创建mysql存放数据的目录,并修改属组和属主为mysql

[[email protected] ~]# mkdir /opt/mysql
[[email protected] ~]# chown -R mysql.mysql /opt/mysql/
[[email protected] ~]# ll /opt/mysql/ -d
drwxr-xr-x. 2 mysql mysql 6 Sep 25 05:25 /opt/mysql/

⑤.初始化数据库,并记录下随机密码,最后一排结尾即是第一次登陆的随机密码

[[email protected] ~]# mysqld --initialize --user=mysql --datadir=/opt/mysql
2018-09-25T09:32:51.537582Z 1 [Note] A temporary password is generated for [email protected]: Mdw,b0uou4,n        <-- 随机密码

⑥.修改mysql 的配置文件

[[email protected] ~]# cat > /etc/my.cnf << EOF
> [mysqld]
> datadir = /opt/mysql      //数据存放路径
> basedir = /usr/local/mysql       //数据库安装路径
> socket = /tmp/mysql.sock     //套接字文件位置
> port = 3306     //监听的端口
> pid-file = /opt/mysql/mysql.pid      //进程id文件存放路径
> user = mysql    //指定mysql的用户身份
> skip-name-resolve     //不解析DNS,需要使用ip连接授权
> EOF


⑦.配置服务启动脚本,将脚本拷贝到/etc/init.d下面方便启动

[[email protected] ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] ~]# vim /etc/init.d/mysqld 
basedir=/usr/local/mysql
datadir=/opt/mysql

⑧.启动服务修改密码即可

[[email protected] ~]# service mysqld start
Starting MySQL.Logging to '/opt/mysql/lizihan.err'.
 SUCCESS! 
[[email protected] ~]# mysql -u root -p
Enter password: 
mysql> set password=password('lzh123!');
Query OK, 0 rows affected, 1 warning (0.01 sec)
[[email protected] ~]# vim .my.cnf
[mysql]
user=root
password=lzh123!
[[email protected] ~]# mysql
mysql>