1.下載mysql
mysql官網:https://dev.mysql.com/downloads/mysql/


將下載的mysql上傳打linux
2.解壓並重命名
[root@rsyncClient local]# tar -zxvf mysql-8.0.18-el7-x86_64.tar.gz -C /usr/local/ [root@rsyncClient local]# mv mysql-8.0.18-el7-x86_64/ mysql
3.在mysql根目錄下創建data目錄,存放數據
[root@rsyncClientopt]# cd /usr/local/mysql/ [root@rsyncClient mysql]# mkdir data
4.創建mysql用戶組和mysql用戶
[root@rsyncClient local]# groupadd mysql [root@rsyncClient local]# useradd -g mysql mysql
5.改變mysql目錄權限
[root@rsyncClient local]# chown -R mysql.mysql /usr/local/mysql/
6.初始化數據庫
[root@rsyncClient mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql
--datadir=/usr/local/mysql/data

7.配置mysql
在mysql/support-files創建文件my-default.cnf
[root@rsyncClient support-files]# cd /usr/local/mysql/support-files/ [root@rsyncClient support-files]# touch my-default.cnf
復制配置文件到/etc/my.cnf
[root@rsyncClient support-files]# cp -a ./my-default.cnf /etc/my.cnf cp: overwrite ‘/etc/my.cnf’? y
編輯my.cnf
[client] port=3306 socket=/tmp/mysql.sock [mysqld] port=3306 user=mysql socket=/tmp/mysql.sock basedir=/usr/local/mysql datadir=/usr/local/mysql/data
8.配置環境變量
編輯 / etc/profile 文件
[root@rsyncClient mysql]# vim /etc/profile #配置mysql環境變量 PATH=/data/mysql/bin:/data/mysql/lib:$PATH export PATH
#讓其生效 [root@rsyncClient mysql]# source /etc/profile
#看環境變量是否生效 [root@rsyncClient mysql]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
9.啟動mysql
[root@rsyncClient mysql]# systemctl start mysqld
啟動失敗報錯1:
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
解決方案:
[root@rsyncClient ~]# chown mysql:mysql -R /usr/local/mysql/
啟動失敗報錯2:
[root@rsyncClient mysql]# service mysql start /etc/init.d/mysql: ./bin/my_print_defaults: /lib/ld-linux.so.2: bad ELF interpreter: No such file or
directory Starting MySQL. ERROR! The server quit without
updating PID file (/var/lib/mysql/rsyncClient.pid).
去這個目錄下面查看 cat/usr/local/mysql/data/rsyncClient.err錯誤,對應的的解決,這里錯誤是因為my.conf配置錯誤
啟動失敗報錯3:
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file:
No such file or directory [root@rsyncClient init.d]# yum install libncurses.so.5
以這個為例,如果缺少這樣依賴,直接用yum安裝
啟動失敗報錯4:
[root@rsyncClient data]# mysql -uroot -p Enter password: ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded:
/usr/lib/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
身份驗證插件不能加載
解決辦法:
[root@rsyncClient lib]# vim /etc/my.cnf
在這個[mysqld]下添加一行:
default_authentication_plugin=mysql_native_password
如果忘記了密碼在加上:
skip-grant-tables(跳過密碼驗證)等設置了密碼就去掉
10 使用systemctl命令啟動關閉mysql服務
啟動mysql服務:
#systemctl start mysqld.service
停止mysql服務
#systemctl stop mysqld.service
重啟mysql服務
#systemctl restart mysqld.service
查看mysql服務當前狀態
#systemctl status mysqld.service
設置mysql服務開機自啟動
#systemctl enable mysqld.service
停止mysql服務開機自啟動
#systemctl disable mysqld.service
11.mysql的基本操作
# 使用mysql客戶端連接mysql
>/usr/local/mysql/bin/mysql -u root -p password
修改mysql的默認初始化密碼
> alter user 'root'@'localhost' identified by 'root';
# 創建用戶 CREATE USER '用戶名稱'@'主機名稱' INDENTIFIED BY '用戶密碼'
> create user 'yehui'@'localhost' identified by 'yehui';
#給所有遠程登錄的進行授權,此方式已經報錯了
> grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
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 'identified by 'root' with grant option' at line 1
# 修改root用戶可以遠程連接
> update mysql.user set host='%' where user='root';
# 授予權限 grant 權限 on 數據庫.表 to '用戶名'@'登錄主機' [INDENTIFIED BY '用戶密碼'];
> grant replication slave on *.* to 'yehui'@'localhost';
12.防火牆問題
[root@rsyncClient data]# firewall-cmd --permanent --zone=public --add-port=3306/tcp #允許訪問 success [root@rsyncClient data]# firewall-cmd --reload #重新加載 success
[root@rsyncClient data]# firewall-cmd --permanent --zone=public --query-port=3306/tcp #查看是否開通訪問權限
yes
