計劃搭建MyCat服務,先搭建好數據庫環境,共在VMwar上安裝了三臺Linux主機,系統爲CentOS7,主機名分別爲:itcast-01;itcast-02;itcast-nginx
這裏以itcast-02爲例搭建數據庫的全流程
博客編寫不易,看過請點贊
安裝配置Mysql大體步驟爲三步
- 下載數據庫的壓縮包或二進制包,可以在linux用wget或yum下載,也可以外網下載再傳到Linux
- 數據庫初始化及環境配置
- 登陸數據庫修改密碼並實現遠程連接
步驟一: 下載解壓mysql數據庫
1.1 mysql安裝文件,我這裏用的是5.7x版本,目錄爲/usr/local/
mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
1.2 解壓Mysql安裝文件,執行命令
tar -xvf mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
## 解壓後
drwxr-xr-x. 9 root root 160 8月 2 2019 apache-tomcat-8.5.32
-rw-r--r--. 1 root root 9584807 8月 2 2019 apache-tomcat-8.5.32.tar.gz
drwxr-xr-x. 2 root root 6 11月 5 2016 bin
drwxr-xr-x. 2 root root 6 11月 5 2016 etc
drwxr-xr-x. 2 root root 6 11月 5 2016 games
drwxr-xr-x. 2 root root 6 11月 5 2016 include
drwxr-xr-x. 8 10 143 255 3月 29 2018 jdk1.8.0_172
-rw-r--r--. 1 root root 190921804 8月 2 2019 jdk-8u172-linux-x64.tar.gz
drwxr-xr-x. 2 root root 6 11月 5 2016 lib
drwxr-xr-x. 2 root root 6 11月 5 2016 lib64
drwxr-xr-x. 2 root root 6 11月 5 2016 libexec
drwxr-xr-x. 9 root root 129 2月 23 11:25 mysql-5.7.15-linux-glibc2.5-x86_64
-rw-r--r--. 1 root root 641556368 2月 23 11:22 mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
drwxr-xr-x. 2 root root 6 11月 5 2016 sbin
drwxr-xr-x. 5 root root 49 8月 1 2019 share
drwxr-xr-x. 2 root root 6 11月 5 2016 src
1.3 修改解壓後的文件名爲mysql
mv mysql-5.7.15-linux-glibc2.5-x86_64 ./mysql
文件名修改後
drwxr-xr-x. 9 root root 160 8月 2 2019 apache-tomcat-8.5.32
-rw-r--r--. 1 root root 9584807 8月 2 2019 apache-tomcat-8.5.32.tar.gz
drwxr-xr-x. 2 root root 6 11月 5 2016 bin
drwxr-xr-x. 2 root root 6 11月 5 2016 etc
drwxr-xr-x. 2 root root 6 11月 5 2016 games
drwxr-xr-x. 2 root root 6 11月 5 2016 include
drwxr-xr-x. 8 10 143 255 3月 29 2018 jdk1.8.0_172
-rw-r--r--. 1 root root 190921804 8月 2 2019 jdk-8u172-linux-x64.tar.gz
drwxr-xr-x. 2 root root 6 11月 5 2016 lib
drwxr-xr-x. 2 root root 6 11月 5 2016 lib64
drwxr-xr-x. 2 root root 6 11月 5 2016 libexec
drwxr-xr-x. 9 root root 129 2月 23 11:25 mysql
-rw-r--r--. 1 root root 641556368 2月 23 11:22 mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
drwxr-xr-x. 2 root root 6 11月 5 2016 sbin
drwxr-xr-x. 5 root root 49 8月 1 2019 share
drwxr-xr-x. 2 root root 6 11月 5 2016 src
1.4 創建組和用戶
[root@itcast-02 local]# groupadd mysql
[root@itcast-02 local]# useradd -r -g mysql mysql
1.5 將安裝目錄的權限改爲mysql
[root@itcast-02 local]# chown -R mysql.mysql /usr/local/mysql
## 查看mysql目錄結構
drwxr-xr-x. 2 mysql mysql 4096 2月 23 11:26 bin
-rw-r--r--. 1 mysql mysql 17987 8月 25 2016 COPYING
drwxr-xr-x. 2 mysql mysql 6 2月 23 11:29 data
drwxr-xr-x. 2 mysql mysql 55 2月 23 11:26 docs
drwxr-xr-x. 3 mysql mysql 4096 2月 23 11:25 include
drwxr-xr-x. 5 mysql mysql 229 2月 23 11:26 lib
drwxr-xr-x. 4 mysql mysql 30 2月 23 11:25 man
-rw-r--r--. 1 mysql mysql 2478 8月 25 2016 README
drwxr-xr-x. 28 mysql mysql 4096 2月 23 11:26 share
drwxr-xr-x. 2 mysql mysql 112 2月 23 11:26 support-files
步驟二:數據庫初始化及環境配置
2.1 設置mysql數據庫data目錄,在mysql安裝目錄下創建
[root@itcast-02 mysql]# mkdir data
2.2 初始化mysql數據庫,注意最後一行的初始化密碼
[root@itcast-02 mysql]# /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
2020-02-23T03:30:05.117588Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-02-23T03:30:05.370286Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-02-23T03:30:05.504257Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-02-23T03:30:05.566668Z 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: c8ad537b-55ec-11ea-a3bc-000c29936866.
2020-02-23T03:30:05.568472Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-02-23T03:30:05.569995Z 1 [Note] A temporary password is generated for root@localhost: +2m)Q)g6Ks9/
2.3 修改mysql配置文件my.cnf
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
# 取消密碼驗證
#skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# skip-grant-tables
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
2.4 設置開機啓動
[root@itcast-02 var]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[root@itcast-02 var]# chkconfig mysql on
## 查看
[root@itcast-02 var]# chkconfig
注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。
要列出 systemd 服務,請執行 'systemctl list-unit-files'。
查看在具體 target 啓用的服務請執行
'systemctl list-dependencies [target]'。
mysql 0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
2.5 啓動mysql服務,成功提示SUCCESS
[root@itcast-02 var]# service mysql start
Starting MySQL. SUCCESS!
[root@itcast-02 var]# netstat -nltp;
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1525/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1113/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1111/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1344/master
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 1113/sshd
tcp6 0 0 ::1:631 :::* LISTEN 1111/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 1344/master
tcp6 0 0 :::3306 :::* LISTEN 3675/mysqld
[root@itcast-02 var]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 3675 mysql 20u IPv6 41045 0t0 TCP *:mysql (LISTEN)
步驟三:修改密碼設置遠程連接
3.1 登錄mysql
[root@itcast-02 var]# mysql -uroot -p;
Enter password:
如果提示: bash: mysql: 未找到命令…
原因:
因爲系統默認會查找/usr/bin下的命令連接,由於mysql沒有在這個目錄下,所以出現not found。因此需要做一個軟連接到/usr/bin目錄下。
解決:
ln -s /usr/local/mysql/bin/mysql /usr/bin
3.2 登錄之後必須先修改初始密碼,否則無法進行數據庫操作
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by 'root123';
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
3.3 切換到mysql數據庫,修改root用戶信息,開放遠程連接服務
mysql> 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> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
mysql默認用戶表 user是用戶名 host是地址(正常顯示localhost(本地),如果允許其它訪問需更改成%)
3.4 給用戶授權
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root123' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
示例說明:
GRANT privileges ON databasename.tablename TO ‘username’@‘host’ IDENTIFIED BY ‘username’ WITH GRANT OPTION;
privileges:表示要授予什麼權力,例如可以有 select , insert ,delete,update等,如果要授予全部權力,則填 ALL
databasename.tablename:表示用戶的權限能用在哪個庫的哪個表中,如果想要用戶的權限很作用於所有的數據庫所有的表,則填 .,*是一個通配符,表示全部。
’username‘@‘host’:表示授權給哪個用戶。
其中兩個username 分別代表 “用戶名”和“密碼”
最後通過Navicat工具遠程連接itcast02的Mysql數據庫
********************
*General Information
********************
服務器類型: MySQL
連接名: itcast02
主機名或 IP 地址: 192.168.79.131
端口: 3306
用戶名: root
保存密碼: True
********************
*Advanced Information
********************
設置位置: C:\Users\張江豐\Documents\Navicat\MySQL\servers\itcast02
編碼: 65001 (UTF-8)
保持連接間隔 (秒): N/A
使用 MySQL 字符集: True
使用壓縮: False
自動連接: False
使用高級連接: False
********************
*SSL Information
********************
使用 SSL: False
使用驗證: False
客戶端密鑰:
客戶端證書:
CA 證書:
********************
*SSH Information
********************
使用 SSH 通道: False
主機名或 IP 地址:
端口: 22
用戶名:
驗證方法: 密碼
保存密碼: False
********************
*HTTP Information
********************
使用 HTTP 通道: False
通道地址:
用 base64 編碼傳出查詢: False
使用密碼驗證: False
用戶名:
保存密碼: False
使用證書驗證: False
客戶端密鑰:
客戶端證書:
CA 證書:
Use Proxy: False
代理服務器 主機:
代理服務器 端口: 0
代理服務器 用戶名:
代理服務器 保存密碼: False
********************
*Other Information
********************
服務器版本: 5.7.15
通訊協定: 10
信息: 192.168.79.131 via TCP/IP