CentOS-7 MySQL 5.7.26安裝

我用的非root賬戶 在 /usr/local目錄下安裝
卸載系統自帶的 mariadb 否則影響 mysql 的安裝
查找是否有:rpm -qa|grep mariadb
卸載:sudo rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
創建 mysql 用戶組: sudo groupadd mysql
創建 mysql 用戶(禁止登錄): sudo useradd -r -g mysql -s /bin/false mysql
切換至 mysql 安裝目錄: cd /usr/local

[qgn@localhost ~]$ rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[qgn@localhost ~]$ sudo rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
[qgn@localhost ~]$ sudo groupadd mysql
[qgn@localhost ~]$ sudo useradd -r -g mysql -s /bin/false mysql
[qgn@localhost ~]$ cd /usr/local
[qgn@localhost local]$



下載 MySQL
下載地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads

在這裏插入圖片描述

點擊 DownLoad

在這裏插入圖片描述

不登錄下載即可

或者直接在linux 上下載
sudo wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

在這裏插入圖片描述

解壓 sudo tar zxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
修改文件夾名字爲 mysql:sudo mv mysql-5.7.26-linux-glibc2.12-x86_64 mysql
在 mysql 創建 data 文件夾:sudo mkdir mysql/data
更改文件所屬用戶和組:sudo chown -R mysql:mysql ./mysql

[qgn@localhost local]$ sudo mv ~/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz ./
[qgn@localhost local]$ sudo tar zxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[qgn@localhost local]$ sudo mv mysql-5.7.26-linux-glibc2.12-x86_64 mysql
[qgn@localhost local]$ sudo mkdir mysql/data
[qgn@localhost local]$ sudo chown -R mysql:mysql ./mysql
[qgn@localhost local]$ ll
總用量 629756
drwxr-xr-x.  2 root  root          6 4月  11 2018 bin
drwxr-xr-x.  2 root  root          6 4月  11 2018 etc
drwxr-xr-x.  2 root  root          6 4月  11 2018 games
drwxr-xr-x.  2 root  root          6 4月  11 2018 include
drwxr-xr-x.  2 root  root          6 4月  11 2018 lib
drwxr-xr-x.  3 root  root         26 5月  22 20:41 lib64
drwxr-xr-x.  2 root  root          6 4月  11 2018 libexec
drwxr-xr-x. 10 mysql mysql       141 5月  25 11:18 mysql
-rw-rw-r--.  1 qgn   qgn   644869837 5月  25 11:12 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x.  2 root  root          6 4月  11 2018 sbin
drwxr-xr-x.  5 root  root         49 5月  23 04:09 share
drwxr-xr-x.  2 root  root          6 4月  11 2018 src
[qgn@localhost local]$



進入 mysql 目錄:cd mysql
初始化: ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

如果遇到下面錯誤。則是權限不夠,更改 data 文件夾的權限:sudo chmod 777 data
然後再初始化,完成之後,綠色方框裏面 root@localhost 後面爲初始密碼,記下來,一會登錄用

在這裏插入圖片描述

將 support-files下面的mysql.server 文件複製到 /etc/init.d/mysql sudo cp support-files/mysql.server /etc/init.d/mysql
修改 /etc/init.d/mysql 文件如下: sudo vim /etc/init.d/mysql

在這裏插入圖片描述

更改 /etc/init.d/mysql 文件執行權限: sudo chmod a+x /etc/init.d/mysql
直接啓動:/etc/init.d/mysql start 或者 service mysql start

[qgn@localhost mysql]$ sudo cp support-files/mysql.server /etc/init.d/mysql
[qgn@localhost mysql]$ sudo vim /etc/init.d/mysql
[qgn@localhost mysql]$ sudo chmod a+x /etc/init.d/mysql
[qgn@localhost mysql]$ /etc/init.d/mysql start
Starting MySQL. SUCCESS!
[qgn@localhost mysql]$



成功開啓 mysql 服務
查看 mysql 服務 ps -ef | grep mysql

[qgn@localhost mysql]$ ps -ef | grep mysql
qgn       78467      1  0 17:14 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/localhost.localdomain.pid
qgn       78558  78467  0 17:14 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=localhost.localdomain.err --pid-file=/usr/local/mysql/data/localhost.localdomain.pid
qgn       79018  20835  0 17:15 pts/0    00:00:00 grep --color=auto mysql
[qgn@localhost mysql]$



開啓時遇到下面錯誤

Starting MySQL./usr/local/mysql/bin/mysqld_safe:行647: /var/log/mariadb/mariadb.log: 沒有那個文件或目錄
2019-05-22T15:51:02.864835Z mysqld_safe error: log-error set to ‘/var/log/mariadb/mariadb.log’, however file don’t exists. Create writable for user ‘mysql’.
/usr/local/mysql/bin/mysqld_safe:行144: /var/log/mariadb/mariadb.log: 沒有那個文件或目錄
ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).


解決辦法:
創建文件夾 創建文件
sudo mkdir /var/log/mariadb
sudo touch /var/log/mariadb/mariadb.log
更改文件權限
sudo chown mysql:mysql mariadb.log
sudo chmod 666 mariadb.log登陸 mysql bin/mysql -uroot -p
輸入初始化時記下的密碼
進入mysql進行操作則會提示更改密碼,
將密碼更改爲 mysql
mysql> set password=password('mysql');
查看用戶以及權限
mysql> use mysql;
mysql> select host,user from user;
更改權限表,使 mysql 可以遠程連接
mysql> update user set host = '%' where user ='root';
重新加載權限表
 

mysql> flush privileges;

[qgn@localhost mysql]$ bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> set password=password('mysql');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> update user set host = '%' where user ='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> quit;
Bye

 

配置開機啓動
 

添加爲服務: chkconfig --add /etc/init.d/mysql

查看服務列表:  chkconfig --list

看到3、4、5狀態爲開或者爲 on 則表示成功。如果是 關或者 off 則執行一下:chkconfig --level 345 mysqld on

重啓計算機:reboot

再次查看服務列表或者查看3306端口號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章