Linux下mysql的安装配置

  • 1.安装

方法一:通过yum安装

首先必须配置了yum源,然后直接执行

yum -y install mysql-server
如果提示Error Downloading Packages:
1:执行yum clean all 清除缓存目录下的软件包及旧的headers;
2:接着执行 yum list 重新列出所有已经安装和可以安装的软件包;
3:重新执行上述命令,发现yum编译成功;
注意:如果执行 yum list 报错,那就检查一下yum仓库是否有问题;并清除/etc/yum.repos.d/下多余.repo文件!
[root@localhost ~]# mount /dev/cdrom /mnt
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@localhost ~]# cd /mnt/Server
[root@localhost Server]# ls
a2ps-4.13b-57.2.el5.x86_64.rpm
acl-2.2.39-8.el5.x86_64.rpm
 
[root@localhost ~]# ls /etc/yum.repos.d/
rhel-debuginfo.repo
[root@localhost ~]# vi /etc/yum.repos.d/rhel-debuginfo.repo
[root@localhost ~]# 
[root@localhost Server]# cat /etc/yum.repos.d/rhel-debuginfo.repo
[rhel-debuginfo]
name=Red Hat Enterprise Linux $releasever - $basearch - Debug
baseurl=file:///mnt/Server
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
查看安装是否完成
[root@localhost ~]# yum list
如果成功后,再次执行mysql的yum 安装命令即可。
安装完毕后可以检查一下是否安装成功:
 

[root@localhost ~]# rpm -qa|grep mysql-server

mysql-server-5.0.77-4.el5_6.6

[root@localhost ~]# 

方法二:通过下载安装包rpm安装

1.下载mysql安装包

  mysql:http://dev.mysql.com/downloads/mysql/

 打开页面之后,在Select Platform:下选择linux Generic

2.下载完毕后解压到指定文件夹下

[root@localhost mysql]# ls
MySQL-5.6.25-1.linux_glibc2.5.x86_64.rpm-bundle.tar
MySQL-client-5.6.25-1.linux_glibc2.5.x86_64.rpm
MySQL-devel-5.6.25-1.linux_glibc2.5.x86_64.rpm
MySQL-embedded-5.6.25-1.linux_glibc2.5.x86_64.rpm
MySQL-server-5.6.25-1.linux_glibc2.5.x86_64.rpm
MySQL-shared-5.6.25-1.linux_glibc2.5.x86_64.rpm
MySQL-shared-compat-5.6.25-1.linux_glibc2.5.x86_64.rpm
MySQL-test-5.6.25-1.linux_glibc2.5.x86_64.rpm

3.检查安装

rpm -qa | grep mysql  或者 rpm -qa |grep MySQL

 --查看系统之前是否已安装MySQL,这里是区分大小写的

如果没有结果显示则表示没有安装可以继续下面的步骤,如果有显示需要先卸载已安装的

rpm -e --nodeps mysql-libs-5.1.47-4.el6.i686   

并要删除相关文件

rm -rf /var/lib/mysql*
rm -rf /usr/share/mysql*

4.安装mysql server

rpm -ivh MySQL-server-5.6.25-1.linux_glibc2.5.x86_64.rpm

5.安装mysql client

rpm -ivh  MySQL-client-5.6.25-1.linux_glibc2.5.x86_64.rpm

6.检测 MySQL 3306 端口是否打开。 netstat 看 MySQL 端口是否打开,如打开表示服务已经启动,安装成功。MySQL 默认的端口是3306。
[root@localhost mysql]# netstat -nat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 127.0.0.1:2208              0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:999                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:2207              0.0.0.0:*                   LISTEN      
tcp        0    132 172.17.8.85:22              172.17.10.213:49662         ESTABLISHED 
tcp        0      0 :::3306                     :::*                        LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::ffff:172.17.8.85:3306     ::ffff:172.17.10.213:53770  ESTABLISHED 
[root@localhost mysql]# 
如果没有3306端口,启动mysql服务: service mysql start  

7.修改mysql连接密码

在安装mysql server的过程中,它会随机生成一个默认的密码,位置在:/root/.mysql_secret ,打开这个文件之后就可以看到随机的密码了。

然后就可以修改密码了,先输入随机密码,再输入新密码并确认

[root@localhost mysql]# mysqladmin -u root -p password 
Enter password: 
New password: 
Confirm new password:

8.进入mysql,通过命令:mysql -u root -p,然后输入修改后的密码即可

[root@localhost mysql]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

9.远程连接

因为Linux系统是装在虚拟机上的,我用本机的navigate连接上面所装的mysql

一开始一直提示错误 --连接错误 Can't connect to MySQL server on XXXX(61)

1)进入mysql数据库:use mysql;
2)执行:update user set host = '%' where user = 'root';  有多条的话可以修改一条
3)有多条记录时,可能会报错:ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
4)flush privileges;
(提交修改,这个绝对不能少,要不然不会生效。切记,我之前就是忘记了,死活连接不上)
以上4步后去连接mysql还是提示以上错误,那么就是防火墙的原因了
5)修改文件 /etc/sysconfig/iptables, 添加 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
,且必须加在icmp-host-prohibited之前
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT

重启防火墙:

[root@localhost mysql]# service iptables restart
清除防火墙规则:                                           [确定]
把 chains 设置为 ACCEPT 策略:filter                       [确定]
正在卸载 Iiptables 模块:                                  [确定]
应用 iptables 防火墙规则:                                 [确定]
载入额外 iptables 模块:ip_conntrack_netbios_ns ip_conntrac[确定]

再重新连接 ,发现可以连接了

 

  • 2.字符集配置
1)vim /etc/my.cnf
2)添加配置,在[mysqld]节点下添加:
default-character-set=utf8
character-set-server=utf8
保存退出。
 
补充关于中文乱码问题:
1.在5.1版本时,为了解决中文乱码问题,在my.ini[mysql][mysqld]中都写:
default-character-set=utf8
2.在5.5版本,[mysql]内可以这么写,[mysqld]内不能这么写,而是写:
character-set-server=utf8
 
  • 3.自启动配置
1)执行chkconfig mysqld on
2)执行chkconfig —list mysqld查看(如果2-5位启用on状态即OK)
[root@localhost ~]# chkconfig --list mysqld
mysqld         0:关闭1:关闭2:关闭3:关闭4:关闭5:关闭6:关闭
[root@localhost ~]# chkconfig mysqld on
[root@localhost ~]# chkconfig --list mysqld
mysqld         0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭
 
  • 4.防火墙配置
1)sudo vim /etc/sysconfig/iptables
2)-A INPUT -p tcp -m tcp —dport 3306 -j ACCEPT
将以上配置添加到防火墙配置中,保存退出
3)执行命令重启防火墙
     sudo service iptables restart 
 
  • 5.MYSQL服务启动(Linux)
1.启动mysql服务service mysqld start或/etc/rc.d/init.d/mysqld start
2.mysql初始化环境设置
 因还未设置密码,执行mysql -u root登录mysql服务器
 
  • 6.mysql配置
1)查看目前mysql的用户
   Select user,host,password from mysql.user
2)修改root密码:
    set password for root@localhost=password(’你的密码')
    set password for root@127.0.0.1=password(‘你的密码’)
3)exit退出mysql
4)重新登录mysql输入mysql -u root -p
5)然后输入密码登录
mysql> set password for root@localhost=password('mysql_4U');
Query OK, 0 rows affected (0.00 sec)
 
mysql> exit
Bye
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.77 Source distribution
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 
mysql> 
 
6)删除匿名用户,执行以下SQL
查看是否有匿名用户:select user,host from mysql.user;
删除匿名用户:delete from mysql.user where user=‘’;
再次查看:select user,host from mysql.user;
刷新,使以上操作生效:flush privileges;
7)插入mysql新用户
Insert into mysql.user(Host,User,Paaword) values(“localhost”,”yourusername”,password(“yourpasswd”));
8)使操作生成:flush privileges;
9)创建新的database
Create database `mmall`
Default character set utf8 collate utf8_general_ci;
10)本地用户赋予所有权限
grant all privileges on mmall.* to yourusername@localhost identified by ‘yourpasswd'
11)给账号开通外围所有权限
grant all privileges on mmall.* to ‘yourusername’@‘%’ identified by ‘userpasswd’;
注:可以根据实际情况决定开通什么权限
grant select,insert,update on mmall.* to ‘yourusername’@‘192.11.11.11’ identified by ‘userpasswd’;
代表给指定的地址开通增改查权限
 
posted @ 2018-01-23 15:09  Zoey Chou  阅读(270)  评论(0编辑  收藏  举报