1、检查服务器上是否已经安装了mysql
yum list installed | grep mysql
注1:(CentOS默认自带MySQL5.1的下载链接)
注2:如果是用免安装版的MYSQL版本;卸载时用命令为:
yum remove mysql mysql-server mysql-libs mysql-server
find / -name mysql #然后把找到的所有的和mysql相关的文件都delete掉
rpm -qa|grep mysql #然后把找到的文件用yum remove 文件名删除掉
2、删除CentOS6.5自带的mysql源(CentOS7中无需此步骤)
rpm -e mysql-libs.x86_64 --nodeps
yum remove 'mysql*'
3、安装mysql5.7的安装源
CentOS7.X上配置yum源:
wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
rpm -ivh mysql80-community-release-el7-1.noarch.rpm
CentOS6.X上配置yum源:
wget https://dev.mysql.com/get/mysql80-community-release-el6-1.noarch.rpm
rpm -ivh mysql80-community-release-el6-1.noarch.rpm
如果你需要安装mysql5.8版本,上述过程执行以后无需做任何修改,默认安装的mysql5.8的安装源;
如果要安装mysql5.5、mysql5.6、mysql5.7的版本,需要修改上述安装的repo文件:
vim /etc/yum.repos.d/mysql-community.repo
只需根据需要修改下列属性中的enabled即可:enabled=1表示可用,enabled=0表示不可用;如果要安装mysql5.6只需将【mysql56-community】属性中的enabled设置为1即可,其他55、57中的均设置为0即可,其它操作同理!
......
# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# 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/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
......
4、安装mysql
yum install mysql-community-server -y
5、启动初始化mysql
启动mysql:
service mysqld start
CentOS7中mysql启动命令也可以用命令:systemctl start mysqld
mysql5.7查看临时密码并重置密码
shell> sudo grep 'temporary password' /var/log/mysqld.log
shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
执行mysql_secure_installation的时候,如果以前安装过mysql或者忘记了mysql的原始密码,可能会报错如下:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
解决办法:
vim /etc/my.cnf
在mysqld下加上:skip-grant-tables
启动mysql: service mysqld start
登录mysql: mysql
修改密码(重点):
mysql5.7不建议用update去修改密码:
update mysql.user set password=password('root') where user='root'
这样修改的话,会报错,mysql5.7密码的字段不叫password,应该用命令:
update mysql.user set authentication_string=password('123456') where user='root'
这样修改也能成功,但是后面操作数据库的话会报错。
此处两个问题:
第一:密码应该为:小写字母,大写字母,符号,数字,字符数>8
第二:修改密码应该用命令:ALTER USER 'root'@'localhost'IDENTIFIED BY 'aaBBcc11%22&33'
如果设置允许远程登录,可以用:ALTER USER 'root'@'%' IDENTIFIED BY 'aaBBcc11%22&33'
修改好之后flush privileges刷新一下
然后将my.cnf里面的配置改回来,重启mysql:service mysqld restart
service mysqld stop
#如果是CentOS7,执行如下命令:systemctl stop mysqld
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
#密码'123456'可以根据实际需要进行修改
mysql> update user set password=password('123456') where user='root' and host='root' or host='localhost';
mysql> flush privileges;
mysql> quit;
#重启mysql
service mysqld restart
#如果是CentOS7,执行如下命令:systemctl restart mysqld
上述操作以后可以用新设置的密码登录mysql;但是还可能报以下错误
You must SET PASSWORD before executing this statement
解决方法:
# mysql -uroot -p
# mysql密码可以根据需求自行修改,但是一定要和之前设置的一致
mysql > set password=password('123456');
mysql > quit
初始化mysql:
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] y
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] n
... skipping.
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
- 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...
6、修改mysql默认编码,并设置mysql的最大连接数
vim /etc/my.cnf
修改编码为utf8:(以下选项缺一不可,踩过很多坑,才终于修成正果)
[mysqld]
......
max_connections=1000
character-set-server=utf8
[mysql]
no-auto-rehash
default-character-set=utf8
......
修改编码为gbk:(以下选项缺一不可)
[mysqld]
......
max_connections=1000
character-set-server=gbk
[mysql]
no-auto-rehash
default-character-set=gbk
......
修改配置文件以后重启mysql:
service mysqld restart
查询mysql字符集:
# mysql -uroot -p
mysql > status;
#查看mysql当前字符集
mysql > show variables like 'character_set%';
#查看mysql最大链接数
mysql > show variables like 'max_connections';
CentOS7中重启mysql推荐用命令:systemctl restart mysqld
7、为mysql赋权限,使外界可以访问mysql数据库
授权
mysql -uroot -p
GRANT REPLICATION SLAVE ON *.* to 'root'@'localhost' identified by '123456';
GRANT REPLICATION SLAVE ON *.* to 'root'@'%' identified by '123456';
FLUSH PRIVILEGES;
exit;
修改配置文件以后重启mysql:
service mysqld restart
CentOS7中重启mysql推荐用命令:systemctl restart mysqld
8、为mysql创建普通用户
mysql -uroot -p
CREATE USER 'test'@'192.168.111.11' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON test.* TO test@'192.168.111.11' IDENTIFIED by '123456';
FLUSH PRIVILEGES;
修改配置文件以后重启mysql:
service mysqld restart
CentOS7中重启mysql推荐用命令:systemctl restart mysqld
总结:经过以上几个步骤,在CentOS6.X及CentOS7.X中mysql5.5、mysql5.6、mysql5.7或者mysql5.8的安装就完成了,大家就可以安心的来使用数据库进行开发了!
接下来的篇幅中还有mysql的主从配置、mysql的高可用配置、mysql的数据库备份、mysql集群的搭建等,敬请期待!