Linux安装MySQL
It has been 400 days since the last update, the content of the article may be outdated.
0. 下载安装包
下载地址:https://dev.mysql.com/downloads/mysql/
选择操作系统 Linux-Generic ,此处下载的是 mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz 。
1. 卸载系统自带的 MariaDB (如果系统附带)
bash
1 | #查询已经安装的 MariaDB 的安装包 |
2. 解压安装包
bash
1 | [root@VM-0-10-centos ~]# tar -Jxvf /root/mysql-8.0.21-linux-glibc2.12-x86_64.ta |
3. 创建MySQL用户和用户组
bash
1 | [root@VM-0-10-centos ~]# groupadd mysql |
4. 修改MySQL目录归属用户
bash
1 | [root@VM-0-10-centos ~]# cd /usr/local/mysql |
5. 创建MySQL配置文件
bash
1 | #在 /etc 目录下创建 my.cnf 文件 |
plaintext
1 | # 在文件中写入如下配置 |
创建默认文档存储目录,并修改权限
bash
1 | [root@VM-0-10-centos ~]# mkdir /var/lib/mysql |
6.安装MySQL
bash
1 | [root@VM-0-10-centos ~]#cd /usr/local/mysql |
一定要记住初始化的默认密码 ! ! !
7.复制启动脚本到资源目录
bash
1 | [root@VM-0-10-centos ~]# cp ./support-files/mysql.server /etc/init.d/mysqld |
修改 /etc/init.d/mysqld ,修改文件中 basedir 和 datadir 对应的实际目录
plaintext
1 | basedir=/usr/local/mysql |
8. 设置启动服务及开机自启
bash
1 | #增加 mysqld 服务控制脚本的执行权限 |
这表明 mysqld 服务生效, 在2,3,4,5运行级别随系统启动而自动启动,以后可以直接使用 service 命令控制 mysqld 的状态
9. 启动MySQL并加入环境变量
bash
1 | [root@VM-0-10-centos ~]# service mysqld start |
将 MySQL 的 bin 目录加入到 PATH 环境变量中
bash
1 | export PATH=$PATH:/usr/local/mysql/bin |
执行如下命令使得环境变量生效
bash
1 | [root@VM-0-10-centos ~]# source ~/.bash_profile |
10. 登陆MySQL修改默认密码
bash
1 | [root@VM-0-10-centos ~]# mysql -u root -p |
修改默认密码为12345678
plaintext
1 | mysql>alter user user() identified by "12345678"; |
11.设置远程登陆
plaintext
1 | mysql> use mysql |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.