Linux下的mysql初次安装使用
安装
mysql的安装我使用了最简单的apt-get方法安装,我使用的阿里云的ESC云服务器,第一次安装,在此做个记录
命令:
1.更新软件包
root@47:~# sudo apt-get update
2.安装mysql-server
默认安装最新版本,如需安装指定版本,如5.6,请使用命令sudo apt-get install mysql-server-5.6
root@47:~# sudo apt-get install mysql-server
//默认最新版本
3.设置密码
安装过程中会提示设置密码,确认密码,然后安装成功
4.连接mysql
root@47:~# mysql -uroot -p
Enter password:
//输入密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
//连接成功
配置
1.修改bind-address
安装成功之后,本地可以连接数据库,但是其他的电脑不能连,需要做一些设置
root@47:~# sudo vim /etc/mysql/my.cnf
打开后发现以下内容
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
``
#
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
可以看到最后两行内容,说明配置内容在下面两个文件夹,最终找到配置在 /etc/mysql/mysql.conf.d/mysqld.cnf文件里。
vim打开编辑
root@47:~# sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
找到以下这行代码注释掉
bind-address = 127.0.0.1
改为#bind-address = 127.0.0.1
2.用户授权
本地连接mysql,
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
其中myuser 是你的mysql用户
其中mypassword 是你的mysql密码
3.设置阿里云的安全组
我用的阿里云服务器,默认是任何端口都不能访问的。数据库设置好以后要开放3306端口(myslq的默认端口,如果你修改过,这个地方就不是3306了,是你修改后端口)的访问权限
4.重启服务器,完成设置
尝试连接,
带ip的连接方式,
shj@shj-PC:~$ mysql -h ip -uroot -ppwd
ip:你的远程ip地址
root :你的mysql用户名
pwd: 你的密码