本文整理了下CentOS7下安装mysql的基本配置。
Docker安装
推荐使用Docker方式,非常方便,容易配置。
安装mysql
service docker start
docker pull mysql:5.7
docker images
运行mysql
docker run -itd --name idb -v /root/suninf/datadir:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -e TZ=Asia/Shanghai -d mysql:5.7
结束docker容器
docker ps
docker stop <containId>
docker rm -f <containId>
shell进入容器
docker exec -it idb bash
本机连接
mysql -h localhost -P 3306 --protocol=tcp -u root -p
CentOS下直接安装
安装
wget https://dev.mysql.com/get/Downloads/MySQL-4.7/mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar
tar -xvf mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar
rpm -ivh mysql-community-common-5.7.22-1.el7.x86_64.rpm \
mysql-community-libs-5.7.22-1.el7.x86_64.rpm \
mysql-community-client-5.7.22-1.el7.x86_64.rpm \
mysql-community-server-5.7.22-1.el7.x86_64.rpm \
mysql-community-devel-5.7.22-1.el7.x86_64.rpm
注意:
- 如果已经安装mariadb,需要删除
yum -y remove mariadb mariadb-libs mariadb-server
- 需要安装perl
yum -y install perl perl-JSON
使用mysql
启动与结束命令
service mysqld start
service mysqld stop
# 使用密码登录mysql
mysql -u root -p
找到并修改初始密码
参考:
- https://blog.csdn.net/lzwglory/article/details/54809916
- https://blog.csdn.net/chenyulancn/article/details/62439389
- http://sharong.iteye.com/blog/2262760
- 通过
cat /var/log/mysqld.log | grep password
找到临时密码 mysql -u root -p
输入临时密码,连接到mysql- 修改初始密码
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MysqlPasswordXXX');
注意:默认密码级别要求比较高,如果报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
,可以通过set global validate_password_policy=0;
来设置降低密码级别
如果找不到临时的初始密码,可以新建密码
参考:https://www.jianshu.com/p/707190d13e5c
- 修改
/etc/my.cnf
文件,在[mysqld]下添加skip-grant-tables
, 再重新启动mysql - 然后用空密码方式使用root用户登录MySQL:
mysql -u root
修改root用户的密码
mysql> update mysql.user set password=PASSWORD('new_password') where User='root'; mysql> flush privileges; mysql> quit
远程访问的权限
默认mysql不支持远程访问,需要设置放开权限:
grant all privileges on *.* to 'root'@'%' identified by '[password]' with grant option;
flush privileges;
然后就能远程访问了:mysql -h xx.xx.xx.xx -P 3306 -u root -p
参考:https://blog.csdn.net/u010758410/article/details/76381632
参考:
- https://itbilu.com/linux/docker/EyP7QP86M.html
- https://www.runoob.com/docker/docker-install-mysql.html
- https://www.runoob.com/docker/docker-command-manual.html
- https://blog.csdn.net/winy_lm/article/details/77980529
mysql常用操作
参考mysql教程: http://www.runoob.com/mysql/mysql-create-tables.html
show databases;
use db_name;
show tables;
exit