CentOS 7 中mysql 的安装详解
Mysql 下载地址:
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
下载成功后,将文件进行解压:
tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
将文件移动到/usr/local/下
mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql
cd /usr/local/mysql/
创建数据库存放目录:
mkdir /usr/local/mysql/data
创建数据库日志存放目录:
mkdir /usr/local/mysql/logs
创建mysql用户组:
groupadd mysql
查看mysql用户组是否创建成功
tail /etc/group
创建mysql 用户,并指定所属用户组mysql 且限制登录,只允许运行数据库
useradd -r -g mysql -s /sbin/nologin mysql
查看mysql 用户:
tail /etc/shadow
授权mysql用户和mysql用户组拥有/usr/local/mysql 目录的访问权限
chown -R mysql:mysql /usr/local/mysql/
进入mysql的bin目录下,初始化数据库的基本信息:
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
./mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data/
进入mysql 的support-files目录下,将my-default.cnf复制到/etc下并重命名my.cnf
cd /usr/local/mysql/support-files/
cp my-default.cnf /etc/my.cnf
编辑my.cnf配置文件并保存:
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. innodb_buffer_pool_size = 1G # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. log_bin # These are commonly set, remove the # and set as required. basedir =/usr/local/mysql datadir =/usr/local/mysql/data port = 3306 server_id = 22209 socket = /tmp/mysql.sock binlog_format =statement log-error=/usr/local/mysql/data/error.log pid-file=/usr/local/mysql/data/mysql.pid user=mysql skip-grant-tables # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. join_buffer_size = 128M sort_buffer_size = 2M read_rnd_buffer_size = 2M log_bin_trust_function_creators=on sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES lower_case_table_names=1 再复制mysql.server 到/etc/init.d/mysql下 cp mysql.server /etc/init.d/mysql
编辑/etc/init.d/mysql 配置文件并保存:
启动mysql 服务
/etc/init.d/mysql start
出现一下提示,表明启动成功
登录mysql
mysql -h localhost -u root -p
设置允许远程登录密码:
usr mysql;
update user set authentication_string=password('root123') where user='root';
flush privileges;
查看centos 防火墙状态:
systemctl status firewalld
查看防火墙是否允许访问3306端口:
firewall-cmd --zone=public --query-port=3306/tcp
如果返回胡的是:no ,则需要调为允许访问3306端口,才能远程访问mysql。
开启防火墙允许访问服务器3306端口:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
重新加载防火墙配置,才即可生效:
firewall-cmd --reload
基本上mysql的配置基本已经完成,可以使用mysql 管理工具连接数据库。
创建一个数据库,然后试试远程连接。
(在稍微强化一下配置信息)
将mysql 的bin目录加入到环境变量中,并重载环境变量文件。
vi /etc/profile
重载环境变量文件:
source /etc/profile
将mysql 加入服务列表,并限制执行等级。
chkconfig --add mysql
(注:这里的mysql不是安装路径bin目录下的mysql可执行文件,而且/etc/init.d/mysql ,如果在/etc/init.d/mysqld的话,就是设置为:chkconfig -add mysqld 了)
chkconfig --level 345 mysql on
现在可以使用service 命令操作mysql服务了。
如:查看mysql服务状态
service mysql status
以下是在配置mysql时遇到的问题及解决方法:
启动mysql服务报:[ERROR] SSL error: Unable to get private key from 'server-key.pem'
解决方法:在mysql的数据库目录中/usr/local/mysql/data下server-key.pem文件没有读取权限,需要分配-r 权限
cd /usr/local/mysql/data
chmod -r server-key.pem
启动mysql服务报:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
解决方法:在/etc/my.cnf配置文件中,[mysqld]后面配置socket = /tmp/mysql.sock。
登录mysql 数据库时报:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
解决方法:在/etc/my.cnf配置文件中,[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程。