第一次裝的時候感覺很簡單沒有記錄专甩,第二次裝的時候報了很多錯誤!P哉选!县遣!
注意如果服務(wù)器有自帶的mysql 或者 mariadb 一定要先進行卸載糜颠,怎么卸載看下面
服務(wù)器版本如下:
[root@centos7 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
基本命令
啟動 : systemctl start mysqld
重啟: systemctl restart mysqld
查看狀態(tài): systemctl status mysqld
安裝步驟
第一步,清理環(huán)境
1. 首先艺玲,查看系統(tǒng)是否已經(jīng)安裝了mysql數(shù)據(jù)庫
rpm -qa | grep mysql
[root@localhost etc]# rpm -qa | grep mysql
mysql-community-libs-8.0.16-2.el7.x86_64
mysql-community-common-8.0.16-2.el7.x86_64
mysql-community-client-8.0.16-2.el7.x86_64
mysql80-community-release-el7-3.noarch
mysql-community-server-8.0.16-2.el7.x86_64
2. 查詢到已安裝的MySQL庫后括蝠,執(zhí)行以下命令依次卸載
yum remove mysql-xxx-xxx
若沒有查詢后,沒有安裝Mysql庫饭聚,可直接跳過此步忌警。
3. 刪除mysql的配置文件(MySQL卸載不會自動刪除配置文件,需自行清理)
首先使用如下命令查找出所用的配置文件
find / -name mysql
[root@localhost etc]# find / -name mysql
/etc/logrotate.d/mysql
/var/lib/docker/overlay2/a74267a6cde69551beede82788b48f1430f6ac88e4c7a8cdf9bcf16b2125aac6/diff/etc/mysql
/var/lib/docker/overlay2/a74267a6cde69551beede82788b48f1430f6ac88e4c7a8cdf9bcf16b2125aac6/diff/usr/include/mysql
/var/lib/docker/overlay2/a74267a6cde69551beede82788b48f1430f6ac88e4c7a8cdf9bcf16b2125aac6/diff/usr/include/mysql/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/bin/mysql
/usr/lib64/mysql
根據(jù)需求使用以下命令 依次 對配置文件進行刪除
rm -rf /var/lib/mysql
二秒梳、刪除MariaDB的文件
由于 在CentOS中默認安裝有MariaDB法绵,所以如果我們不刪除MariaDB文件的話,安裝mysql時可能會發(fā)生沖突酪碘。沖突信息如下:
[root@localhost yum.repos.d]# yum install mysql-community-server.x86_64
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* epel: mirrors.ustc.edu.cn
* extras: mirrors.163.com
* updates: centos.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package mariadb-server.x86_64 1:5.5.52-1.el7 will be obsoleted
--> Processing Dependency: mariadb-server for package: akonadi-mysql-1.9.2-4.el7.x86_64
---> Package mysql-community-server.x86_64 0:5.7.19-1.el7 will be obsoleting
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.19-1.el7 for package: mysql-community-server-5.7.19-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.19-1.el7.x86_64
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.52-1.el7 will be obsoleted
---> Package mariadb-server.x86_64 1:5.5.52-1.el7 will be obsoleted
--> Processing Dependency: mariadb-server for package: akonadi-mysql-1.9.2-4.el7.x86_64
---> Package mysql-community-client.x86_64 0:5.7.19-1.el7 will be obsoleting
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.19-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.19-1.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.52-1.el7 will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: perl-DBD-MySQL-4.023-5.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 1:qt-mysql-4.8.5-13.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: perl-DBD-MySQL-4.023-5.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 1:qt-mysql-4.8.5-13.el7.x86_64
---> Package mariadb-server.x86_64 1:5.5.52-1.el7 will be obsoleted
--> Processing Dependency: mariadb-server for package: akonadi-mysql-1.9.2-4.el7.x86_64
---> Package mysql-community-libs.x86_64 0:5.7.19-1.el7 will be obsoleting
--> Running transaction check
---> Package mariadb-server.x86_64 1:5.5.52-1.el7 will be obsoleted
--> Processing Dependency: mariadb-server for package: akonadi-mysql-1.9.2-4.el7.x86_64
---> Package mysql-community-libs-compat.x86_64 0:5.7.19-1.el7 will be obsoleting
--> Finished Dependency Resolution
Error: Package: akonadi-mysql-1.9.2-4.el7.x86_64 (@anaconda)
Requires: mariadb-server
Removing: 1:mariadb-server-5.5.52-1.el7.x86_64 (@anaconda)
mariadb-server = 1:5.5.52-1.el7
Obsoleted By: mysql-community-server-5.7.19-1.el7.x86_64 (mysql57-community)
Not found
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest
1.使用rpm 命令查找出要刪除的mariadb文件,命令如下
rpm -pa | grep mariadb
可能出現(xiàn)結(jié)果
mariadb-libs-5.5.56-2.el7.x86_64
刪除上面的程序
yum -y remove mariadb-libs.x86_64
至此朋譬,原有的mysql和mariadb數(shù)據(jù)庫就刪除了
三、安裝mysql
- contos7默認安裝了mariadb數(shù)據(jù)庫,這里把他移除:yum remove mariadb-libs.x86_64
- 創(chuàng)建文件目錄 mkdir /etc/mysql
1兴垦、 安裝mysql官網(wǎng)提供的mysql repo源**
網(wǎng)址:https://dev.mysql.com/downloads/repo/yum/ 操作如下:
2. 把這個rpm文件下載下來放到服務(wù)器上,或者在linux系統(tǒng)中通過wget命令下載
wget http://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
下載完成后使用yum命令本地安裝yum源
3. 安裝mysql80-community-release-el7-1.noarch.rpm包徙赢。使用命令:
yum localinstall mysql80-community-release-el7-1.noarch.rpm
rpm -ivh mysql80-community-release-el7-1.noarch.rpm
執(zhí)行完畢后使用下面的命令檢查是否安裝成功
yum repolist enabled | grep "mysql.*-community.*"
看到類似下圖說明安裝成功
安裝MySQL
使用命令yum install -y mysql-community-server
進行安裝
4. 使用命令 “yum install mysql-community-server”安裝mysql。大概有1個多G探越,請耐心等待安裝完成
默認配置文件路徑:
配置文件:/etc/my.cnf
日志文件:/var/log/var/log/mysqld.log
服務(wù)啟動腳本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
啟動MySQL服務(wù)
使用systemctl start mysqld
命令啟動
使用systemctl status mysqld
查看服務(wù)啟動狀態(tài)
開機啟動
打上下面這兩條命令就行了,不行就不行了..看不懂
systemctl enable mysqld
systemctl daemon-reload
. 修改root默認密碼
兩種方式第二種在下面我用的第一種
在修改密碼之前要先做一件事,就是找到mysql的配置文件(vim /etc/my.cnf)↓
把密碼的加密方式改成之前版本的,8.0版本更換了密碼的加密方式,我們就先用舊的..
把上面圖片里這一行信息前面的"#"刪掉,改完之后記得重啟服務(wù)(systemctl restart mysqld)
修改密碼
1.剛剛啟動成功之后, 用命令查看默認密碼并且登錄
查看:cat /var/log/mysqld.log | grep password
登陸:mysql -u root -p 然后輸入密碼
-
修改密碼
set global validate_password.policy=0;
set global validate_password.length=1;
ALTER USER "root"@"localhost" IDENTIFIED BY "123456"; // 新密碼為123456
- exit 退出 mysql -u root -p 然后輸入密碼即可登錄
授權(quán)遠程訪問
首先確保關(guān)閉了防火墻 并重啟mysql狡赐。
一般不允許root用戶進行遠程登錄,我先記錄一下命令,這個是root遠程登錄授權(quán)的命令
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '密碼' WITH GRANT OPTION;
遠程訪問
- 選擇數(shù)據(jù)庫 use mysql show tables;可以看到很多表
mysql> use mysql
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| engine_cost |
| func |
| general_log |
| global_grants |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| password_history |
| plugin |
| procs_priv |
| proxies_priv |
| role_edges |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
33 rows in set (0.00 sec)
mysql>
- 修改連接規(guī)則:host表示允許哪個ip來連接,user表示哪個數(shù)據(jù)庫钦幔。例如 mysql –uroot –p 連的就是叫root數(shù)據(jù)庫枕屉。
-
查看規(guī)則 select host,user from user \G; 修改規(guī)則 update user set host= '%' where user = 'root';
mysql> update user set host= '%' where user = 'root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host,user from user \G;
*************************** 1. row ***************************
host: %
user: root
*************************** 2. row ***************************
host: localhost
user: mysql.infoschema
*************************** 3. row ***************************
host: localhost
user: mysql.session
*************************** 4. row ***************************
host: localhost
user: mysql.sys
4 rows in set (0.00 sec)
ERROR:
No query specified
mysql>
- mysql5.7.6之后就修改了加密規(guī)則,我這里是mysql8.0.12 所以需要更改加密方式鲤氢。(由于上面已經(jīng)修改了配置文件所以這種方式并未嘗試)
update user set plugin='mysql_native_password' where user ='root'; - 刷新權(quán)限 flush privileges;
-
測試外網(wǎng)連接mysql服務(wù)器搀擂。
(如果外網(wǎng)連接失敗的話西潘,先按照【重置密碼】執(zhí)行一遍,再執(zhí)行【修改密碼】哨颂,只是ALTER USER "root"@"localhost" IDENTIFIED BY "1234";要替換成ALTER USER "root"@"%" IDENTIFIED BY "1234";)
重置密碼
- 開啟免密碼登陸 修改my.cnf文件 默認在/etc/my.cnf喷市。
vim /etc/my.cnf 在【mysqld】模塊下面添加:skip-grant-tables 保存退出。 - 重啟服務(wù)咆蒿,使配置生效 东抹。 service mysqld restart
- 登陸 mysql -u root -p //不輸入密碼直接敲回車鍵
- 選擇數(shù)據(jù)庫 use mysql 把密碼置空(因為免密登陸時不能直接修改密碼)
update user set authentication_string = '' where user = 'root'; - 退出 quit 把/etc/my.cnf免密刪掉。 重啟服務(wù) service mysqld restart
- 登陸 mysql -u root -p //直接敲回車鍵沃测,因為剛剛置空密碼了缭黔。
- 和上面修改密碼的步驟一樣,重復(fù)一遍就好了~ 哈哈蒂破!
卸載mysql
首先 馏谨, 停止mysql service mysqld stop
總共分兩步: 1.卸載mysql組件 2.刪除相關(guān)文件夾
【第一步卸載mysql組件】
【 查看MySQL組件 】
[root@localhost ~]# rpm -qa | grep -i mysql
mysql-community-libs-8.0.12-1.el7.x86_64
mysql-community-common-8.0.12-1.el7.x86_64
mysql-community-client-8.0.12-1.el7.x86_64
mysql-community-server-8.0.12-1.el7.x86_64
【刪除】
[root@localhost ~]# rpm -ev mysql-community-server-8.0.12-1.el7.x86_64
Preparing packages...
mysql-community-server-8.0.12-1.el7.x86_64
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave
【刪除】
[root@localhost ~]# rpm -ev mysql-community-client-8.0.12-1.el7.x86_64
Preparing packages...
mysql-community-client-8.0.12-1.el7.x86_64
【刪除失敗】
[root@localhost ~]# rpm -ev mysql-community-common-8.0.12-1.el7.x86_64
error: Failed dependencies:
mysql-community-common(x86-64) >= 8.0.0 is needed by (installed) mysql-community-libs-8.0.12-1.el7.x86_64
【繼續(xù)查看】
[root@localhost ~]# rpm -qa | grep -i mysql
mysql-community-libs-8.0.12-1.el7.x86_64
mysql-community-common-8.0.12-1.el7.x86_64
【刪除】
[root@localhost ~]# rpm -e --noscripts mysql-community-libs-8.0.12-1.el7.x86_64
[root@localhost ~]#
【刪除】
[root@localhost ~]# rpm -e --noscripts mysql-community-common-8.0.12-1.el7.x86_64
[root@localhost ~]#
【查看】
[root@localhost ~]# rpm -qa | grep -i mysql
[root@localhost ~]#
【第二步:刪除相關(guān)目錄】
【查看】
[root@localhost ~]# find / -name mysql
/etc/mysql
/etc/selinux/targeted/active/modules/100/mysql
/var/lib/mysql
/var/lib/mysql/mysql
【刪除】
[root@localhost ~]# rm -rf /etc/mysql
[root@localhost ~]# rm -rf /etc/selinux/targeted/active/modules/100/mysql
[root@localhost ~]# rm -rf /var/lib/mysql
[root@localhost ~]# find / -name mysql
【再次檢查,什么都沒有說明卸載成功了】
[root@localhost ~]# rpm -qa | grep -i mysql
[root@localhost ~]#
注意如果卸載不掉用下面的方法卸載
一附迷、如果是使用yum安裝的mysql惧互,使用如下命令進行卸載(不能確定使用何種方式安裝的mysql情況下,按后續(xù)步驟一一進行處理即可):
# yum remove mysql mysql-server mysql-libs compat-mysql51
# rm -rf /var/lib/mysq
# rm /etc/my.cnf
使用rpm -qa|grep mysql命令來查看rpm方式安裝的mysql喇伯,如果查詢結(jié)果不為空喊儡,需要將這些rpm卸載掉。
#參考鏈接:
https://www.cnblogs.com/wlwl/p/9686809.html
https://my.oschina.net/liuyuantao/blog/1859577
https://blog.csdn.net/qq_39470733/article/details/80999080
注意參考:https://blog.csdn.net/chrisjingu/article/details/90291445