mysql安装及简单使用
本文简要记录一下Mysql的安装及简单使用。具体的安装环境如下:
# cat /etc/centos-release CentOS Linux release 7.3.1611 (Core) # uname -a Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
当前我们使用较多的版本是: mysql5.6、mysql5.7
1. Mysql在线安装
CentOS7默认数据库是mariadb,其是mysql的一个分支。这里首先检查当前操作系统是否有mariadb,如有的话需要进行卸载:
# rpm -qa | grep mariadb mariadb-libs-5.5.52-1.el7.x86_64 # yum remove mariadb-libs-5.5.52-1.el7.x86_64 //(如果卸载不掉,请用如下方式卸载) # rpm -ev mariadb-libs-5.5.52-1.el7.x86_64
1.1 安装mysql官方yum源
在官网https://dev.mysql.com/downloads/repo/yum/
可以找到最新的mysql yum源,最新的yum源一般会包括当前最新版的mysql,也会包含一些相对旧的常用的mysql。这里我们下载的mysql80-community-release-el7-1.noarch.rpm中包含所需要的mysql5.6与mysql5.7版本:
# wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm # yum localinstall ./mysql80-community-release-el7-1.noarch.rpm
安装完成后我们可以在如下目录看到:
# ls -al /etc/yum.repos.d/mysql-community mysql-community.repo mysql-community-source.repo
也可以通过如下命令查看mysql yum源是否安装成功:
# yum repolist enabled | grep "mysql.*-community.*" mysql-connectors-community/x86_64 MySQL Connectors Community 51 mysql-tools-community/x86_64 MySQL Tools Community 63 mysql80-community/x86_64 MySQL 8.0 Community Server 17
接下来我们修改mysql-community.repo
源,改变默认安装的mysql版本。比如我们需要默认安装mysql5.7, 则把5.7版本对应的enable
`字段改为1,其他版本的改为0:
这里我们将mysql80-community
段的enable字段改为0; mysql57-community
段的enable字段改为1。
1.2 安装mysql5.7
上面我们改完mysql-community.repo
后就可以直接执行如下的命令安装mysql5.7
了:
# yum install mysql-community-server Dependencies Resolved =========================================================================================================================================================================== Package Arch Version Repository Size =========================================================================================================================================================================== Installing: mysql-community-server x86_64 5.7.22-1.el7 mysql57-community 165 M Installing for dependencies: mysql-community-client x86_64 5.7.22-1.el7 mysql57-community 24 M mysql-community-common x86_64 5.7.22-1.el7 mysql57-community 274 k mysql-community-libs x86_64 5.7.22-1.el7 mysql57-community 2.1 M Transaction Summary =========================================================================================================================================================================== Install 1 Package (+3 Dependent packages) # rpm -qpl ./mysql-community-client-5.7.27-1.el7.x86_64.rpm /usr/bin/mysql /usr/bin/mysql_config_editor /usr/bin/mysqladmin /usr/bin/mysqlbinlog /usr/bin/mysqlcheck /usr/bin/mysqldump /usr/bin/mysqlimport /usr/bin/mysqlpump /usr/bin/mysqlshow /usr/bin/mysqlslap /usr/share/doc/mysql-community-client-5.7.27 /usr/share/doc/mysql-community-client-5.7.27/COPYING /usr/share/doc/mysql-community-client-5.7.27/README /usr/share/man/man1/mysql.1.gz /usr/share/man/man1/mysql_config_editor.1.gz /usr/share/man/man1/mysqladmin.1.gz /usr/share/man/man1/mysqlbinlog.1.gz /usr/share/man/man1/mysqlcheck.1.gz /usr/share/man/man1/mysqldump.1.gz /usr/share/man/man1/mysqlimport.1.gz /usr/share/man/man1/mysqlpump.1.gz /usr/share/man/man1/mysqlshow.1.gz /usr/share/man/man1/mysqlslap.1.gz
接着我们再安装一个mysql客户端开发所用到的lib库:
# yum install mysql-devel mysql-community-devel-5.7.22-1.el7.x86_64.rpm | 3.6 MB 00:00:11 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql-community-devel-5.7.22-1.el7.x86_64 1/1 Verifying : mysql-community-devel-5.7.22-1.el7.x86_64 1/1 Installed: mysql-community-devel.x86_64 0:5.7.22-1.el7
1.3 配置mysql
安装完mysql后有如下默认配置文件:
-
配置文件
: /etc/my.cnf -
日志文件
: /var/log/var/log/mysqld.log -
服务启动脚本
: /usr/lib/systemd/system/mysqld.service -
socket文件
: /var/run/mysqld/mysqld.pid
/etc/my.cnf配置文件:
1.4 启动mysql
1) 执行如下的命令设置mysql开机启动
# systemctl enable mysqld # systemctl daemon-reload
2) 执行如下命令启动mysql服务
3) 找出mysql初始启动密码
不过采用上面方式安装的mysql,第一次启动使用的是一个随机的密码,我们通过如下方法找出:
# grep "password" /var/log/mysqld.log 2018-05-15T10:03:43.142527Z 1 [Note] A temporary password is generated for root@localhost: E?*RxdrDq6Ta
4) 登录重置密码
我们使用上述密码登录mysql,然后对密码进行重置:
我们登录之后,暂时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:
//下面我们将密码重置为testAa@123 (注: 当前mysql对密码强度有要求,必须为大小写字母+数组) # ALTER USER 'root'@'localhost' IDENTIFIED BY 'testAa@123'; //或通过如下命令修改用户密码 # set password for 'root'@'localhost'=password('testAa@123'); //执行如下刷新权限 # flush privileges;
说明:我们也可以在/etc/mysql.cnf如下行下加入skip-grant-tables
来免密码登录
# Disabling symbolic-links is recommended to prevent assorted security risks skip-grant-tables
成功登录之后采用上面的方式来修改密码,然后再将上述语句去掉。
5) 为root账户授予远程访问的权限
# GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'testAa@123' WITH GRANT OPTION; # flush privileges;
也可以专门新建一个账户用于远程访问:
# create user 'test_user'@'%' identified by 'testAa@123'; # GRANT ALL PRIVILEGES ON *.* TO 'test_user'@'%' IDENTIFIED BY 'testAa@123' WITH GRANT OPTION; # flush privileges; // 也可以通过如下指定要授予的权限 # grant select, insert, update, delete on *.* to 'test_user'@'localhost' identified by 'testAa@123'; # flush privileges;
6) 修改mysql默认字符编码
首先采用下面的命令查看当前默认字符编码:
关于mysql字符编码的设置,我们这里暂时不详细介绍。这里暂时可以不用修改,但是下面我们给出修改相应字符的方法:
# SET character_set_client = utf8; # SET character_set_results = utf8; # SET character_set_connection = utf8;
1.4 开放3306端口
这里我们可以直接通过如下方法关闭防火墙与SeLinux:
systemctl stop firewalld.service systemctl disable firewalld.service setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
也可以通过如下方法单独开放3306端口:
//centos查询端口是不是开放的 # firewall-cmd --permanent --query-port=3306/tcp //添加对外开放端口 # firewall-cmd --permanent --add-port=3306/tcp //重启防火墙 # firewall-cmd –reload
1.5 测试远程连接mysql
1) telnet 3306端口
首先telnet该端口:
2) 通过Navicat for MySQL进行测试连接
1.6 程序测试
上面我们安装了mysql-devel
,我们这里用其来对刚搭建的mysql进行测试。 编写test_mysql.c
文件如下:
编译运行:
# whereis mysql mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz # gcc -o test_mysql test_mysql.c -L/usr/lib64/mysql -lmysqlclient # ./test_mysql MYSQL tables in mysql database: columns_priv db engine_cost event func general_log gtid_executed help_category help_keyword help_relation help_topic innodb_index_stats innodb_table_stats ndb_binlog_index plugin proc procs_priv proxies_priv server_cost servers slave_master_info slave_relay_log_info slave_worker_info slow_log tables_priv time_zone time_zone_leap_second time_zone_name time_zone_transition time_zone_transition_type user finish
2. MySQL离线安装
首先查看是否安装MariaDB并卸载,请参看上述方法。
2.1 下载离线安装包
在https://dev.mysql.com/downloads/mysql/
页面点击Looking for previous GA versions?
链接,然后针对我们当前的Centos7操作系统选择:
-
version: 5.7.22
-
os: Red Hat Enterprise Linux 7 / Oracle Linux 7
-
os version: all
然后下载64bit版本的mysql5.7安装包mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar。
2.2 安装mysql
将上述安装包解压:
# tar -xvf ./mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar mysql-community-libs-5.7.22-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.22-1.el7.x86_64.rpm mysql-community-embedded-5.7.22-1.el7.x86_64.rpm mysql-community-test-5.7.22-1.el7.x86_64.rpm mysql-community-server-5.7.22-1.el7.x86_64.rpm mysql-community-client-5.7.22-1.el7.x86_64.rpm mysql-community-server-minimal-5.7.22-1.el7.x86_64.rpm mysql-community-devel-5.7.22-1.el7.x86_64.rpm mysql-community-common-5.7.22-1.el7.x86_64.rpm mysql-community-minimal-debuginfo-5.7.22-1.el7.x86_64.rpm mysql-community-embedded-devel-5.7.22-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.22-1.el7.x86_64.rpm
这里我们安装如下几个包:
# ls -al total 201696 drwxr-xr-x 2 root root 4096 May 16 09:37 . drwxr-xr-x 3 root root 4096 May 16 09:34 .. -rw-r--r-- 1 root root 25106088 May 16 09:35 mysql-community-client-5.7.22-1.el7.x86_64.rpm -rw-r--r-- 1 root root 280800 May 16 09:35 mysql-community-common-5.7.22-1.el7.x86_64.rpm -rw-r--r-- 1 root root 3781636 May 16 09:36 mysql-community-devel-5.7.22-1.el7.x86_64.rpm -rw-r--r-- 1 root root 2239868 May 16 09:36 mysql-community-libs-5.7.22-1.el7.x86_64.rpm -rw-r--r-- 1 root root 2116356 May 16 09:36 mysql-community-libs-compat-5.7.22-1.el7.x86_64.rpm -rw-r--r-- 1 root root 172992596 May 16 09:37 mysql-community-server-5.7.22-1.el7.x86_64.rpm
再执行如下命令安装:
# yum localinstall *.rpm
2.3 配置mysql
请参看上面在线安装相关章节。
2.4 测试
请参看上面在线安装相关章节。
3. Ubuntu环境下安装MySQL(附录)
执行如下命令安装:
# sudo apt-get install mysql-server # sudo apt-get install mysql-client # sudo apt-get install libmysqlclient-dev
通过上面安装后,查看是否安装成功:
# netstat -nlp | grep mysql tcp6 0 0 :::3306 :::* LISTEN 76868/mysqld unix 2 [ ACC ] STREAM LISTENING 345340 76868/mysqld /var/lib/mysql/mysql.sock
然后修改配置文件,使MySQL支持远程登录(修改/etc/mysql/my.cnf文件),注释掉如下语句:
# bind-address = 127.0.0.1
以安装时默认的root用户登录mysql:
# mysql -h 127.0.0.1 -uroot -p
登录后创建一个测试用户(用户名: test_user, 密码:testAa@123)
# create user 'test_user'@'%' identified by 'testAa@123';
为创建的用户授权(目前授予最大权限,方便操作):
# grant all privileges *.* to 'test_user'@'%' identified by 'testAa@123';
创建一个测试数据库:
# create database testdb;
4. Mysql下执行sql脚本
下面介绍两种方法:
1) 在命令行下(未连接数据库),输入:
# mysql -uroot -ptestAa@123 < /root/CreateDB_app.sql
2) 在命令行下(已连接数据库,此时的提示符为mysql),输入:
[参看】