CentOS7 yum 安装mysql 5.6

转自:http://www.centoscn.com/mysql/2016/0315/6844.html

准备工作:
[root@localhost ~]# rpm -qa|grep mariadb  // 查询出来已安装的mariadb  
[root@localhost ~]# rpm -e --nodeps 文件名  // 卸载mariadb,文件名为上述命令查询出来的文件

[root@localhost ~]# rpm -qa|grep mysql  // 查询出来已安装的mysql
[root@localhost ~]# rpm -e --nodeps 文件名  // 卸载mysql,文件名为上述命令查询出来的文件  
1、下载 MySQL Yum Repository
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
2、添加 MySQL Yum Repository
rpm -ivh mysql-community-release-el7-5.noarch.rpm
3、验证下是否添加成功
yum repolist enabled | grep "mysql.*-community.*"
4、选择要启用 MySQL 版本

查看 MySQL 版本,执行

 yum repolist all | grep mysql

可以看到 5.5, 5.7 版本是默认禁用的,因为现在最新的稳定版是 5.6
可以通过类似下面的语句来启动某些版本

yum-config-manager --disable mysql56-community
yum-config-manager --enable mysql57-community-dmr

或者通过修改 /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 指启用。
注意: 任何时候,只能启用一个版本。
5、通过 Yum 来安装 MySQL
yum install mysql-community-server
6、配置/etc目录下的my.cnf文件
[root@localhost support-files]# vim /etc/my.cnf  

通过vim编辑器编辑my.cnf代码如下:

[mysql]
default-character-set=utf8   
socket=/var/lib/mysql/mysql.sock  

[mysqld]
skip-name-resolve
#设置3306端口  
port = 3306  
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装目录  
basedir=/var/lib/mysql
# 设置mysql数据库的数据的存放目录  
datadir=/var/lib/mysql/data
# 允许最大连接数  
max_connections=500 
# 服务端使用的字符集默认为8比特编码的latin1字符集  
character-set-server=utf8  
# 创建新表时将使用的默认存储引擎  
default-storage-engine=INNODB  
lower_case_table_name=1  
max_allowed_packet=300M  

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

======================================================
7、启动mysqld
[root@localhost mysql]# service mysqld start  
8、关闭mysqld
[root@localhost mysql]# service mysqld stop
9、MySQL 安全设置(设置密码),一定要先启动MYSQL

mysql_secure_installation;

	[root@server1 ~]# mysql_secure_installation
	 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] <– 是否设置root用户密码,输入y并回车或直接回车
	 New password: <– 设置root用户的密码
	 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] <– 是否删除匿名用户,生产环境建议删除,所以直接回车
	 … 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] <–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
	 … 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] <– 是否删除test数据库,直接回车
	 - Dropping test database…
	 … Success!
	 - 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] <– 是否重新加载权限表,直接回车
	 … Success!
	 Cleaning up…
	 All done! If you've completed all of the above steps, your MySQL
	 installation should now be secure.
	 Thanks for using MySQL!
	 [root@server1 ~]#
10、以root账户登录mysql,默认是没有密码的
[root@localhost mysql]# mysql -uroot -p  

要输入密码的时候直接回车即可。

11、设置root账户密码为root(也可以修改成你要的密码)
mysql>use mysql;  
mysql>update user set password=password('这里是要改成的密码') where user='root' and host='localhost';  
如果是mysql5.7操作为:mysqladmin -u root -h 127.0.0.1 -p password
mysql>flush privileges;
上面如果失败,因为mysql5.7 可能为
update user set authentication_string=password('123456') where user='root' and host='localhost'; 
12、设置远程主机登录,注意下面的your username 和 your password改成你需要设置的用户和密码
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '这里输入root用户的密码' WITH  GRANT OPTION;

或者设置其它用户:

创建一个普通用户 sa ,密码是 some_pass
	CREATE USER 'sa'@'%' IDENTIFIED BY 'some_pass';

	给这个用户授予 SELECT,INSERT,UPDATE,DELETE 的远程访问的权限,这个账号一般用于提供给实施的系统访问
	GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO 'sa'@'%';

	创建一个管理员用户 admin 账号 ,密码是 some_pass
	CREATE USER 'admin'@'%' IDENTIFIED BY 'some_pass';

	给这个用户授予所有的远程访问的权限。这个用户主要用于管理整个数据库、备份、还原等操作。
	GRANT ALL ON *.* TO 'admin'@'%';

	使授权立刻生效
	flush privileges;
13、备份、还原

备份

mysqldump --socket=/home/data/mysql/mysql.sock --single-transaction=TRUE -u root -p emsc > emsc.sql

还原

mysql --socket=/home/data/mysql/mysql.sock -u root -p emsc < emsc.sql
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值