本文集記錄日常Mysql使用的一些技能點跳座,積累作為一個未來工作的參照 。
1. CentOS 7 安裝Mysql :
1.1 安裝文件下載:
安裝Linux版本的Mysql丽涩,首先下載安裝包:https://dev.mysql.com/downloads/mysql/
根據(jù)版本選擇 “Red Hat Enterprise Linux” 版本選擇7了。下載 “RPM Bundle” :
1.2 檢查CentOS 7是否符合了Mysql依賴
Mysql安裝過程中奉瘤,會通過mysql用戶在 /tmp下創(chuàng)建tmp_db文件葬凳,所以需要給/tmp較大權限。
chmod -R 777 /tmp
檢查兩個依賴包是否都安裝了 :
rpm -qa | grep libaio
rpm -qa | grep net-tools
我的系統(tǒng)檢查,沒有安裝 net-tools充坑,這里需要做一下安裝:
yum install net-tools
至此,依賴包也滿足了染突,下面開始安裝Mysql 捻爷。
1.3 開始安裝Mysql
下載的bundle中有很多rpm文件,只需要安裝部分即可份企,同時需要按照下表列舉的順序安裝(相互間有依賴關系):
rpm -ivh mysql-community-common-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.31-1.el7.x86_64.rpm
在安裝到libs和mysql-server時候報了如下錯誤 :
[root@localhost rpm-mysql]# rpm -ivh mysql-community-libs-8.0.31-1.el7.x86_64.rpm
warning: mysql-community-libs-8.0.31-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
error: Failed dependencies:
mariadb-libs is obsoleted by mysql-community-libs-8.0.31-1.el7.x86_64
因centos7默認安裝了mariadb, 會造成依賴沖突也榄。把機器上的libs先remove掉。
yum remove mysql-libs
至此薪棒,完成了所有安裝包的安裝工作手蝎。 Well Done榕莺!
1.4 數(shù)據(jù)庫Server初始化和配置
為了保證數(shù)據(jù)庫目錄與文件的所有者為mysql登陸用戶俐芯,如果是以root身份運行mysql服務棵介,需要執(zhí)行以下命令進行初始化 :
mysqld --initialize --user=mysql
這時候,我們就想登陸數(shù)據(jù)庫了吧史,那么需要密碼邮辽,下邊命令查看密碼,密碼保存在文件中:
cat /var/log/mysqld.log
登陸前贸营,再查看下mysql服務是否開了,并且設置為開機自啟動 :
systemctl status mysqld
systemctl enable mysqld.service
下面吨述,我們就要登陸mysql了,用之前查看的密碼登陸root用戶钞脂,這時候登陸后需要修改密碼揣云。
mysql> alter user 'root'@'localhost' identified by 'redhat' ;
2. mysql用戶、權限管理:
2.1 binlog是否開啟:
show variables like 'log_%'
如果log_bin顯示為ON冰啃,則代表已開啟邓夕。
2.2 查看用戶,并查看用戶權限:
查看庫里的所有用戶 :
mysql> select user,host from mysql.user ;
查看當前datasync被授予了哪些權限:
mysql> SHOW GRANTS FOR 'datasync'@'%';
+------------------------------------------------------------------------------+
| Grants for datasync@% |
+------------------------------------------------------------------------------+
| GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON . TO 'datasync'@'%' |
+------------------------------------------------------------------------------+
1 row in set (0.00 sec)
2.3 新建用戶datasync阎毅,并授予讀取binlog權限:
CREATE USER 'datasync'@'%' IDENTIFIED BY 'qUH%*69mP&%s' ;
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON . TO 'datasync'@'%' ;
3. 只讀備庫管理員修改配置:
3.1 查看數(shù)據(jù)庫是否處于“只讀狀態(tài)” :
MySQL [jylhlog]> show global variables like "%read_only%";
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| super_read_only | ON |
| transaction_read_only | OFF |
| tx_read_only | OFF |
+-----------------------+-------+
5 rows in set (0.00 sec)
3.2 關閉“只讀狀態(tài)” :
set global read_only=0;
set global super_read_only=0;
下面就可以開始一頓操作了 焚刚。
3.3 恢復“只讀狀態(tài)” :
一頓操作后,別忘了恢復只讀狀態(tài) :
set global read_only=1;
set global super_read_only=1;
4. Mysql主從同步問題:
首先需要檢查扇调,當前主從同步是否一致矿咕, 登陸從庫,執(zhí)行SQL :
show slave status\G
IO_Running的error如下 :
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires. Replicate the missing transactions from elsewhere, or provision a new slave from backup. Consider increasing the master's binary log expiration period. The GTID sets and the missing purged transactions are too long to print in this message. For more information, please see the master's error log or the manual for GTID_SUBTRACT.'
這里狼钮,先找到Mysql的配置文件 碳柱, my.cnf ,那么問題來了熬芜,它在哪兒呢 士聪?
MySQL [(none)]> show variables like "%char%";
+--------------------------+------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /newhome/env/mysql/share/charsets/ |
+--------------------------+------------------------------------+
8 rows in set (0.00 sec)
Mysql的配置位置 : /newhome/env/mysql