关于Linux下服务器MySQL的安装和搭建
一、检测是否已经安装Mysql
检测
# yum list installed | grep mysql //检查安装 # yum -y remove mysql-libs.x86_64 //卸载 //若有多个依赖文件则依次卸载。当结果显示为 Complete!即卸载完毕。
依赖安装libaio
MySQL依赖libaio,所以先要安装libaio
# yum search libaio //检索相关信息 # yum install libaio //安装依赖包 // 成功安装,提示如下: 已加载插件: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.yun-idc.com * extras: mirrors.163.com * updates: mirrors.163.com 注 : 软件包 libaio-0.3.109-12.el7.x86_64 已安装并且是最新版本
添加mysql yum respository
# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm # yum localinstall mysql-community-release-el7-5.noarch.rpm // 验证是否添加成功 # yum repolist enabled | grep "mysql.*-community.*" //返回的内容如下: mysql-connectors-community/x86_64 MySQL Connectors Community 74 mysql-tools-community/x86_64 MySQL Tools Community 74 mysql56-community/x86_64 MySQL 5.6 Community Server 429
选择要启用的版本
# yum repolist all | grep mysql //查看mysql版本,执行 //返回数据如下 mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 74 mysql-connectors-community-source MySQL Connectors Community - S disabled mysql-tools-community/x86_64 MySQL Tools Community enabled: 74 mysql-tools-community-source MySQL Tools Community - Source disabled mysql55-community/x86_64 MySQL 5.5 Community Server disabled mysql55-community-source MySQL 5.5 Community Server - S disabled mysql56-community/x86_64 MySQL 5.6 Community Server enabled: 429 mysql56-community-source MySQL 5.6 Community Server - S disabled mysql57-community-dmr/x86_64 MySQL 5.7 Community Server Dev disabled mysql57-community-dmr-source MySQL 5.7 Community Server Dev disabled //可以通过类似下面的语句来启动某些版本 # yum-config-manager --disable mysql56-community yum-config-manager --enable mysql57-community-dmr //或者通过修改 /etc/yum.repos.d/mysql-community.repo 文件 #vi /etc/yum.repos.d/mysql-community.repo //内容如下 # Enable to use MySQL 5.6 [mysql56-community] name=MySQL 5.6 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql //其中 enabled=0 是指禁用,enabled=1 指启用。 //执行,查看当前的启动的 MySQL 版本 # yum repolist enabled | grep mysql
注 : 任何时候,只能启用一个版本。
二、通过Yum 安装Mysql
# yum install mysql-community-server //中途遇到提示,按照提示安装 //执行 # rpm -qi mysql-community-server.x86_64 0:5.6.24-3.el7 # whereis mysql //可以看到mysql的安装目录是/usr/bin
三、防火墙启动、关闭命令
# systemctl start mysqld //开启 MySQL Server # systemctl status mysqld //查看 MySQL Server 状态 # systemctl stop mysqld //关闭 MySQL Server
四、检测是否安装成功
[root@root home]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.42 MySQL Community Server (GPL) 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> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
有数据返回这说明安装成功
五、远程连接mysql,开启防火墙端口
//打开 iptables 的配置文件: # vim /etc/sysconfig/iptables //如果该 iptables 配置文件 不存在,先执行 # yum install iptables-services //安装。 //iptables文件中添加以下内容 firewall-cmd --permanent --zone=public --add-port=3306/tcp firewall-cmd --permanent --zone=public --add-port=3306/udp //执行 # firewall-cmd --reload //重启防火墙,使最新的防火墙设置规则生效。 //mysql安全设置,服务器启动后,可以执行 # mysql_secure_installation;
六、mysql的设置
1.修改密码
可以直接登录
mysql>set password for 'root'@'localhost'=password('newpasswd');
无法直接登录
//获取MySQL的临时密码, 只有启动过一次mysql才可以查看临时密码 # grep 'temporary password' /var/log/mysqld.log //登陆并修改密码 # mysql -uroot -p //修改密码 # ALTER USER 'root'@'localhost' IDENTIFIED BY 'root'; //密码设置太简单出现以下的提示 ERROR 1819 (HY000): Your password … //首先,修改validate_password_policy参数的值 mysql> set global validate_password_policy=0; //再修改密码的长度 mysql> set global validate_password_policy=0; //再次执行修改密码就可以了 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root'; //授权其他机器登陆 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; mysql> FLUSH PRIVILEGES;
2.设置mysql UTF-8编码
# vim /etc/my.cnf //修改内容如下 [mysqld] character_set_server = utf8 [mysql] default-character-set = utf8 //查看当前 MySQL 字符集进入mysql #mysql> SHOW VARIABLES LIKE 'character%'; #mysql> SHOW VARIABLES LIKE 'character%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
3.设置开机启动Mysql
# systemctl is-enabled mysqld.service;echo $? //如果是 enabled 则说明是开机自动,如果不是,执行 # chkconfig --levels 235 mysqld on
4.更改数据库存放目录
//home 目录下建立 data 目录 # mkdir /home/data //把 MySQL 服务进程停掉 # systemctl stop mysqld //或者 # mysqladmin -u root -p shutdown //移动数据到数据存放目录 // /var/lib/mysql 整个目录移到 /home/data,执行 # mv /var/lib/mysql /home/data #vim /etc/my.cnf //修改 /etc/my.cnf 文件 //内容如下 [mysqld] datadir=/home/data/mysql socket=/home/data/mysql/mysql.sock]
[mysql] socket=/home/data/mysql/mysql.sock # chown -R mysql:mysql /home/data/mysql //修改权限 //重启后,如果不能启动 MySQL 服务,执行 # vim /etc/sysconfig/selinux 调整SELINUX=permissive 保存设置,执行 reboot 重启生效
5.账号设置
//创建一个普通用户 user ,密码是 user mysql> CREATE USER 'user'@'%' IDENTIFIED BY 'user'; //给这个用户授予 SELECT,INSERT,UPDATE,DELETE 的远程访问的权限,这个账号一般用于提供给实施的系统访问 mysql> GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO 'user'@'%'; //创建一个管理员用户 admin 账号 ,密码是 some_pass mysql> CREATE USER 'admin'@'%' IDENTIFIED BY 'admin'; //给这个用户授予所有的远程访问的权限。这个用户主要用于管理整个数据库、备份、还原等操作。 mysql> GRANT ALL ON *.* TO 'admin'@'%'; //使授权立刻生效 mysql> flush privileges;
七、关于用本地的sql连接工具连接服务器的Mysql
1.关于Host 'xxx' is not allowed to connect to this MySQL server.
1.登录到mysql: mysql -uroot -ppwd 2.查看user表: mysql> use mysql Database changed mysql> select host,user,password from user; +------+------+-------------------------------------------+ | host | user | password | +------+------+-------------------------------------------+ | localhost | root | *826960FA9CC8A87953B3156951F3634A80BF9853 | +------+------+-------------------------------------------+ 1 row in set (0.00 sec) 表中host、user字段标识了可以访问数据库的主机和用户。例如上面的数据就表示只能本地主机通过root用户访问。原来如此,难怪远程连接死活连不上
为了让数据库支持远程主机访问,有两种方法可以开启远程访问功能。
第一种(改表法):
修改host字段的值,将localhost修改成需要远程连接数据库的ip地址。或者直接修改成%。修改成%表示,所有主机都可以通过root用户访问数据库。为了方便,我直接修改成%。命令:mysql> update user set host = '%' where user = 'root'; 再次查看user表 +------+------+-------------------------------------------+ | host | user | password | +------+------+-------------------------------------------+ | % | root | *826960FA9CC8A87953B3156951F3634A80BF9853 | +------+------+-------------------------------------------+ 1 row in set (0.00 sec)
第二种(授权法):
例如,你想root使用mypassword从任何主机连接到mysql服务器的话。 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; 如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器,并使用mypassword作为密码 GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
输入命令mysql> FLUSH PRIVILEGES; 回车使刚才的修改生效,再次远程连接数据库成功。bingo.
别忘记最后的FLUSH PRIVILEGES; 刷新先前的修改。
2.Mysql for Linux 报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
一般这个错误是由密码错误引起,解决的办法自然就是重置密码。
1.重置密码的第一步就是跳过MySQL的密码认证过程,方法如下: #vim /etc/my.cnf(注:windows下修改的是my.ini)
在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程,如下图所示:
保存文档并退出:
#:wq
2.接下来我们需要重启MySQL
3.重启之后输入#mysql即可进入mysql。
4.接下来就是用sql来修改root的密码 mysql> use mysql; mysql> update user set password=password("你的新密码") where user="root"; mysql> flush privileges; mysql> quit 到这里root账户就已经重置成新的密码了。
5.编辑my.cnf,去掉刚才添加的内容,然后重启MySQL。大功告成!
注 :很多刚接触的朋友可能比较迷惑的是在自己的平台上找不到my.cnf或者my.ini文件,如果你是Linux,使用如下方式可以搜索到:
至于windows平台,去安装目录下找一下my.ini吧。