创建密码:
mysqladmin -u root -p password 123
或
MariaDB [(none)]> use mysqlMariaDB [mysql]> update user set password=password("123456")where user='root';Query OK, 0 rows affected (0.00 sec)Rows matched: 4 Changed: 0 Warnings: 0MariaDB [mysql]> flush privileges;Query OK, 0 rows affected (0.00 sec)
开启远程服务:
查看user表:可以发现表中没有开启远程的服务 % | root
mysql> use mysql;Database changedmysql> select host,user,password from user;+--------------+------+-------------------------------------------+| host | user | password |+--------------+------+-------------------------------------------+| localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E || 127.0.0.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |+--------------+------+-------------------------------------------+2 rows in set (0.00 sec)
实现远程连接(授权法):
将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。
update user set host = ’%’ where user = ’root’; 将权限改为ALL PRIVILEGES
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '12345' WITH GRANT OPTION;Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)mysql> select host,user,password from user; +-----------+------+-------------------------------------------+ | host | user | password | +-----------+------+-------------------------------------------+ | localhost | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | 127.0.0.1 | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | ::1 | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | | localhost | | | | % | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | +-----------+------+-------------------------------------------+ 5 rows in set (0.00 sec)
这样机器就可以以用户名root密码远程访问该机器上的MySql.
创建数据库(在root权限下)
create database mydb;//授权admin用户拥有mydb数据库的所有权限。>grant all privileges on mydb.* to admin@localhost identified by 'admin';//刷新系统权限表mysql>flush privileges;删除用户。@>mysql -u root -p@>密码mysql>DELETE FROM user WHERE User="admin" and Host="localhost";mysql>flush privileges;//删除用户的数据库mysql>drop database mydb;修改指定用户密码。@>mysql -u root -p@>密码mysql>update mysql.user set password=password('新密码') where User="admin" and Host="localhost";mysql>flush privileges;