數據庫排名mysql
RDBMS:Oracle、MySQL、PG、MSSQL NoSQL:MongoDB、Redis、ES NEWSQL(分佈式):TiDBit、Spanner、AliSql(RDS+DRDS)、OB、PolarDBlinux
我司版本 5.7.16,發佈時間:2016-9-28sql
下載方式:shell
Mysql -> DOWNLOADS -> MySQL Community (GPL) Downloads -> MySQL Community Server -> Archives數據庫
選擇想要下載的版本便可。vim
ssh root@IP
複製代碼
我喜歡將項目放在 /home/work
目錄下,服務放在 /home/service/
目錄下,好比 Nginx,MySQL,軟件放在 /home/software
目錄下。centos
/home/service/
目錄和 /home/software
目錄,不使用 root 用戶建立,避免一些權限問題。# useradd -m work
# su - work
$ mkdir /home/software
$ mkdir /home/service
複製代碼
我選擇的 MySQL 版本是 mysql5.7.26安全
$ cd /home/software
$ wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
$ mv mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz mysql-5.7.26
複製代碼
$ tar -zxvf mysql-5.7.26 -C /home/service/
$ mv /home/service/mysql-5.7.26-linux-glibc2.12-x86_64/ /home/service/mysql
複製代碼
咱們須要建立一個用於運行 mysqld
的用戶和組,MySQL 須要獨立的用戶來進行數據管理,咱們建立用戶爲 mysql,這是 MySQL 內置本身使用的用戶,咱們建立出來便可。bash
$ exit
# useradd -s /sbin/nologin mysql
# id mysql
uid=1001(mysql) gid=1001(mysql) 組=1001(mysql)
複製代碼
修改環境變量的目的就是爲了未來使用命令方便一些。markdown
# echo 'export PATH=/home/service/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
# source /etc/profile
# mysql -V
mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper
複製代碼
安裝 MySQL,必須初始化數據目錄,包括 mysql 系統數據庫中的 table:
brew install mysql
。# mkdir /data/mysql/data -p
# mysqld --initialize --user=mysql --basedir=/home/service/mysql --datadir=/data/mysql/data
mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
# yum -y install numactl
# mysqld --initialize --user=mysql --basedir=/home/service/mysql --datadir=/data/mysql/data
2021-01-13T14:17:51.963896Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-13T14:17:52.304834Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-13T14:17:52.399099Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-13T14:17:52.473765Z 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: 1f69721e-55aa-11eb-a47c-525400ea4205.
2021-01-13T14:17:52.477812Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-13T14:17:52.479083Z 1 [Note] A temporary password is generated for root@localhost: wZDqM,ATd3Fb
複製代碼
說明:
--initialize 參數做用:
固然咱們能夠選擇不設置密碼,使用以下命令:
# rm -rf /data/mysql/data/*
# mysqld --initialize-insecure --user=mysql --basedir=/home/service/mysql --datadir=/data/mysql/data
2021-01-13T14:25:01.404438Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-13T14:25:01.820712Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-13T14:25:01.941025Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-13T14:25:02.021329Z 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: 1f713392-55ab-11eb-aca4-525400ea4205.
2021-01-13T14:25:02.028582Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-13T14:25:02.029771Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
複製代碼
說明:
--initialize-insecure 參數做用:
注:mysql5.6 初始化數據方法:
/home/service/mysql/scripts/mysql_install_db
複製代碼
將 mysql 運行目錄和數據目錄受權
# chown -R mysql.mysql /home/service/mysql/*
# chown -R mysql.mysql /data
複製代碼
mysql 的配置文件是 /etc/my.cnf
,當咱們想要自定義配置一些文件時,可在該文件中配置。
cat > /etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/home/service/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=6
port=3306
[mysql]
socket=/tmp/mysql.sock
EOF
複製代碼
# cp /home/service/mysql/support-files/mysql.server /etc/init.d/mysqld
# service mysqld start
Starting MySQL.Logging to '/data/mysql/data/VM-0-3-centos.err'.
SUCCESS!
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)
......
複製代碼
# netstat -lnp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 13215/mysqld
# ps -ef | grep mysql
root 13049 1 0 11:22 pts/0 00:00:00 /bin/sh /home/service/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/data/VM-0-3-centos.pid
mysql 13215 13049 0 11:22 pts/0 00:00:03 /home/service/mysql/bin/mysqld --basedir=/home/service/mysql --datadir=/data/mysql/data --plugin-dir=/home/service/mysql/lib/plugin --user=mysql --log-error=VM-0-3-centos.err --pid-file=/data/mysql/data/VM-0-3-centos.pid --socket=/tmp/mysql.sock --port=3306
複製代碼
這裏列舉一些比較常見的錯誤處理方式。
咱們來試驗一下,假設當咱們把一些數據文件權限修改的時候,數據庫應該就啓動不了了。
# service mysqld stop
# cd /data/mysq/data
# chown root.root ibdata1
# service mysqld start
Starting MySQL.. ERROR! The server quit without updating PID file (/data/mysql/data/VM-0-3-centos.pid).
複製代碼
但其實咱們沒法經過該條日誌來判斷是什麼緣由致使的 MySQL 沒法啓動的,由於 MySQL 啓動失敗的日誌都是這個。那麼咱們應該如何處理呢,在數據目錄下,也就是 /data/mysql/data
下,有一個錯誤文件,文件名.err
,好比個人是 VM-0-3-centos.err
。咱們來查看一下日誌:
# vim VM-0-3-centos.err
......
2021-01-14T06:32:55.240343Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2021-01-14T06:32:55.240358Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2021-01-14T06:32:55.240363Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2021-01-14T06:32:55.840908Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2021-01-14T06:32:55.840940Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2021-01-14T06:32:55.840947Z 0 [ERROR] Failed to initialize builtin plugins.
2021-01-14T06:32:55.840957Z 0 [ERROR] Aborting
......
複製代碼
其中的一些錯誤日誌以下,'ibdata1' must be writable
這條錯誤日誌能夠看到,多是 ibdata1
文件有問題,正好對應了咱們修改了文件的權限。
更極端的一種狀況,日誌也沒法寫入,好比:
# cd /data/mysq/data
# chown -R root.root *
複製代碼
那麼咱們能夠採用以下方式,將日誌輸出到屏幕上:
# service mysqld --defaults-file=/etc/my.cnf
Usage: mysqld {start|stop|restart|reload|force-reload|status} [ MySQL server options ]
[root@VM-0-3-centos data]# /home/service/mysql/bin/mysqld --defaults-file=/etc/my.cnf
2021-01-14T06:48:51.325923Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-14T06:48:51.326061Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2021-01-14T06:48:51.326094Z 0 [Note] /home/service/mysql/bin/mysqld (mysqld 5.7.26) starting as process 19593 ...
2021-01-14T06:48:51.333910Z 0 [Note] InnoDB: PUNCH HOLE support available
2021-01-14T06:48:51.333941Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2021-01-14T06:48:51.333947Z 0 [Note] InnoDB: Uses event mutexes
2021-01-14T06:48:51.333953Z 0 [Note] InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
2021-01-14T06:48:51.333957Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2021-01-14T06:48:51.333962Z 0 [Note] InnoDB: Using Linux native AIO
2021-01-14T06:48:51.334272Z 0 [Note] InnoDB: Number of pools: 1
2021-01-14T06:48:51.334378Z 0 [Note] InnoDB: Using CPU crc32 instructions
2021-01-14T06:48:51.336333Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2021-01-14T06:48:51.346967Z 0 [Note] InnoDB: Completed initialization of buffer pool
2021-01-14T06:48:51.349513Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2021-01-14T06:48:51.359267Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2021-01-14T06:48:51.359287Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2021-01-14T06:48:51.359292Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2021-01-14T06:48:51.959778Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2021-01-14T06:48:51.959804Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2021-01-14T06:48:51.959811Z 0 [ERROR] Failed to initialize builtin plugins.
2021-01-14T06:48:51.959831Z 0 [ERROR] Aborting
2021-01-14T06:48:51.959848Z 0 [Note] Binlog end
2021-01-14T06:48:51.959901Z 0 [Note] Shutting down plugin 'MyISAM'
2021-01-14T06:48:51.959917Z 0 [Note] Shutting down plugin 'CSV'
2021-01-14T06:48:51.960368Z 0 [Note] /home/service/mysql/bin/mysqld: Shutdown complete
複製代碼
# mysqladmin -uroot -p舊密碼 password 新密碼
複製代碼
修改了新密碼以後,我沒法直接經過不輸入密碼登錄了:
mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
複製代碼
這個時候怎麼辦呢,我把密碼忘記了,登不進去了啊,不要緊,咱們能夠採用安全模式登錄。
首先,咱們先關閉 MySQL
# service mysqld stop
複製代碼
而後執行以下命令:
# mysqld_safe --skip-grant-tables --skip-networking &
複製代碼
--skip-grant-tables
參數含義是跳過受權表;--skip-networking
參數含義是跳過遠程登陸;執行成功後,咱們登錄一下 MySQL,發現能夠正常登陸了。
# mysql
複製代碼
登陸進去後,咱們查看一下 root 用戶的帳號密碼:
mysql> use mysql;
select user,host,authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)
複製代碼
咱們修改一下密碼,爲了之後方便登陸,咱們仍是不設置密碼了
grant all on *.* to root@'localhost' identified by '';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
複製代碼
執行失敗了,是由於咱們跳過權限表了,這個時候咱們就把權限表加載進來便可,而後再執行一下上述命令。
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to root@'localhost' identified by '';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select user,host,authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)
複製代碼
而後咱們退出從新登陸一下。
mysql> exit
Bye
複製代碼
# service mysqld stop
Shutting down MySQL..2021-01-14T09:21:50.663434Z mysqld_safe mysqld from pid file /data/mysql/data/VM-0-3-centos.pid ended
SUCCESS!
[1]+ 完成 mysqld_safe --skip-grant-tables --skip-networking
# service mysqld start
Starting MySQL. SUCCESS!
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
......
複製代碼
到這裏,咱們的 MySQL 安裝使用基本就結束了。
今天呢,咱們學習了 MySQL 的安裝使用,咱們採用了通用二進制文件的方式來進行 MySQL 安裝。
按照上面的步驟走一遍呢,咱們基本上就能本身手動安裝一個 MySQL 了。
上面的一些重要命令你們仍是要記住滴!