Linux内安装MySQL的三种方法
内容概要
- yum安装MySQL
- 二进制安装MySQL
- 源码安装MySQL
yum安装MySQL
- 进入MySQL官网
https://dev.mysql.com/
---> 1
---> 2
---> 3
---> 4
---> 5
---> 6
1.卸载软件残留
[root@localhost ~]# yum remove mariadb* -y
2.安装yum源
[root@localhost ~]# yum install wget -y
[root@localhost ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
3.安装yum源
[root@localhost ~]# yum install mysql80-community-release-el7-5.noarch.rpm -y
4.检查源
[root@localhost ~]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community 230
mysql-tools-community/x86_64 MySQL Tools Community 138
mysql80-community/x86_64 MySQL 8.0 Community Server 321
5.禁用mysql80启用mysql57
[root@localhost ~]# yum install yum-utils -y
[root@localhost ~]# yum-config-manager --disable mysql80-community
[root@localhost ~]# yum-config-manager --enable mysql57-community
6.安装MySQL
[root@localhost ~]# yum install mysql-server -y
7.启动MySQL
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# netstat -nutlp
8.获取MySQL默认密码
[root@localhost ~]# cat /var/log/mysqld.log | grep password
2022-02-25T14:56:20.008587Z 1 [Note] A temporary password is generated for root@localhost: Y.S4ikhNLe4d
9.登入MySQL
[root@localhost ~]# mysql -uroot -p'Y.S4ikhNLe4d'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
10.修改密码
mysql> alter user root@localhost identified by 'Ysln123!';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
[root@localhost ~]# mysql -uroot -p'Ysln123!'
mysql> show batabases;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'batabases' at line 1
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
二进制安装MySQL
---> 1
---> 2
---> 3
---> 4
---> 5
---> 6
---> 7
---> 8
1.卸载软件残留
[root@localhost ~]# yum remove mariadb* -y
2.下载软件包
[root@localhost ~]# yum install wget -y
[root@localhost ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
3.安装
[root@localhost ~]# tar -xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ln -s /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql-5.7.37
4.统一用户
[root@localhost mysql-5.7.37]# groupadd mysql -g 666
[root@localhost mysql-5.7.37]# useradd mysql -g 666 -u 666 -r -M -s /sbin/nologin
5.创建配置文件
[root@localhost mysql-5.7.37]# vim /etc/my.cnf
[mysqld]
# 安装目录
basedir=/usr/local/mysql-5.7.37
# 存放数据的目录
datadir=/usr/local/mysql-5.7.37/data
# 指定端口号
port=3306
# 指定Socket文件存放路径
socket=/usr/local/mysql-5.7.37/data/mysql.sock
# 指定默认的字符集编码
character-set-server=utf8
# MySQL错误日志路径
log-error=/var/log/mysqld.log
# 指定MySQL pid文件路径
pid-file=/usr/local/mysql-5.7.37/data/mysqld.pid
[mysql]
socket=/usr/local/mysql-5.7.37/data/mysql.sock
[client]
socket=/usr/local/mysql-5.7.37/data/mysql.sock
6.创建数据目录
[root@localhost mysql-5.7.37]# mkdir /usr/local/mysql-5.7.37/data
7.授权
[root@localhost mysql-5.7.37]# chown mysql.mysql -R /usr/local/mysql-5.7.37
[root@localhost mysql-5.7.37]# chown mysql.mysql -R /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64/
[root@localhost mysql-5.7.37]# touch /var/log/mysqld.log
[root@localhost mysql-5.7.37]# chown mysql.mysql -R /var/log/mysqld.log
8.安装MySQL依赖软件
[root@localhost mysql-5.7.37]# yum install ncurses-devel libaio-devel gcc gcc-c++ numactl libaio glibc cmake autoconf -y
9.初始化数据库
[root@localhost mysql-5.7.37]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-5.7.37 --datadir=/usr/local/mysql-5.7.37/data
10.注册MySQL启动服务
[root@localhost mysql-5.7.37]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql-5.7.37/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
[root@localhost mysql-5.7.37]# systemctl daemon-reload
[root@localhost mysql-5.7.37]# systemctl start mysqld
11.测试连接
[root@localhost mysql-5.7.37]# cat /var/log/mysqld.log | grep password
2022-02-25T16:10:47.165356Z 1 [Note] A temporary password is generated for root@localhost: ahrermuKP4,D
[root@localhost mysql-5.7.37]# /usr/local/mysql-5.7.37/bin/mysql -uroot -p'ahrermuKP4,D'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
12.修改默认密码
mysql> alter user root@localhost identified by 'Ysln123!';
Query OK, 0 rows affected (0.00 sec)
13.添加环境变量
[root@localhost mysql-5.7.37]# vim /etc/profile
export MYSQL_HOME=/usr/local/mysql-5.7.37
export PATH=$PATH:$MYSQL_HOME/bin
[root@localhost mysql-5.7.37]# source //etc/profile
[root@localhost mysql-5.7.37]# mysql -uroot -p'Ysln123!'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
源码安装MySQL
1.卸载软件残留
[root@localhost ~]# yum remove mariadb* -y
2.下载源代码
[root@localhost ~]# yum install wget -y
[root@localhost ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.36.tar.gz
[root@localhost ~]# tar -xf mysql-5.7.36.tar.gz
3.统一用户
[root@localhost ~]# groupadd mysql -g 666
[root@localhost ~]# useradd mysql -g 666 -u 666 -r -M -s /sbin/nologin
4.创建配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
# 安装目录
basedir=/usr/local/mysql-5.7.36
# 存放数据的目录
datadir=/usr/local/mysql-5.7.36/data
# 指定端口号
port=3306
# 指定Socket文件存放路径
socket=/usr/local/mysql-5.7.36/data/mysql.sock
# 指定默认的字符集编码
character-set-server=utf8
# MySQL错误日志路径
log-error=/var/log/mysqld.log
# 指定MySQL pid文件路径
pid-file=/usr/local/mysql-5.7.36/data/mysqld.pid
[mysql]
socket=/usr/local/mysql-5.7.36/data/mysql.sock
[client]
socket=/usr/local/mysql-5.7.36/data/mysql.sock
5.安装依赖软件
[root@localhost mysql-5.7.36]# yum install ncurses-devel libaio-devel gcc gcc-c++ glibc cmake autoconf openssl openssl-devel -y
6.打开MAC终端上传boot软件包到Linux
MacdeMacBook-Pro:~ mac$ scp /Users/mac/Downloads/boost_1_59_0.tar.gz root@192.168.15.50:~
7.安装boot
[root@localhost mysql-5.7.36]# tar -xf /root/boost_1_59_0.tar.gz
8.创建数据目录
[root@localhost mysql-5.7.36]# mkdir /usr/local/mysql-5.7.36
[root@localhost mysql-5.7.36]# chown mysql.mysql -R /usr/local/mysql-5.7.36
9.设置编译参数
[root@localhost mysql-5.7.36]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.36 \
> -DMYSQL_DATADIR=/usr/local/mysql-5.7.36/data \
> -DMYSQL_UNIX_ADDR=/usr/local/mysql-5.7.36/tmp/mysql.sock \
> -DDOWNLOAD_BOOST=1 \
> -DWITH_BOOST=/usr/local/boost_1_59_0 \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DWITH_EXTRA_CHARSETS=all \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_FEDERATED_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
> -DWITH_ZLIB=bundled \
> -DWITH_SSL=system \
> -DENABLED_LOCAL_INFILE=1 \
> -DWITH_EMBEDDED_SERVER=1 \
> -DENABLE_DOWNLOADS=1 \
> -DWITH_DEBUG=0
[root@localhost mysql-5.7.36]# make
[root@localhost mysql-5.7.36]# make install
[root@localhost mysql-5.7.36]# mkdir /usr/local/mysql-5.7.36/data
10.初始化数据库
[root@localhost mysql-5.7.36]# touch /var/log/mysqld.log
[root@localhost mysql-5.7.36]# chown mysql.mysql /var/log/mysqld.log
[root@localhost mysql-5.7.36]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-5.7.36/ --datadir=/usr/local/mysql-5.7.36/data/
11.注册MySQL启动服务
[root@localhost mysql-5.7.36]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql-5.7.36/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
[root@localhost mysql-5.7.36]# systemctl daemon-reload
[root@localhost mysql-5.7.36]# systemctl start mysqld
12.连接测试
[root@localhost mysql-5.7.36]# cat /var/log/mysqld.log | grep password
2022-02-25T16:10:47.165356Z 1 [Note] A temporary password is generated for root@localhost: ahrermuKP4,D
[root@localhost mysql-5.7.36]# /usr/local/mysql-5.7.36/bin/mysql -uroot -p'ahrermuKP4,D'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
13.修改默认密码
mysql> alter user root@localhost identified by 'Test123!';
Query OK, 0 rows affected (0.00 sec)
14.添加环境变量
[root@localhost mysql-5.7.36]# vim /etc/profile
export MYSQL_HOME=/usr/local/mysql-5.7.36
export PATH=$PATH:$MYSQL_HOME/bin
[root@localhost mysql-5.7.36]# source //etc/profile
[root@localhost mysql-5.7.36]# mysql -uroot -p'Test123!'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!