初学linux 路上遇过各种坑 把正确安装mysql-5.7.14分享一下
1.CentOs7.0 默认的数据库为MariaDB,先卸载MariaDB,否则安装mysql,引起冲突
先查看当前安装的MariaDB
rpm -qa|grep mariadb
之后下面就会列出现在的版本 使用
rpm -e --nodeps mariadb........
2.之后 创建文件夹 我是放在/home/mysql
mkdir /home/mysql/data
3.创建mysql用户组与用户
groupadd mysql useradd -g mysql -d /home/mysql mysql
PS.如果已经存在提示错误了 可以先删除
userdel mysql groupdel mysql
4.mysql5.7之前使用
./bin/mysql_install_db --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data
命令
但是 5.7以后已经放弃mysql_install_db
所以使用新的
./bin/mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize
这时有人会提示
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
缺少libaio.so 文件 对此文件进行安装
yum search libaio #查询文件 yum install libaio #安装文件
之后执行上面安装命令
2016-09-08T06:34:35.215617Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-09-08T06:34:37.175558Z 0 [Warning] InnoDB: New log files created, LSN=45790 2016-09-08T06:34:37.456478Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2016-09-08T06:34:37.529873Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 505a95f7-758e-11e6-a966-00163e0368de. 2016-09-08T06:34:37.531971Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2016-09-08T06:34:37.534742Z 1 [Note] A temporary password is generated for root@localhost: b/:q5Dd&mo-v
牢记上面的随机密码, 如上b/:q5Dd&mo-v
, 下面我们修改密码时需要用到。
PS.如果你的data文件有内容 会报
[ERROR] --initialize specified but the data directory has files in it. Aborting.
这个错误 正确的是把data文件清空 rm -rf *
5.之后我们运行一下
cd /home/mysql /support-files/mysql.server start 之后你会收到如下错误 ./support-files/mysql.server: line 276: cd: /usr/local/mysql: No such file or directory Starting MySQLCouldn't find MySQL server (/usr/local/mysql/[FAILED]ld_safe) 因为默认的目录是/usr/local/mysql 我们换了别的目录 只做继续修改 vi support-files/mysql.server 按I进入编辑模式(这是新手的福音 当年我就不知道) 找到 basedir=/home/mysql datadir=/home/mysql/data 进行修改成现在的目录 之后 ESC , :号后WQ回车 保存退出 继续运行 我还不信了 。。终于 成功了 Starting MySQL. [ OK ] 不容易啊。。。。 之后 我们继续 6.创建软链接 软链接就是两个地方都可以用一个文件 却不多占磁盘地方 在linux里还是很有用的 # ln -s /home/mysql/bin/mysql /usr/bin/mysql 7. 创建配置文件 将默认生成的my.cnf备份 # mv /etc/my.cnf /etc/my.cnf.bak 进入mysql的安装目录支持文件目录 # cd /home/mysql/support-files 拷贝配置文件模板为新的mysql配置文件, # cp my-default.cnf /etc/my.cnf 可按需修改新的配置文件选项, 不修改配置选项, mysql则按默认配置参数运行. 如下是我修改配置文件/etc/my.cnf, 设置编码为utf8以防乱码 # vim /etc/my.cnf [mysqld] basedir = /home/mysql datadir = /home/mysql/data character_set_server=utf8 init_connect='SET NAMES utf8' [client] # default-character-set=utf8 8.配置mysql服务开机自动启动 拷贝启动文件到/etc/init.d/下并重命令为mysqld # cp /home/mysql/support-files/mysql.server /etc/init.d/mysqld 增加执行权限 # chmod 755 /etc/init.d/mysqld 检查自启动项列表中没有mysqld这个,如果没有就添加mysqld: # chkconfig --list mysqld # chkconfig --add mysqld 设置MySQL在345等级自动启动 # chkconfig --level 345 mysqld on 或用这个命令设置开机启动: # chkconfig mysqld on
9. mysql服务的启动/重启/停止
启动mysql服务
# service mysqld start
重启mysql服务
# service mysqld restart
停止mysql服务
# service mysqld stop
10. 初始化mysql用户root的密码
先将mysql服务停止
# service mysqld stop
进入mysql安装目录, 执行:
cd /home/mysql ./bin/mysqld_safe --skip-grant-tables --skip-networking& [1] 6225[root@localhost mysql]# 151110 02:46:08 mysqld_safe Logging to '/home/mysql/data/localhost.localdomain.err'.151110 02:46:08 mysqld_safe Starting mysqld daemon with databases from /home/mysql/data12345
另外打开一个终端(p.s. 如果是ssh连接登录的, 另外创建一个ssh连接即可), 执行操作如下:
# mysql -u root mysql
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2Server version: 5.7.9 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> use mysql;
Database changed # mysql> UPDATE user SET password=PASSWORD('123456') WHERE user='root'; ERROR 1054 (42S22): Unknown column 'password' in 'field list' mysql> update user set authentication_string = PASSWORD('123456') where user = 'root'; Query OK, 1 row affected, 1 warning (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> \s -------------- mysql Ver 14.14 Distrib 5.7.9, for linux-glibc2.5 (x86_64) using EditLine wrapper Connection id: 2 Current database: mysql Current user: root@ SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.9 MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /tmp/mysql.sock Uptime: 4 min 47 sec Threads: 1 Questions: 43 Slow queries: 0 Opens: 127 Flush tables: 1 Open tables: 122 Queries per second avg: 0.149-------------- mysql> exit; Bye
到此, 设置完mysql用户root的密码且确保mysql编码集是utf8, 注意上面, 新版本的mysql.user表里的密码字段是authentication_string
快捷键ctrl + c
停止# ./bin/mysqld_safe ...
命令, 重新启动mysql服务, 用新密码连接mysql:
# service mysqld startStarting MySQL SUCCESS! [root@localhost bin]# mysql -uroot -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3Server version: 5.7.9Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use mysql; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql > exit; Bye
咦?又要我改密码, 我们通过mysqladmin来修改密码, 先输入原密码, 再设置新密码, 总算可以了吧!!!
# cd /home/mysql# ./bin/mysqladmin -u root -p passwordEnter password: New password: Confirm new password: Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.# mysql -uroot -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6Server version: 5.7.9 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 itsaffiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql>
或直接:
# ./bin/mysqladmin -uroot -p 'b/:q5Dd&mo-v' password '123456'
mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.1234
其中,
b/:q5Dd&mo-v
就是我们在使用
mysqld --initialize
时牢记下的随机密码
11. mysql远程授权
格式如下:
mysql> grant all [privileges] on db_name.table_name to 'username'@'host' identified by 'password';
示例如下:
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.04 sec)mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)mysql> 12345678
或用
mysql> grant all on *.* to 'root'@'%' identified by '123456';
打完收功!
ps.contos7使用firewall防火墙 可以先关闭进行远程连接
停用firewall:
systemctl stop firewalld
systemctl mask firewalld
试试远程开没开
之后再开启
systemctl start firewalld
开启防火墙端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
重启防火墙
systemctl restart firewalld
转载于:https://blog.51cto.com/hong1yuan/1850761