mysql搭建tar包安裝方式
1.從官網下載mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
2.建立mysql的使用者組/使用者,data目錄及其使用者目錄
#mkdir /usr/local/mysql #在/usr/local資料夾下建立mysql檔案
#mkdir /usr/local/mysql/data #在mysql資料夾下建立data檔案
#groupadd mysql #建立一個名為mysql的使用者組
#useradd -g mysql -d #在使用者組下 建立使用者
3.解壓安裝包並將解壓包裡的內容拷貝到mysql的安裝目錄/usr/local/mysql
#tar -zxvf mysql-5.7.18-linux-glib2.5-x86_64.tar.gz #解壓檔案
#cd mysql-5.7.18-linux-glib2.5-x86_64 #進入解壓檔案
#mv * /usr/local/mysql #移動到之前的mysql檔案下
4.初始化mysql資料庫
#cd /usr/local/myslq
#./bin/mysql –user=mysql –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details). 2016-04-08T01:47:59.945537Z 0
[Warning] InnoDB: New log files created, LSN=45790 2016-04-08T01:48:00.333528Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2016-04-08T01:48:00.434908Z 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: ece26421-fd2b-11e5-a1e3-00163e001e5c. 2016-04-08T01:48:00.440125Z 0
[Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2016-04-08T01:48:00.440904Z 1
[Note] A temporary password is generated for
記住上面的隨機密碼,如上mjT,#x_5sW修改密碼時需要用到
5. 檢測下是否能啟動mysql服務
#cd /usr/local/myslq
#./support-files/mysql.server start
Starting MySql..ok!
以上是正常情況,啟動如果出錯,會出現下面資訊
# ./support-files/mysql.server start
./support-files/mysql.server: line 276: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
這時候我們需要修改/support-files/mysql.server檔案的basedir和datadir目錄路徑為我們環境所在的mysql的basedir和datadir路徑, 如下:
# vim support-files/mysql.server
————————–
…
basedir=/home/mysql
datadir=/home/mysql/data
…
————————–
# ./support-files/mysql.server start
start Starting MySQL.. OK!
6. 建立配置檔案
a.將預設生成的my.cnf備份
# mv /etc/my.cnf /etc/my.cnf.bak
b、進入mysql的安裝目錄支援檔案目錄
# cd /home/mysql/support-files
c、拷貝配置檔案模板為新的mysql配置檔案,
# cp my-default.cnf /etc/my.cnf
d、設定編碼,可按需修改新的配置檔案選項, 不修改配置選項, mysql則按預設配置引數執行. 修改配置檔案/etc/my.cnf, 設定編碼為utf8以防亂碼
# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
character_set_server=utf8
init_connect='SET NAMES utf8'
[client]
default-character-set=utf8
有的時候使用這個命令後,前3步有問題的這裡可能my.cnf檔案是空的,這個不要緊,既然沒有生成,那麼就自己編輯
7.配置mysql服務開機自動啟動
#cp/usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 拷貝啟動檔案到/etc/init.d/下並重命令為mysqld
# chmod 755 /etc/init.d/mysqld # 增加執行許可權
# chkconfig –list mysqld # 檢查自啟動項列表中沒有mysqld
# chkconfig –add mysqld # 如果沒有就新增mysqld:
# chkconfig mysqld on # 用這個命令設定開機啟動:
8. mysql服務的啟動/重啟/停止
# service mysqld start # 啟動服務
# service mysqld restart # 重啟服務
# service mysqld stop # 停止服務
9. 初始化mysql使用者root的密碼
#mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright © 2000, 2017, 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> use mysql
mysql>update user set authentication_string = PASSWORD('123456') where user = 'root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
mysql>flush privileges; #更改密碼後需用flush privileges重新整理MySQL的系統許可權相關表,否則會出現拒絕訪問
10.更改一些編碼
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> \s
————–
mysql Ver 14.14 Distrib 5.7.18, for linux-glibc2.5 (x86_64) using EditLine wrapper
Connection id: 7
Current database: mysql
Current user: [email protected]
SSL: Not in use
Current pager: stdout
Using outfile:
Using delimiter: ;
Server version: 5.7.18 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: latin1 —-需要修改成你想要的編碼
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 3 days 3 min 21 sec Threads: 2 Questions: 88 Slow queries: 0 Opens: 158 Flush tables: 1 Open tables: 153 Queries per second avg: 0.000
————– mysql> show variables like 'character%';
+————————–+———————————-+
| Variable_name | Value |
+————————–+———————————-+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+————————–+———————————-+
8 rows in set (0.00 sec) mysql> mysql> SET character_set_database = utf8; —設定編碼
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> show variables like 'character%';
+————————–+———————————-+
| Variable_name | Value |
+————————–+———————————-+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+————————–+———————————-+
8 rows in set (0.00 sec) mysql> \s
————–
mysql Ver 14.14 Distrib 5.7.18, for linux-glibc2.5 (x86_64) using EditLine wrapper Connection id: 7
Current database: mysql
Current user: [email protected]
SSL: Not in use
Current pager: stdout
Using outfile:
Using delimiter: ;
Server version: 5.7.18 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 3 days 6 min 39 sec
Threads: 2 Questions: 94 Slow queries: 0 Opens: 159 Flush tables: 1 Open tables: 154 Queries per second avg: 0.000
mysql>