linux 安装mysql及常用配置 授权 改密 开放端口
ubuntu 18.04
1.安装
apt-get install -y mysql-server
使用上述命令即可完成mysql的安装,有些教程建议install mysql-client,实践表明,上述命令同时完成client的安装。
安装以后,mysql是在后台运行的,可以通过service命令查看mysql的运行情况:
service mysql status
或者
systemctl status mysql
2.修改root的默认密码
mysql的配置文件放在/etc/mysql
目录下,具体为:
# 查看文件目录
root@ubuntu:/etc/mysql# ls
conf.d debian.cnf debian-start my.cnf my.cnf.fallback mysql.cnf mysql.conf.d
# 查看debian.cnf,借此登录mysql
root@ubuntu:/etc/mysql# cat debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = V5kqkke6kuSbaKcj
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = V5kqkke6kuSbaKcj
socket = /var/run/mysqld/mysqld.sock
# 根据cnf文件信息登录mysql
mysql -u debian-sys-maint -p
# 输入刚刚的password
# 进入mysql client,变更root密码
show databases;
use mysql;
update user set authentication_string=PASSWORD("root") where user='root'; # root用户密码也是root
update user set plugin="mysql_native_password";
flush privileges;
exit;
3.设置远程访问 & 开放3306端口
1,修改表,登录mysql数据库,切换到mysql数据库,使用sql语句查看"select host,user from user ;"
mysql -u root -pvmwaremysql>use mysql;
mysql>update user set host = '%' where user ='root';
mysql>select host, user from user;
mysql>flush privileges;
开放3306端口
1.检查端口
netstat -tnlp | grep 3306
2.修改配置
cd /etc/mysql/mysql.conf.d
vi mysqld.cnf
注释bind
# bind-address = 127.0.0.1
3.重启mysql
service mysql restart
4.确认修改成功
netstat -tnlp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 124676/mysqld