CentOS 7 安装MySQL
1.安装YUM Repo
a、由于CentOS 的yum源中没有mysql,需要到mysql的官网下载yum repo配置文件。
下载命令:
[root@cnetos7 software]# wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
b、然后进行repo的安装:
[root@cnetos7 software]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm
warning: mysql57-community-release-el7-9.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql57-community-release-el7-9 ################################# [100%]
执行完成后会在/etc/yum.repos.d/目录下生成两个repo文件mysql-community.repo和mysql-community-source.repo
2.使用yum命令即可完成安装
a、安装命令:
[root@cnetos7 software]# yum -y install mysql-server
b、启动msyql:
[root@cnetos7 software]# systemctl start mysqld
[root@cnetos7 software]# 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 2021-01-22 16:20:01 CST; 1s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 8694 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 8584 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 8696 (mysqld)
CGroup: /system.slice/mysqld.service
└─8696 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
c、获取安装时的临时密码(在第一次登录时就是用这个密码):
[root@cnetos7 software]# grep 'temporary password' /var/log/mysqld.log
2021-01-22T08:19:41.297488Z 1 [Note] A temporary password is generated for root@localhost: =zQobeOA3s.y
You have mail in /var/spool/mail/root
d、倘若没有获取临时密码,则删除原来安装过的mysql残留的数据,再启动mysql
[root@cnetos7 ~]# rm -rf /var/lib/mysql
[root@cnetos7 ~]# systemctl start mysqld
3、登录MySQL
a、方式一(已验证):
[root@cnetos7 software]# mysql -u root -p
Enter password: 输入密码(刚刚获取的临时密码)
b、若登录不了,则进行以下配置,跳过登录验证
在 [mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程
[root@cnetos7 software]# vim /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/mysqld/mysqld.pid
skip-grant-tables
重启MySQL
4.登录成功后修改密码
[root@cnetos7 software]# mysql -uroot -p
Enter password:
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
5.开启远程控制
MySQL默认是没有开启远程控制的,必须添加远程访问的用户,即默认是只能自己访问,别的机器是访问不了的。
1、方式一:
1.1、连接服务器:
[root@cnetos7 software]# mysql -u root -p
1.2、看当前所有数据库:
mysql> show databases;
1.3、进入mysql数据库:
mysql> use mysql;
1.4、查看mysql数据库中所有的表:
mysql> show tables;
1.5、查看user表中的数据:
mysql> select Host, User,Password from user;
1.6、修改user表中的Host:
mysql> update user set Host='%' where User='root';
说明: % 代表任意的客户端,可替换成具体IP地址。
1.7、最后刷新一下:
mysql> flush privileges;
1.8、注意:一定要记得在写sql的时候要在语句完成后加上" ; "
2、方式二:
1、使用 grant 命令
grant all privileges on 数据库名.表名 to 创建的用户名(root)@"%" identified by “密码”;
2、格式说明:
数据库名.表名 如果写成*.*代表授权所有的数据库 flush privileges; #刷新刚才的内容
如:
CREATE USER canal IDENTIFIED BY 'username';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'username'@'192.168.0.103';
-- GRANT ALL PRIVILEGES ON *.* TO 'username'@'ip或者%' ;
FLUSH PRIVILEGES;
@ 后面是访问mysql的客户端IP地址(或是 主机名) % 代表任意的客户端,如果填写 localhost 为本地访问(那此用户就不能远程访问该mysql数据库了)
6.其他配置
1、设置安全选项:
mysql_secure_installation
2、关闭MySQL
[root@cnetos7 software]# systemctl stop mysqld
3、重启MySQL
[root@cnetos7 software]# systemctl restart mysqld
4、查看MySQL运行状态
[root@cnetos7 software]# systemctl status mysqld
5、设置开机启动
[root@cnetos7 software]# systemctl enable mysqld
6、关闭开机启动
[root@cnetos7 software]# systemctl disable mysqld
7、配置默认编码为utf8:
[root@cnetos7 software]# vim /etc/my.cnf
character_set_server=utf8
init_connect='SET NAMES utf8'
8、查看版本
mysql> select version();