本文集记录日常Mysql使用的一些技能点,积累作为一个未来工作的参照 。
1. CentOS 7 安装Mysql :
1.1 安装文件下载:
安装Linux版本的Mysql,首先下载安装包:https://dev.mysql.com/downloads/mysql/
根据版本选择 “Red Hat Enterprise Linux” 版本选择7了。下载 “RPM Bundle” :
1.2 检查CentOS 7是否符合了Mysql依赖
Mysql安装过程中,会通过mysql用户在 /tmp下创建tmp_db文件,所以需要给/tmp较大权限。
chmod -R 777 /tmp
检查两个依赖包是否都安装了 :
rpm -qa | grep libaio
rpm -qa | grep net-tools
我的系统检查,没有安装 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 数据库Server初始化和配置
为了保证数据库目录与文件的所有者为mysql登陆用户,如果是以root身份运行mysql服务,需要执行以下命令进行初始化 :
mysqld --initialize --user=mysql
这时候,我们就想登陆数据库了,那么需要密码,下边命令查看密码,密码保存在文件中:
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 查看数据库是否处于“只读状态” :
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 关闭“只读状态” :
set global read_only=0;
set global super_read_only=0;
下面就可以开始一顿操作了 。
3.3 恢复“只读状态” :
一顿操作后,别忘了恢复只读状态 :
set global read_only=1;
set global super_read_only=1;
4. Mysql主从同步问题:
首先需要检查,当前主从同步是否一致, 登陆从库,执行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