centos安装mysql步骤,CentOS安装mysql超详细步骤
MySQL5.7下載地址:?https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-el7-x86_64.tar.gz
1.?刪除系統自帶的Mariadb 或MySQL
#卸載系統自帶的Mariadb
[root@localhost ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.64-1.el7.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
#刪除etc目錄下的my.cnf文件,若不存在可以略過此步
[root@localhost soft]# rm /etc/my.cnf
rm: 無法刪除"/etc/my.cnf": 沒有那個文件或目錄
#檢查mysql是否存在
[root@localhost ~]# rpm -qa | grep mysql
#檢查mysql組和用戶是否存在,如無創建
[root@localhost ~]# cat /etc/group | grep mysql
[root@localhost ~]# cat /etc/passwd | grep mysql
2.?新建mysql用戶、用戶組
#創建mysql用戶組
[root@localhost ~]# groupadd mysql
#創建一個用戶名為mysql的用戶并加入mysql用戶組(第一個mysql表示用戶組,第二個表示用戶名)
[root@localhost ~]# useradd -g mysql mysql
#設定password(mysql為我創建的用戶名,可以自行調整)
[root@localhost soft]# passwd mysql
更改用戶 mysql 的密碼 。
新的 密碼:
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
3. 新建數據目錄
#在home下新建data數據存放目錄
[root@localhost ~]# cd /home
[root@localhost home]# mkdir mysqldata
#授權用戶、組
[root@localhost home]# chown -R mysql:mysql mysqldata
4. 修改配置文件
在etc下新建配置文件my.cnf
my.cnf參考配置:
[mysql]
default-character-set=utf8
[mysqld]
skip-name-resolve
port = 3306
#此處自行調整,mysql的安裝目錄
basedir=/usr/local/mysql
#此處自行調整,mysql數據存放目錄
datadir=/home/mysqldata
max_connections=200
character-set-server=utf8
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
#log-bin=/data/mysql/mysql57/binlog
log-bin=OFF
binlog-format=ROW
server-id=1
#是否開始驗證,若放開,則登錄mysql時忽略驗證
#skip-grant-tables
設置權限:
[root@localhost local]# chown 777 /etc/my.cnf
5.解壓文件
將下載好的壓縮文件上傳到服務器,具體目錄可以自己設置,我這里再home目錄下新建了soft目錄來存放
#進入壓縮文件所在目錄
[root@localhost soft]# pwd
/home/soft
#解壓到/usr/local/
[root@localhost soft]# tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost soft]# cd /usr/local/
#重命名為mysql
[root@localhost local]# mv mysql-5.7.21-linux-glibc2.12-x86_64/ mysql
6.修改mysql所屬用戶組,初始化mysql
[root@localhost local]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost local]# cd mysql/bin
[root@localhost bin]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/home/mysqldata/
### 如果上面命令初始化不成功可以使用下面語句初始化
[root@localhost bin]# ./mysql_install_db --no-defaults --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysqldata/
2019-10-18 09:17:08 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2019-10-18 09:17:12 [WARNING] The bootstrap log isn't empty:
2019-10-18 09:17:12 [WARNING] 2019-10-18T01:17:08.767197Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2019-10-18T01:17:08.767839Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5010)
2019-10-18T01:17:08.767845Z 0 [Warning] Changed limits: max_connections: 214 (requested 1000)
2019-10-18T01:17:08.767847Z 0 [Warning] Changed limits: table_open_cache: 400 (requested 2000)
7.防火墻設置
#查看防火墻狀態是否啟動 ,如下是已經啟動
[root@localhost bin]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2019-10-15 16:20:16 CST; 2 days ago
Docs: man:firewalld(1)
Main PID: 1223 (firewalld)
CGroup: /system.slice/firewalld.service
└─1223 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
10月 15 16:20:15 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
10月 15 16:20:16 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
#如果防火墻未啟動, 通過systemctl start firewalld開啟防火墻,沒有任何提示即開啟成功。
[root@localhost bin]# systemctl start firewalld
#添加端口
[root@localhost bin]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
#重新加載設置
[root@localhost bin]# firewall-cmd --reload
success
8.設置全局查看mysql服務
#如果未配置會出現如下
[root@localhost support-files]# service mysql status
Redirecting to /bin/systemctl status mysql.service
Unit mysql.service could not be found.
#配置
[root@localhost support-files]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[root@localhost support-files]# vi /etc/init.d/mysql
#修改內容,具體目錄可以自行調整
basedir=/usr/local/mysql
datadir=/home/mysqldata
#修改權限
[root@localhost support-files]# chmod +x /etc/init.d/mysql
[root@localhost init.d]# service mysql status
ERROR! MySQL is not running
9. mysql用戶初始化默認密碼、遠程連接初始化
#查看mysql服務狀態
[root@localhost bin]# service mysql status
ERROR! MySQL is not running
#啟動,stop是停止
[root@localhost bin]# service mysql start
Starting MySQL. SUCCESS!
[root@localhost bin]# service mysql status
SUCCESS! MySQL running (7645)
##獲取默認密碼
[root@localhost bin]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2019-10-18 09:17:08
1fNsl6LkQ+*/
#登錄
[root@localhost 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.21
Copyright (c) 2000, 2018, 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>set PASSWORD = PASSWORD('123456a');
###切庫
mysql>use mysql;
###若修改密碼不生效,使用以下命令修改
mysql>update user set authentication_string=PASSWORD("自定義密碼") where user='root';
##修改權限,設置遠程訪問,user后的單引號是用戶名
mysql>update user set host='%' where user='root';
###刷新
mysql> flush privileges;
如果修改無效,刷新一下再重啟mysql服務!
至此,結束,可以使用navicat遠程連接上!
附帶一些基本命令
show databases;//顯示所有數據庫
show tables;//顯示所有表
use 庫名;//切庫
desc 表名;//查看表結構
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的centos安装mysql步骤,CentOS安装mysql超详细步骤的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 皮皮虾能不能冷冻保存?
- 下一篇: 夏天种哪些蔬菜最耐热呀?