配置YUM源
在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/
# 下载mysql源安装包
shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
# 安装mysql源
shell> yum localinstall mysql57-community-release-el7-8.noarch.rpm
检查mysql源是否安装成功
shell> yum repolist enabled | grep "mysql.*-community.*"
输出以下内容表示ok
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
!mysql-connectors-community/x86_64 MySQL Connectors Community 74
!mysql-tools-community/x86_64 MySQL Tools Community 74
!mysql57-community/x86_64 MySQL 5.7 Community Server 307
可以修改vim /etc/yum.repos.d/mysql-community.repo源,改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。
安装MySQL
先安装相关依赖:
yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* make cmake bison git openssl openssl-devel
开始安装:
shell> yum install mysql-community-server
启动mysql
systemctl start mysqld
开机启动
systemctl enable mysqld.service
停止服务
systemctl enable mysqld.service
重启服务
systemctl restart mysqld.service
查看状态
systemctl status mysqld.service
mysql查找报错信息&配置信息
查找mysql路径:
which mysqld
输出:/usr/sbin/mysqld
查找my.cnf的路径同时如果有报错信息也会输出
/usr/sbin/mysqld --verbose --help |grep -A 1 'Default options'
my.cnf基本配置(/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 lower_case_table_names=1 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' #validate_password = off [mysqld] #character_set_server=utf8 #init_connect='SET NAMES utf8' #skip-grant-tables wait_timeout=86400 character-set-server=utf8mb4 [mysql] default-character-set=utf8mb4
修改默认密码
mysql5.7 会生成默认密码在:/var/log/mysqld.log [Note] A temporary password is generated for root@localhost: lQidlh;BX4*x
通过命令查看默认密码
grep 'temporary password' /var/log/mysqld.log
密码不对 or 找不到密码怎嘛办..?
- 修改配置文件
vim /etc/my.cnf
在[mysqld]节点添加
skip-grant-tables
- 重启mysql
- 用空密码进入
mysql -uroot
update mysql.user set authentication_string=password('123456') where user='root' and Host = 'localhost';
flush privileges;
exit;
- 还原my.cnf
授权远程连接权限
mysql -u root -p
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;#重载授权表: exit;