Mysql總結1-mysql安裝
阿新 • • 發佈:2019-01-28
一. Mysql安裝
mysql安裝在linux和windows是不一樣的.
- windows: exe安裝, 綠色版配置安裝
- linux: rpm,tar包,線上安裝等
1.1 安裝包下載
進入mysql官網社群版本下載頁面
選擇相應的系統,CentOS選擇Red Hat Enterprise Linux / Oracle Linux即可(centos:6.8,選擇的redhat linux 6).
1.2 linux下安裝
1.2.0 刪除之前檢視
[root@bogon mysql]# find / -name mysql
/usr/lib64/mysql
/usr/share/mysql
/etc/selinux/targeted/active/modules/100 /mysql
/root/soft/mysql
[root@bogon mysql]#
1.2.1 刪除舊版本資料庫
//1.刪除老版本的MySQL(本次操作使用的是該方法)
//刪除一
rpm -qa | grep -i mysql
yum -y remove mysql-libs*
[[email protected] soft]# rpm -qa | grep -i mysql
mysql-libs-5.1.73-7.el6.x86_64
[[email protected] soft]# yum -y remove mysql-libs*
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package mysql-libs.x 86_64 0:5.1.73-7.el6 will be erased
--> Processing Dependency: libmysqlclient.so.16()(64bit) for package: 2:postfix-2.6.6-6.el6_7.1.x86_64
--> Processing Dependency: libmysqlclient.so.16(libmysqlclient_16)(64bit) for package: 2:postfix-2.6.6-6.el6_7.1.x86_64
--> Processing Dependency: mysql-libs for package: 2 :postfix-2.6.6-6.el6_7.1.x86_64
--> Running transaction check
---> Package postfix.x86_64 2:2.6.6-6.el6_7.1 will be erased
--> Processing Dependency: /usr/sbin/sendmail for package: cronie-1.4.4-15.el6_7.1.x86_64
--> Running transaction check
---> Package cronie.x86_64 0:1.4.4-15.el6_7.1 will be erased
--> Processing Dependency: cronie = 1.4.4-15.el6_7.1 for package: cronie-anacron-1.4.4-15.el6_7.1.x86_64
--> Running transaction check
---> Package cronie-anacron.x86_64 0:1.4.4-15.el6_7.1 will be erased
--> Processing Dependency: /etc/cron.d for package: crontabs-1.10-33.el6.noarch
--> Processing Dependency: /etc/cron.d for package: sysstat-9.0.4-31.el6.x86_64
--> Restarting Dependency Resolution with new changes.
--> Running transaction check
---> Package crontabs.noarch 0:1.10-33.el6 will be erased
---> Package sysstat.x86_64 0:9.0.4-31.el6 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
===================================================================================================================================
Package Arch Version Repository Size
===================================================================================================================================
Removing:
mysql-libs x86_64 5.1.73-7.el6 @anaconda-CentOS-201605220104.x86_64/6.8 4.0 M
Removing for dependencies:
cronie x86_64 1.4.4-15.el6_7.1 @anaconda-CentOS-201605220104.x86_64/6.8 174 k
cronie-anacron x86_64 1.4.4-15.el6_7.1 @anaconda-CentOS-201605220104.x86_64/6.8 43 k
crontabs noarch 1.10-33.el6 @anaconda-CentOS-201605220104.x86_64/6.8 2.4 k
postfix x86_64 2:2.6.6-6.el6_7.1 @anaconda-CentOS-201605220104.x86_64/6.8 9.7 M
sysstat x86_64 9.0.4-31.el6 @anaconda-CentOS-201605220104.x86_64/6.8 826 k
Transaction Summary
===================================================================================================================================
Remove 6 Package(s)
Installed size: 15 M
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Erasing : sysstat-9.0.4-31.el6.x86_64 1/6
Erasing : crontabs-1.10-33.el6.noarch 2/6
Erasing : cronie-anacron-1.4.4-15.el6_7.1.x86_64 3/6
Erasing : cronie-1.4.4-15.el6_7.1.x86_64 4/6
Erasing : 2:postfix-2.6.6-6.el6_7.1.x86_64 5/6
Erasing : mysql-libs-5.1.73-7.el6.x86_64 6/6
Verifying : cronie-1.4.4-15.el6_7.1.x86_64 1/6
Verifying : mysql-libs-5.1.73-7.el6.x86_64 2/6
Verifying : cronie-anacron-1.4.4-15.el6_7.1.x86_64 3/6
Verifying : crontabs-1.10-33.el6.noarch 4/6
Verifying : sysstat-9.0.4-31.el6.x86_64 5/6
Verifying : 2:postfix-2.6.6-6.el6_7.1.x86_64 6/6
Removed:
mysql-libs.x86_64 0:5.1.73-7.el6
Dependency Removed:
cronie.x86_64 0:1.4.4-15.el6_7.1 cronie-anacron.x86_64 0:1.4.4-15.el6_7.1 crontabs.noarch 0:1.10-33.el6
postfix.x86_64 2:2.6.6-6.el6_7.1 sysstat.x86_64 0:9.0.4-31.el6
Complete!
//刪除二
安裝新版mysql之前,我們需要將系統自帶的mariadb-lib解除安裝
[root@5201351 ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.41-2.el7_0.x86_64
或者試試
rpm -e --nodeps mariadb-libs-1:5.5.52-1.el7.x86_64(以下報錯通過該命令執行解決問題)
否則安裝會報錯
[[email protected] mysql]# rpm -ivh mysql-community-common-5.7.20-1.el7.x86_64.rpm
warning: mysql-community-common-5.7.20-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
file /usr/share/mysql/czech/errmsg.sys from install of mysql-community-common-5.7.20-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.52-1.el7.x86_64
file /usr/share/mysql/danish/errmsg.sys from install of mysql-community-common-5.7.20-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.52-1.el7.x86_64
file /usr/share/mysql/dutch/errmsg.sys from install of mysql-community-common-5.7.20-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.52-1.el7.x86_64
file /usr/share/mysql/english/errmsg.sys from install of mysql-community-common-5.7.20-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.52-1.el7.x86_64
file /usr/share/mysql/estonian/errmsg.sys from install of mysql-community-common-5.7.20-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.52-1.el7.x86_64
file /usr/share/mysql/french/errmsg.sys from install of mysql-community-common-5.7.20-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.52-1.el7.x86_64
[root@5201351 ~]# rpm -e mariadb-libs-5.5.41-2.el7_0.x86_64 --nodeps
附:解除安裝mariadb(總有一個適合)
systemctl stop mariadb
rpm -qa | grep mariadb
rpm -e --nodeps mariadb-5.5.52-1.el7.x86_64
rpm -e --nodeps mariadb-server-5.5.52-1.el7.x86_64
rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64
//確定是否都刪乾淨了.
find / -name mysql
1.2.2 tar安裝
1.2.2.1 準備安裝檔案
//1.準備安裝檔案包
//1.1 新建資料夾儲存tar檔案
[[email protected] ~]# mkdir soft
[[email protected] ~]# mv mysql-5.7.19-1.el6.x86_64.rpm-bundle.tar soft/
//1.2.解壓tar包
[[email protected] soft]# tar -xvf mysql-5.7.19-1.el6.x86_64.rpm-bundle.tar
mysql-community-libs-5.7.19-1.el6.x86_64.rpm #MySQL的庫檔案
mysql-community-devel-5.7.19-1.el6.x86_64.rpm #MySQL的標頭檔案
mysql-community-embedded-5.7.19-1.el6.x86_64.rpm #MySQL的嵌入式程式
mysql-community-common-5.7.19-1.el6.x86_64.rpm #mysql公用包
mysql-community-libs-compat-5.7.19-1.el6.x86_64.rpm
mysql-community-server-5.7.19-1.el6.x86_64.rpm #MySQL服務端程式
mysql-community-test-5.7.19-1.el6.x86_64.rpm
mysql-community-embedded-devel-5.7.19-1.el6.x86_64.rpm
mysql-community-client-5.7.19-1.el6.x86_64.rpm #MySQL客戶端程式
[[email protected] soft]#
進行mysql服務安裝,需要以下rpm,按照順序進行
//1.3 準備以下檔案待使用
mysql-community-common-5.7.19-1.el6.x86_64.rpm //mysql公用包
mysql-community-libs-5.7.19-1.el6.x86_64.rpm //MySQL的庫檔案,依賴於common
mysql-community-client-5.7.19-1.el6.x86_64.rpm //MySQL客戶端程式,依賴於libs
mysql-community-server-5.7.19-1.el6.x86_64.rpm //MySQL服務端程式,依賴於client、common
//如果直接安裝rpm會報下錯:
[[email protected] ~]# rpm -ivh mysql-community-server-5.7.19-1.el6.x86_64.rpm
warning: mysql-community-server-5.7.19-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
mysql-community-client(x86-64) >= 5.7.9 is needed by mysql-community-server-5.7.19-1.el6.x86_64
mysql-community-common(x86-64) = 5.7.19-1.el6 is needed by mysql-community-server-5.7.19-1.el6.x86_64
[[email protected] ~]#
1.2.2.2 安裝資料庫
//2.安裝資料庫
//2.1安裝前檢視檔案效果
[root@bogon soft]# find / -name mysql
[root@bogon soft]#
//2.2 按照順序按照rpm包
[root@bogon soft]# rpm -ivh mysql-community-common-5.7.19-1.el6.x86_64.rpm
warning: mysql-community-common-5.7.19-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-community-common ########################################### [100%]
[root@bogon soft]# rpm -ivh mysql-community-libs-5.7.19-1.el6.x86_64.rpm
warning: mysql-community-libs-5.7.19-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-community-libs ########################################### [100%]
[root@bogon soft]# rpm -ivh mysql-community-client-5.7.19-1.el6.x86_64.rpm
warning: mysql-community-client-5.7.19-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-community-client ########################################### [100%]
[root@bogon soft]# rpm -ivh mysql-community-server-5.7.19-1.el6.x86_64.rpm
warning: mysql-community-server-5.7.19-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-community-server ########################################### [100%]
[root@bogon soft]#
//2.3 檢視下安裝後的效果
[root@bogon soft]# find / -name mysql
/var/lib/mysql
/etc/logrotate.d/mysql
/usr/share/mysql
/usr/lib64/mysql
/usr/bin/mysql
[root@bogon soft]#
安裝中的問題
[email protected] mysql]# rpm -ivh mysql-community-server-5.7.20-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.20-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.20-1.el7.x86_64
libnuma.so.1(libnuma_1.1)(64bit) is needed by mysql-community-server-5.7.20-1.el7.x86_64
libnuma.so.1(libnuma_1.2)(64bit) is needed by mysql-community-server-5.7.20-1.el7.x86_64
[[email protected] mysql]# yum install libnuma*
1.2.2.3 初始化資料庫
//3.1 初始化資料庫
//3.1.1 初始化資料庫方法一(本次操作用該種方法,根據提示檢視更建議使用方法二)
//3.1.1.1 執行命令
//必須指定datadir,(/usr/bin/mysql_install_db)
//執行後會生成~/.mysql_secret密碼檔案 (/root/.mysql_secret)
[[email protected] bin]# mysql_install_db --datadir=/var/lib/mysql
2017-10-07 20:36:21 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
//3.1.1.2 檢視/etc/my.cnf檔案產生了
[[email protected] soft]# ll /etc/my.cnf
-rw-r--r--. 1 root root 960 Jun 22 08:30 /etc/my.cnf
//3.1.2 初始化資料庫方法二:
[[email protected] ~]# mysqld --initialize
//新版的推薦此方法,執行生會在/var/log/mysqld.log生成隨機密碼
//3.2 賦給mysql使用者許可權並啟動服務
[[email protected] bin]#chown mysql:mysql /var/lib/mysql -R
如果不賦給許可權會啟動報錯.
[[email protected] soft]# service mysqld start
MySQL Daemon failed to start.
Starting mysqld: [FAILED]
//3.3 檢視安裝目錄
[[email protected] ~]# cd /var/lib/mysql
[[email protected] mysql]# ll
total 110652
-rw-r-----. 1 mysql mysql 56 Oct 7 20:36 auto.cnf
-rw-------. 1 mysql mysql 1679 Oct 7 20:36 ca-key.pem
-rw-r--r--. 1 mysql mysql 1074 Oct 7 20:36 ca.pem
-rw-r--r--. 1 mysql mysql 1078 Oct 7 20:36 client-cert.pem
-rw-------. 1 mysql mysql 1679 Oct 7 20:36 client-key.pem
-rw-r-----. 1 mysql mysql 420 Oct 7 20:36 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Oct 7 20:36 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Oct 7 20:36 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Oct 7 20:36 ib_logfile1
drwxr-x---. 2 mysql mysql 4096 Oct 7 20:36 mysql
drwxr-x---. 2 mysql mysql 4096 Oct 7 20:36 performance_schema
-rw-------. 1 mysql mysql 1679 Oct 7 20:36 private_key.pem
-rw-r--r--. 1 mysql mysql 451 Oct 7 20:36 public_key.pem
-rw-r--r--. 1 mysql mysql 1078 Oct 7 20:36 server-cert.pem
-rw-------. 1 mysql mysql 1675 Oct 7 20:36 server-key.pem
drwxr-x---. 2 mysql mysql 12288 Oct 7 20:36 sys
//3.4 檢視/etc/my.cnf
[[email protected] etc]# more /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysq
//3.5 啟動msyql服務
[[email protected] etc]# service mysqld start
Starting mysqld: [ OK ]
[[email protected] etc]#
//3.6檢視埠
netstat -atln
[[email protected] etc]# netstat -atln
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 64 192.168.17.129:22 192.168.17.1:52520 ESTABLISHED
tcp 0 0 192.168.17.129:22 192.168.17.1:60942 ESTABLISHED
tcp 0 0 192.168.17.129:22 192.168.17.1:51222 ESTABLISHED
tcp 0 0 192.168.17.129:22 192.168.17.1:52611 ESTABLISHED
tcp 0 0 192.168.17.129:46672 23.33.178.25:80 ESTABLISHED
tcp 0 0 :::3306 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:631 :::* LISTEN
[[email protected] etc]#
//3306埠已經監聽.說明啟動成功
1.2.2.4 服務端登入連線檢視資料庫
//4.1 檢視預設密碼,準備登陸,並重設密碼
[email protected] mysql]# cat /root/.mysql_secret
# Password set for user '[email protected]' at 2017-10-07 20:36:21
;g(%3xp4eiTX
[[email protected] mysql]#
如果用的是第二種初始化方法,密碼位置為
[[email protected] mysql]# more /var/log/mysqld.log
2017-11-22T06:03:58.568894Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-11-22T06:03:59.827641Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-11-22T06:04:00.103434Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-11-22T06:04:00.216796Z 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: eefcdbee-cf4a-11e7-
8dc1-5254000cbc62.
2017-11-22T06:04:00.238617Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-11-22T06:04:00.239169Z 1 [Note] A temporary password is generated for [email protected]: Os3aWF-#<1eo
//4.2.1 登陸連線資料庫
[[email protected] etc]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.19
Copyright (c) 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>
//4.2.2 提示必須要重設密碼
mysql> show variables like '%char%';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>
//4.3 重置使用者密碼
mysql> alter user 'root'@'localhost' identified by '111111';
Query OK, 0 rows affected (0.00 sec)
mysql>
[[email protected] etc]# service mysqld restart;
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[[email protected] etc]#
//4.4.1 檢視資料庫編碼
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| 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 | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
發現 character_set_database與character_set_server 使用的是西歐編碼
更改下編碼準備修改為utf8編碼
//4.4.2 修改資料庫編碼為utf8編碼
mysql>
mysql> exit;
Bye
[[email protected] etc]# vim /etc/my.cnf
在my.cnf中的mysqld 中增加
character_set_server=utf8
init_connect='SET NAMES utf8'
追加完如下
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character_set_server=utf8
init_connect='SET NAMES utf8'
~
~
儲存, 重啟mysql
[[email protected] soft]# service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[[email protected] soft]#
//4.4.3 檢視修改資料庫編碼後的編碼
[[email protected] etc]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.19 MySQL Community Server (GPL)
Copyright (c) 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> show variables like '%char%';
+--------------------------+----------------------------+
| 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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql> exit;
Bye
//4.5重啟mysql服務
[[email protected]on etc]# service mysqld restart;
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[[email protected] etc]#
1.2.2.5 遠端登入連線檢視資料庫
前面的操作此時可以在本機連線了.
下面開啟遠端連線.
//5.1 連線服務
[[email protected] etc]# mysql -uroot -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
//5.2 使用msyql資料庫
mysql> use mysql;
//5.3 查看錶結構
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition