內容概要
- 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)