Centos7 安装Mysql(在线安装)
1.一般 Centos7 中都默认安装 mariadb 所以安装Mysql的话需要先移除 mariadb
首先查看都安装了哪些的mariadb相关的模块。
1 |
rpm -qa | grep mariadb |
然后进行卸载。
1 |
rpm -e mariadb-libs-5.5.52-1.el7.x86_64 |
会提示有依赖于这的安装包,那么就强制卸载,不查检依赖:
1 |
rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64 |
将所有的mariadb安装包都删除,然后自行安装MySQL就可以了。
2.正确的安装方法
众所周知,Linux系统自带的repo是不会自动更新每个软件的最新版本(基本都是比较靠后的稳定版),所以无法通过yum方式安装MySQL的高级版本。所以我们需要先安装带有当前可用的mysql5系列社区版资源的rpm包。
#安装rpm包
1 |
rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm |
这时查看当前可用的mysql安装资源:
1 |
yum repolist enabled | grep "mysql.*-community.*" |
从上面的列表可以看出, mysql56-community/x86_64 和 MySQL 5.6 Community Server 可以使用。
因此,我们就可以直接用yum方式安装了MySQL5.6版本了。
1 |
yum -y install mysql-community-server |
3.MySQL安装完成后,进行相关配置
安装完MySQL后,需要进行一些基础配置工作:
#安装成功后,将其加入开机启动
1 |
systemctl enable mysqld |
#启动mysql服务进程
1 |
systemctl start mysqld |
#配置mysql(设置密码等)
1 |
mysql_secure_installation |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): 【直接回车】 OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation. Set root password? [Y/n] y 【设置root用户密码】 New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y 【删除匿名用户】 ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] y 【禁止root远程登录】 ... Success! By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] y 【删除test数据库】 - Dropping test database... ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist ... Failed! Not critical, keep moving... - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y 【刷新权限】 ... Success! All done! If you've completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL! Cleaning up... |
进入mysql
1 |
mysql -uroot -p |
输入密码
1 2 |
mysql>use mysql; mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; |
开启远程访问权限“%”代表所有人
查询设置
1 |
select host,user,password from mysql.user; |
开启MySQL用户的远程访问权限
mysql -u root -p mysql # 第1个mysql是执行命令,第2个mysql是系统数据名称
在MySQL控制台执行:
1 |
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option; |
# root是用户名,%代表任意主机,’123456’指定的登录密码(这个和本地的root密码可以设置不同的,互不影响)
1 2 |
flush privileges; # 重载系统权限 exit; |
如果想允许用户root从ip为192.168.137.99的主机连接到MySQL服务:
1 2 |
grant all privileges on *.* to 'root'@'192.168.137.99' identified by '123456' with grant option; flush privileges; |
设置防火墙,让 3306 端口对外可访问
1 2 3 4 5 6 |
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT # 查看规则是否生效 iptables -L -n # 或者: service iptables status # 此时生产环境是不安全的,远程管理之后应该关闭端口,删除之前添加的规则 iptables -D INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT |
注意:上面iptables添加/删除规则都是临时的,如果需要重启后也生效,需要保存修改:
1 |
service iptables save # 或者: /etc/init.d/iptables save |
另外,
1 2 |
vi /etc/sysconfig/iptables # 加上下面这行规则也是可以的 -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT |
远程管理数据库的软件,Windows系统下可以使用SQLyog,用了几种远程软件,感觉这个用起来蛮不错的。
参考资料: