:1. 安裝包準(zhǔn)備
mysql 下載地址:
https://dev.mysql.com/downloads/mysql/


- 檢查環(huán)境是否有mysql
命令:
rpm -qa | grep mysql
cat /etc/group | grep mysql
如果有則刪除
命令:
find / -name mysql|xargs rm -rf
- 先安裝必備庫
命令:
yum search libaio
yum install libaio
結(jié)果
[root@localhost ~]# yum search libaio
已加載插件:fastestmirror, langpacks
Determining fastest mirrors
* base: mirrors.btte.net
* extras: mirrors.nwsuaf.edu.cn
* updates: mirrors.btte.net
(1/2): base/7/x86_64/primary_db | 5.6 MB 00:00:01
(2/2): updates/7/x86_64/primary_db | 7.8 MB 00:00:01
================================ N/S matched: libaio ================================
libaio.i686 : Linux-native asynchronous I/O access library
libaio.x86_64 : Linux-native asynchronous I/O access library
libaio-devel.i686 : Development files for Linux-native asynchronous I/O access
libaio-devel.x86_64 : Development files for Linux-native asynchronous I/O access
名稱和簡介匹配 only,使用“search all”試試。
[root@localhost ~]# yum install libaio
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.nwsuaf.edu.cn
* updates: mirrors.btte.net
軟件包 libaio-0.3.109-13.el7.x86_64 已安裝并且是最新版本
無須任何處理
[root@localhost ~]#
- 檢查mysql組和用戶是否存在,如無創(chuàng)建
命令:
groupadd mysql
useradd -g mysql mysql
passwd mysql
連續(xù)輸入兩次密碼完成用戶創(chuàng)建

上傳軟件包

- 解壓TAR包,更改所屬的組和用戶
命令:
cd /home/mysql/
tar -zxvf mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.19-linux-glibc2.12-x86_64 /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
cd /usr/local/mysql/
結(jié)果:
[root@localhost mysql]# mv mysql-5.7.19-linux-glibc2.12-x86_64 /usr/local/mysql
[root@localhost mysql]# chown -R mysql:mysql /usr/local/mysql
[root@localhost mysql]# ls
mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz
[root@localhost mysql]# cd /usr/local/mysql/
[root@localhost mysql]# ls
bin COPYING docs include lib man README share support-files
[root@localhost mysql]# ll
總用量 36
drwxr-xr-x. 2 mysql mysql 4096 8月 27 12:18 bin
-rw-r--r--. 1 mysql mysql 17987 6月 22 22:13 COPYING
drwxr-xr-x. 2 mysql mysql 55 8月 27 12:18 docs
drwxr-xr-x. 3 mysql mysql 4096 8月 27 12:18 include
drwxr-xr-x. 5 mysql mysql 229 8月 27 12:18 lib
drwxr-xr-x. 4 mysql mysql 30 8月 27 12:18 man
-rw-r--r--. 1 mysql mysql 2478 6月 22 22:13 README
drwxr-xr-x. 28 mysql mysql 4096 8月 27 12:18 share
drwxr-xr-x. 2 mysql mysql 90 8月 27 12:18 support-files

- 創(chuàng)建目錄并授權(quán)
命令
mkdir data mysql-files
chmod 750 mysql-files
chown -R mysql .
chgrp -R mysql .
結(jié)果:
[root@localhost mysql]# mkdir data mysql-files
[root@localhost mysql]# chmod 750 mysql-files
[root@localhost mysql]# chown -R mysql .
[root@localhost mysql]# chgrp -R mysql .
[root@localhost mysql]# ll
總用量 36
drwxr-xr-x. 2 mysql mysql 4096 8月 27 12:18 bin
-rw-r--r--. 1 mysql mysql 17987 6月 22 22:13 COPYING
drwxr-xr-x. 2 mysql mysql 6 8月 27 12:21 data
drwxr-xr-x. 2 mysql mysql 55 8月 27 12:18 docs
drwxr-xr-x. 3 mysql mysql 4096 8月 27 12:18 include
drwxr-xr-x. 5 mysql mysql 229 8月 27 12:18 lib
drwxr-xr-x. 4 mysql mysql 30 8月 27 12:18 man
drwxr-x---. 2 mysql mysql 6 8月 27 12:21 mysql-files
-rw-r--r--. 1 mysql mysql 2478 6月 22 22:13 README
drwxr-xr-x. 28 mysql mysql 4096 8月 27 12:18 share
drwxr-xr-x. 2 mysql mysql 90 8月 27 12:18 support-files
- 初始化數(shù)據(jù)庫
命令
bin/mysqld --initialize --user=mysql
結(jié)果:
[root@localhost mysql]# bin/mysqld --initialize --user=mysql
2017-08-27T04:25:05.984358Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-08-27T04:25:06.293545Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-08-27T04:25:06.381713Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-08-27T04:25:06.459091Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b4410940-8adf-11e7-b689-080027b199be.
2017-08-27T04:25:06.467223Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-08-27T04:25:06.469594Z 1 [Note] A temporary password is generated for root@localhost: koYyPqyL2y#t
初始化數(shù)據(jù)庫并生成臨時密碼 :koYyPqyL2y#t 需要記錄下來

[注意]root@localhost生成臨時密碼,也就是root@localhost:后的字符串
- 生成RSA私鑰,可以跳過此步驟 個人建議不用執(zhí)行
mysql_ssl_rsa_setup
需要openssl
支持,用于啟用數(shù)據(jù)量ssl連接,需要進(jìn)一步配置。
參考-MySQL 使用 SSL 連接
命令
bin/mysql_ssl_rsa_setup
結(jié)果
[root@localhost mysql]# bin/mysql_ssl_rsa_setup
Generating a 2048 bit RSA private key
..................................................................................+++
..........+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
..........................................+++
......+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
.................................................+++
..............+++
writing new private key to 'client-key.pem'
-----
- 授予讀寫權(quán)限
命令:
chown -R root .
chown -R mysql data mysql-files
結(jié)果:
[root@localhost mysql]# chown -R root .
[root@localhost mysql]# chown -R mysql data mysql-files
[root@localhost mysql]# ll
總用量 36
drwxr-xr-x. 2 root mysql 4096 8月 27 12:18 bin
-rw-r--r--. 1 root mysql 17987 6月 22 22:13 COPYING
drwxr-xr-x. 2 mysql mysql 6 8月 27 12:21 data
drwxr-xr-x. 2 root mysql 55 8月 27 12:18 docs
drwxr-xr-x. 3 root mysql 4096 8月 27 12:18 include
drwxr-xr-x. 5 root mysql 229 8月 27 12:18 lib
drwxr-xr-x. 4 root mysql 30 8月 27 12:18 man
drwxr-x---. 2 mysql mysql 6 8月 27 12:21 mysql-files
-rw-r--r--. 1 root mysql 2478 6月 22 22:13 README
drwxr-xr-x. 28 root mysql 4096 8月 27 12:18 share
drwxr-xr-x. 2 root mysql 90 8月 27 12:18 support-files
- 添加到MySQL 啟動腳本到系統(tǒng)服務(wù)
命令:
cp support-files/mysql.server /etc/init.d/mysql.server
結(jié)果:
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql.server
- 啟動腳本
命令:
service mysql.server start
結(jié)果:
[root@localhost mysql]# service mysql.server start
Unit mysql.server.service could not be found.
Starting MySQL. SUCCESS!
- 錯誤1:
Linux安裝MySQL時候出現(xiàn)了問題

這個一看就是權(quán)限問題了
log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
用WinSCP去看的時候發(fā)現(xiàn)并沒有/var/log/mariadb/mariadb.log這個路徑,
那我們就創(chuàng)建, 并給mysql用戶授權(quán)即可
mkdir /var/log/mariadb touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/log/mariadb/
再輸入開啟mysql服務(wù), 搞定!
- 錯誤2:
root@DB-02 ~]# mysql -u root
-bash: mysql: command not found
原因:這是由于系統(tǒng)默認(rèn)會查找/usr/bin下的命令,如果這個命令不在這個目錄下,當(dāng)然會找不到命令,我們需要做的就是映射一個鏈接到/usr/bin目錄下,相當(dāng)于建立一個鏈接文件。
首先得知道m(xù)ysql命令或mysqladmin命令的完整路徑,比如mysql的路徑是:/usr/local/mysql/bin/mysql,我們則可以這樣執(zhí)行命令:
ln -s /usr/local/mysql/bin/mysql /usr/bin
或者將mysql安裝目錄 /usr/local/mysql 配置成環(huán)境變量
vim /etc/profile
MYSQL_HOME=/usr/local/mysql
JAVA_HOME=/usr/java/jdk1.8.0_181-amd64
PATH=$MYSQL_HOME/bin:$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME PATH CLASSPATH


- 數(shù)據(jù)庫已經(jīng)啟動,現(xiàn)在登陸使用,這里用到上面的臨時密碼
錯誤1

錯誤原因:
這是mysql登錄時找不到套接字的問題。
首先需要明白的是,Linux端的mysql server啟動時會開啟一個socket,Linux上的MySQL的客戶端在不使用IP連接時mysql server時,默認(rèn)也會通過套接字來鏈接mysql server。
如果不手動配置my.cnf,那么默認(rèn)的socket一般是DATADIR/mysql.sock,而DATADIR如果沒有手動修改,一般默認(rèn)是/var/lib/mysql目錄。不同的mysql版本,可能socket的默認(rèn)路徑是/tmp/mysql.sock。
例如我將socket路徑修改為/data/mysql.sock。
結(jié)局辦法
client 也加上 socket
[client]
socket=/var/lib/mysql/mysql.sock
[mysqldump]
socket=/var/lib/mysql/mysql.sock
[mysqladmin]
socket=/var/lib/mysql/mysql.sock

然后重啟 service mysql.server restart
[root@localhost mysql]# mysql -uroot -p
Enter password:
ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.
出現(xiàn)問題2:ERROR 1862
翻譯:
錯誤1862(HY000):你的密碼已經(jīng)過期。登錄必須改變它使用一個客戶端,支持過期的密碼。
解決方法:
1、 用忽略授權(quán)表的方法進(jìn)入mysql
vi /etc/my.cnf
[mysqld]
skip-grant-tables
:wq! #保存退出

重啟mysql 就可以進(jìn)入數(shù)據(jù)庫了

- 進(jìn)入mysql,查看root用戶的詳細(xì)信息
SQL:
select * from mysql.user where user='root' \G
[root@localhost mysql]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> select * from mysql.user where user='root' \G
*************************** 1. row ***************************
Host: localhost
User: root
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *84658F872CACC6CFB61752520A0D5E18BEC41D02
password_expired: Y
password_last_changed: 2017-08-27 12:25:07
password_lifetime: NULL
account_locked: N
1 row in set (0.01 sec)
MySQL [(none)]>
3、把password_expired 改成不過期
SQL:
use mysql
update user set password_expired='N' where user='root';
flush privileges;
quit
結(jié)果
MySQL [(none)]> 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 [mysql]> update user set password_expired='N' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MySQL [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL [mysql]> quit
Bye

4、把 /etc/my.cnf 的 skip-grant-tables 這行注釋掉

5、重啟服務(wù)
命令
service mysql.server restart
6、再次登陸 mysql 就正常了

- 接下來 回來繼續(xù)修改
首先需要重置密碼,不然數(shù)據(jù)庫不讓進(jìn)行操作
SQL:
use mysql;
ALTER USER 'root'@'localhost' identified by 'root';
或者
set password=password("root");
flush privileges;
exit;
結(jié)果
MySQL [(none)]> 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 [mysql]> ALTER USER 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.00 sec)
MySQL [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL [mysql]> exit;
Bye
[root@localhost mysql]#
- 最后進(jìn)行授權(quán)
重新登陸使用新密碼
SQL:
mysql -uroot -proot
結(jié)果
[root@localhost mysql]# mysql -uroot -proot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
接下來添加遠(yuǎn)程連接:
SQL:
use mysql;
update user set host = '%' where user ='root';
flush privileges;
結(jié)果
MySQL [(none)]> 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 [mysql]> update user set host = '%' where user ='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MySQL [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL [mysql]>
15.到這里所有的操作都完成了,現(xiàn)在在遠(yuǎn)程機(jī)器上使用客戶端登陸試試吧,
客戶端我選用兩款工具,navicat和Sqlyog
首先關(guān)閉放火墻
命令
systemctl stop firewalld.service


- 修改配置文件my.cnf
命令
vim /etc/my.cnf
# 添加下面數(shù)據(jù)
[mysql]
default-character-set=utf8
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
# 退出重啟
:x
service mysql.server restart

詳細(xì)的配置參照 https://blog.linuxeye.cn/379.html
- 設(shè)置環(huán)境變量profile
vim /etc/profile
MYSQL=/usr/local/mysql
PATH=$MYSQL/bin:$PATH
export MYSQL
# 保存后別忘記使生效
source /etc/profile

- 添加開機(jī)啟動chkconfig
命令:
chmod 755 /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 345 mysql on
結(jié)果:
[root@izj6c06j2vnul9swefj2c4z ~]# chmod 755 etc/init.d/mysql
[root@izj6c06j2vnul9swefj2c4z ~]# chkconfig --add mysql
[root@izj6c06j2vnul9swefj2c4z ~]# chkconfig --level 345 mysql on
后記:
在安裝的過程中,出現(xiàn)很多的問題, 這里簡單記錄下可能的出現(xiàn)的問題
路徑問題
如果安裝的路徑不是 /usr/local/mysql 而是 /home/mysql/mysql-5.7.19-linux-glibc2.12-x86_64
或者其他的什么路徑是,需要啟動數(shù)據(jù)庫前 配置mysql.server
vim support-files/mysql.server
找到文件中配置路徑的basedir 和datadir 進(jìn)行修改成你自己的安裝路徑
basedir=/home/mysql
datadir=/home/mysql/data文件執(zhí)行權(quán)限問題:
將第6步修改給mysql所用戶組的文件 在第9步修改回來,不然有可能出現(xiàn)問題,防火墻一定要設(shè)置,或者關(guān)閉,不然是連不了的。
如果使用的阿里云的,需要設(shè)置控制臺設(shè)備的端口允許IP允許。
點波關(guān)注 系統(tǒng)搭建(docker)