1. 程式人生 > 其它 >伺服器安裝mysql

伺服器安裝mysql

1、解除安裝mysql

1.檢視是否安裝mysql

首先檢查是否已經安裝,如果已經安裝先刪除以前版本,以免安裝不成功

[root@localhost ~]# php -v

[root@localhost ~]# rpm -qa | grep mysql

[root@localhost ~]# yum list installed | grep mysql

如果顯示以下內容說明沒有安裝服務 -bash: gerp: command not found

如果有會顯示如下:

mysql57-community-release-el7-8.noarch
mysql-community-common-5.1.73-1.el7.x86_64
mysql-community-client-5.1.73-1.el7.x86_64
mysql-community-server-5.1.73-1.el7.x86_64
mysql-community-libs-5.1.73-1.el7.x86_64
mysql-community-libs-compat-5.1.73-1.el7.x86_64

這時就需要解除安裝這些安裝服務

[root@localhost ~]# rpm -e mysql57-community-release-el7-8.noarch
[root@localhost ~]# rpm -e mysql-community-common-5.1.73-1.el7.x86_64
[root@localhost ~]# rpm -e mysql-community-client-5.1.73-1.el7.x86_64
[root@localhost ~]# rpm -e mysql-community-server-5.1.73-1.el7.x86_64
[root@localhost ~]# rpm -e mmysql-community-libs-5.1.73-1.el7.x86_64
[root@localhost ~]# rpm -e mysql-community-libs-compat-5.1.73-1.el7.x86_64

2.刪除配置的mysql空間

  • 先檢視my.cnf中配置的datadir,不要刪錯了檔案(預設為:/var/lib/mysql)

  • 刪除datadir

rm -rf /var/lib/mysql/
  • 刪除配置

rm /etc/my.cnf

3.檢查剩餘的mysql資訊

whereis mysql
  • 比如像我的就還有如下資訊

mysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
  • 將搜尋出的mysql全都刪掉

  • 檢視mysql依賴

systemctl list-dependencies mysqld

如果顯示只有

mysqld.service

這時就刪乾淨了! 如果顯示像這樣,還得繼續刪

mysqld.service
● ├─system.slice
● └─basic.target
●   ├─microcode.service
●   ├─rhel-dmesg.service
●   ├─[email protected]
●   ├─paths.target
●   ├─slices.target
●   │ ├─-.slice
●   │ └─system.slice
●   ├─sockets.target
●   │ ├─dbus.socket
●   │ ├─systemd-initctl.socket
●   │ ├─systemd-journald.socket
●   │ ├─systemd-shutdownd.socket
●   │ ├─systemd-udevd-control.socket
●   │ └─systemd-udevd-kernel.socket
●   ├─sysinit.target
●   │ ├─dev-hugepages.mount
●   │ ├─dev-mqueue.mount
●   │ ├─kmod-static-nodes.service
●   │ ├─plymouth-read-write.service
●   │ ├─plymouth-start.service
●   │ ├─proc-sys-fs-binfmt_misc.automount
●   │ ├─rhel-autorelabel-mark.service
●   │ ├─rhel-autorelabel.service
●   │ ├─rhel-domainname.service
●   │ ├─rhel-import-state.service
●   │ ├─rhel-loadmodules.service
●   │ ├─sys-fs-fuse-connections.mount
●   │ ├─sys-kernel-config.mount
●   │ ├─sys-kernel-debug.mount

4.mysql解除安裝完成

完成上面步驟,終於解除安裝完成!

2、安裝Mysql

1.確保伺服器系統處於最新狀態

[root@localhost ~]# yum -y update

如果顯示內容中含有

[root@localhost ~]# Complete!

說明更新完成

2.重啟伺服器(可選)

[root@localhost ~]# reboot

3.下載MySql安裝包

root@localhost ~]# rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

[root@localhost ~]# rpm -ivh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

4.安裝MySql

[root@localhost ~]# yum install -y mysql-server

[root@localhost ~]# yum install mysql-community-server

如果顯示以下內容說明安裝成功

Complete!

5.修改mysql預設字符集

這裡我們需要設定成utf8,開啟my.cnf配置檔案

vi /etc/my.cnf

新增以下內容:

//在[mysqld]的下面新增服務端字符集
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
//lower_case_table_names預設為0,對錶名大小寫敏感;1不敏感
lower_case_table_names=1

//需要在最下方填寫客戶端字符集
[client]
default-character-set=utf8

6.設定開機啟動Mysql

[root@localhost ~]# systemctl enable mysqld.service

檢查是否開機自動啟動設定成功

[root@localhost ~]# systemctl list-unit-files | grep mysqld

如果顯示以下內容說明已經完成自動啟動安裝

mysqld.service enabled

7.設定開啟服務

[root@localhost ~]# systemctl start mysqld.service

[root@localhost ~]# service mysqld start

這裡需要在例項安全組配置規則,開啟3306埠

8.修改mysql登入密碼

設定mysql密碼,只要滿足六位的長度。

alter user 'root'@'localhost' identified by '123456'; 

9.授予遠端連線許可權(Navicat)

mysql> use mysql

mysql> select Host,User from user;
+-----------+------------------+
| Host     | User             |
+-----------+------------------+
| localhost | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session   |
| localhost | mysql.sys       |
+-----------+------------------+
4 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> select Host,User from user;
+-----------+------------------+
| Host     | User             |
+-----------+------------------+
| %         | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session   |
| localhost | mysql.sys       |
+-----------+------------------+
4 rows in set (0.00 sec)

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