【MySQL】安裝與配置

kingdelee發表於2019-07-24

1.官方安裝文件:
https://dev.mysql.com/doc/mysql-yum-repo-q...
2.映象下載地址:
http://mirrors.sohu.com/mysql/MySQL-5.7/
3.查詢是否已經有mysql:
如果有,必須先刪除

rpm -qa | grep -i mysql
yum remove -y mysql*
rpm -qa | grep -i mysql

方式1:通過rpm下載安裝

下載以下安裝包:
client, common, libs, server

http://mirrors.sohu.com/mysql/MySQL-5.7/my...
http://mirrors.sohu.com/mysql/MySQL-5.7/my...
http://mirrors.sohu.com/mysql/MySQL-5.7/my...
http://mirrors.sohu.com/mysql/MySQL-5.7/my...
要按以下順序安裝,否則出錯

wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-community-common-5.7.26-1.el7.x86_64.rpm
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-community-libs-5.7.26-1.el7.x86_64.rpm
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-community-client-5.7.26-1.el7.x86_64.rpm
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-community-server-5.7.26-1.el7.x86_64.rpm

rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm  mysql-community-common-5.7.26-1.el7.x86_64.rpm mysql-community-common-5.7.26-1.el7.x86_64.rpm mysql-community-common-5.7.26-1.el7.x86_64.rpm

ROM Bundle相關包解釋

mysql-community-libs-compat-8.0.15-1.el6.x86_64.rpm MySQL之前版本的共享相容庫
mysql-community-test-8.0.15-1.el6.x86_64.rpm MySQL服務端的測試元件
mysql-community-devel-8.0.15-1.el6.x86_64.rpm MySQL資料庫客戶端應用程式的開發標頭檔案和庫
mysql-community-common-8.0.15-1.el6.x86_64.rpm 服務端和客戶端的公共檔案
mysql-community-libs-8.0.15-1.el6.x86_64.rpm 客戶端共享庫
mysql-community-client-8.0.15-1.el6.x86_64.rpm 客戶端及相關工具
mysql-community-server-8.0.15-1.el6.x86_64.rpm 服務端及相關工具

安裝 mysql-community-common-5.7.26-1.el7.x86_64.rpm 過程中報錯:

缺少 libaio.so.1:

rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
        libaio.so.1()(64bit) is needed by mysql-community-server-5.7.26-1.el7.x86_64
        libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-5.7.26-1.el7.x86_64
        libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-5.7.26-1.el7.x86_64
        mysql-community-client(x86-64) >= 5.7.9 is needed by mysql-community-server-5.7.26-1.el7.x86_64

解決:
去centos庫裡邊下載缺少的依賴
http://mirror.centos.org/centos/7/os/x86_6...

 wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libaio-0.3.109-13.el7.x86_64.rpm
 rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm
 rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm

原因:
32位系統現在很少了,yum似乎預設安裝32位的

啟動mysql

systemctl start mysqld.service

檢視mysql 狀態

[root@iZbp12ft4a1zgzzaoq9xgrZ ~]#  service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-08-02 10:48:20 CST; 3min 19s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 30783 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 30709 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 30787 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─30787 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Aug 02 10:48:16 iZbp12ft4a1zgzzaoq9xgrZ systemd[1]: Starting MySQL Server...
Aug 02 10:48:20 iZbp12ft4a1zgzzaoq9xgrZ systemd[1]: Started MySQL Server.
[root@iZbp12ft4a1zgzzaoq9xgrZ ~]#

Aug 02 10:48:20 iZbp12ft4a1zgzzaoq9xgrZ systemd[1]: Started MySQL Server.
[root@iZbp12ft4a1zgzzaoq9xgrZ ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-08-02 10:48:20 CST; 6min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 30783 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 30709 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 30787 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─30787 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Aug 02 10:48:16 iZbp12ft4a1zgzzaoq9xgrZ systemd[1]: Starting MySQL Server...
Aug 02 10:48:20 iZbp12ft4a1zgzzaoq9xgrZ systemd[1]: Started MySQL Server.
[root@iZbp12ft4a1zgzzaoq9xgrZ ~]#

檢視臨時密碼

[root@iZbp12ft4a1zgzzaoq9xgrZ ~]# grep 'temporary password' /var/log/mysqld.log
2019-08-02T02:48:18.053602Z 1 [Note] A temporary password is generated for root@localhost: DaelA_:Y#6&x
[root@iZbp12ft4a1zgzzaoq9xgrZ ~]#

密碼必須包含 符號 + 大小寫 + 數字

mysql  -uroot  -p

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!'

檢視當前使用者

SELECT CURRENT_USER();

授權

授權所有所有資料庫以及所有訪問許可權

GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '密碼'';

無法插入中文 報錯:Incorrect string value: '\xE7\x8F' for column 'name' at row 1

分析

show variables like 'char%';

發現 character_set_server 的編碼不是utf8mb4

【MySQL】安裝

show create table user;

CREATE TABLE `user` (
  `id` varchar(64) NOT NULL COMMENT '主鍵id',
  `username` varchar(32) DEFAULT NULL COMMENT '姓名',
  `role_no` int(11) DEFAULT NULL COMMENT '角色編號',
  `role_name` varchar(32) DEFAULT NULL COMMENT '角色名稱',
  `mobile` bigint(20) DEFAULT NULL COMMENT '手機號碼',
  `remark` varchar(200) DEFAULT NULL COMMENT '備註',
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id_uindex` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

發現 預設的建表用的是latin1

解決1:針對該表

ALTER TABLE 表名 DEFAULT CHARSET utf8 collate utf8_general_ci ; 

解決2:針對該資料庫

建立資料庫並指定編碼和校驗集

ALTER DATABASE 資料庫名 DEFAULT CHARACTER SET utf8 collate utf8_general_ci ; 

解決3:統一修改預設配置 一勞永逸

[root@iZbp12ft4a1zgzzaoq9xgrZ ~]# ps axu | grep mysql
root     15144  0.0  0.0 112708   980 pts/0    S+   11:31   0:00 grep --color=auto mysql
mysql    30787  0.0  5.6 1603084 220688 ?      Sl   Aug02   1:58 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

[root@iZbp12ft4a1zgzzaoq9xgrZ ~]# vim /etc/my.cnf

character-set-server=utf8mb4
character-set-database=utf8mb4
character-set-system=uft8mb4

systemctl restart mysqld

然而,發現只要修改character-set-database=utf8mb4就出現問題無法啟動
網上說需要升級成8.0....
參考:
https://www.jianshu.com/p/65c526cd7572

【MySQL】安裝

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章